Android开发基础
环境搭建
在开始Android开发之前,你需要搭建一个开发环境。以下是一个基本的步骤指南:
- 安装Android Studio:Android Studio是Google官方推荐的Android开发工具,它包含了Android开发所需的所有功能。
# 下载Android Studio
wget https://dl.google.com/dl/android/studio/ide/3.5.3.0/running安装在本地
# 解压文件
tar -xvzf android-studio-ide-2023.1.1.236.9363999-linux.tar.gz
# 进入目录
cd android-studio/bin
# 启动Android Studio
./studio.sh
- 安装JDK:Android Studio需要JDK 1.8或更高版本。
# 下载JDK
wget https://download.java.net/java/ga/jdk18/latest/jdk-18_linux-x64_bin.tar.gz
# 解压文件
tar -xvzf jdk-18_linux-x64_bin.tar.gz
# 配置环境变量
export JAVA_HOME=/path/to/jdk
export PATH=$JAVA_HOME/bin:$PATH
- 配置Android模拟器:Android Studio提供了模拟器来测试你的应用。
# 启动模拟器
android-studio/bin/avdmanager create avd --name "Nexus_5" --package 'system-images;android-28;google_apis;x86_64' --target '28'
Android UI设计
Android UI设计主要是通过XML文件来定义的。以下是一个简单的例子:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello, Android!"
android:layout_centerInParent="true" />
</RelativeLayout>
在这个例子中,我们创建了一个RelativeLayout布局,其中包含一个TextView组件。
Activity生命周期
Android中的Activity有几种状态,包括创建、启动、运行、暂停和停止。以下是一个简单的Activity示例:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
protected void onStart() {
super.onStart();
// 当Activity从不可见状态变为可见状态时调用
}
@Override
protected void onResume() {
super.onResume();
// 当Activity从后台变为前台时调用
}
@Override
protected void onPause() {
super.onPause();
// 当Activity从前台变为后台时调用
}
@Override
protected void onStop() {
super.onStop();
// 当Activity不可见时调用
}
@Override
protected void onDestroy() {
super.onDestroy();
// 当Activity销毁时调用
}
}
在这个例子中,我们重写了Activity的几个生命周期方法。
实战案例
案例一:简单的计数器应用
在这个案例中,我们将创建一个简单的计数器应用,用户可以通过按钮来增加或减少计数。
- 创建新的Activity:在Android Studio中创建一个新的Activity,命名为
CounterActivity。 - 设计UI:在
activity_counter.xml文件中设计UI,包含两个按钮和一个TextView。 - 编写代码:在
CounterActivity中编写代码,处理按钮点击事件,更新TextView中的计数。
以下是activity_counter.xml文件的内容:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/button_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
<Button
android:id="@+id/button_subtract"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true" />
<TextView
android:id="@+id/textView_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:layout_below="@id/button_add"
android:layout_centerHorizontal="true" />
</RelativeLayout>
以下是CounterActivity的代码:
public class CounterActivity extends AppCompatActivity {
private int count = 0;
private TextView textViewCount;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_counter);
textViewCount = findViewById(R.id.textView_count);
Button buttonAdd = findViewById(R.id.button_add);
buttonAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
count++;
textViewCount.setText(String.valueOf(count));
}
});
Button buttonSubtract = findViewById(R.id.button_subtract);
buttonSubtract.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
count--;
textViewCount.setText(String.valueOf(count));
}
});
}
}
案例二:简单的列表应用
在这个案例中,我们将创建一个简单的列表应用,用户可以从列表中选择一个项目。
- 创建新的Activity:在Android Studio中创建一个新的Activity,命名为
ListActivity。 - 设计UI:在
activity_list.xml文件中设计UI,包含一个ListView和一个按钮。 - 编写代码:在
ListActivity中编写代码,加载列表数据,处理按钮点击事件。
以下是activity_list.xml文件的内容:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/button_show"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Selected Item"
android:layout_below="@id/listView"
android:layout_centerHorizontal="true" />
</RelativeLayout>
以下是ListActivity的代码:
public class ListActivity extends AppCompatActivity {
private ListView listView;
private String[] items = {"Item 1", "Item 2", "Item 3", "Item 4", "Item 5"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
listView = findViewById(R.id.listView);
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, items);
listView.setAdapter(adapter);
Button buttonShow = findViewById(R.id.button_show);
buttonShow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int position = listView.getCheckedItemPosition();
if (position != ListView.INVALID_POSITION) {
Toast.makeText(ListActivity.this, "Selected: " + items[position], Toast.LENGTH_SHORT).show();
}
}
});
}
}
以上是两个简单的Android编程实战案例。通过这些案例,你可以学习到Android开发的基本知识,并开始创建自己的Android应用。
