前面的文章已经讲到了如何在一个Activity中与用户进行交互。这篇文章将解释如何创建多个Activity的用户界面。
这个多Activity的示例程序的功能大概是这样的:
- 在主页面上有三个元素:一个文本输入框,两个按钮。用户可以在文本框中输入一些字符,如"This is PalmCity"
- 点击第一个按钮,将弹出一个对话框形式的Activity,显示”Hi, welcome to get aboard Android adventure!+回车+你输入的内容”
- 如果点击的是第二个按钮,将显示另一个全屏的Activity,显示”Hi, welcome to get aboard Android adventure!+回车+你输入的内容”
OK,按照前面的规矩,分成几步:
第1步,先完成UI的设计,创建布局。
从程序来看,似乎有三个Activity,但是第二个和第三个几乎是相同的,于是,只创建两个布局:
res/layout/main.xml
res/layout/main.xml
<?
xml
version
="1.0"
encoding
="utf-8"
?>
< LinearLayout xmlns:android ="http://schemas.android.com/apk/res/android"
android:orientation ="vertical"
android:layout_width ="fill_parent"
android:layout_height ="fill_parent"
>
< EditText
android:id ="@+id/textview_input"
android:layout_width ="fill_parent"
android:layout_height ="wrap_content"
android:textSize ="20px"
android:textStyle ="bold"
/>
< Button
android:id ="@+id/button_hi_dialog"
android:layout_width ="fill_parent"
android:layout_height ="wrap_content"
android:text ="@string/sayhi_dialog"
/>
< Button
android:id ="@+id/button_hi_activity"
android:layout_width ="fill_parent"
android:layout_height ="wrap_content"
android:text ="@string/sayhi_activity"
/>
</ LinearLayout >
< LinearLayout xmlns:android ="http://schemas.android.com/apk/res/android"
android:orientation ="vertical"
android:layout_width ="fill_parent"
android:layout_height ="fill_parent"
>
< EditText
android:id ="@+id/textview_input"
android:layout_width ="fill_parent"
android:layout_height ="wrap_content"
android:textSize ="20px"
android:textStyle ="bold"
/>
< Button
android:id ="@+id/button_hi_dialog"
android:layout_width ="fill_parent"
android:layout_height ="wrap_content"
android:text ="@string/sayhi_dialog"
/>
< Button
android:id ="@+id/button_hi_activity"
android:layout_width ="fill_parent"
android:layout_height ="wrap_content"
android:text ="@string/sayhi_activity"
/>
</ LinearLayout >
res/layout/dialog.xml
<?
xml
version
="1.0"
encoding
="utf-8"
?>
< LinearLayout
xmlns:android ="http://schemas.android.com/apk/res/android"
android:layout_width ="wrap_content"
android:layout_height ="wrap_content" >
< TextView
android:layout_width ="fill_parent"
android:layout_height ="wrap_content"
android:id ="@+id/textview_hi"
android:text ="@string/hi"
/>
</ LinearLayout >
< LinearLayout
xmlns:android ="http://schemas.android.com/apk/res/android"
android:layout_width ="wrap_content"
android:layout_height ="wrap_content" >
< TextView
android:layout_width ="fill_parent"
android:layout_height ="wrap_content"
android:id ="@+id/textview_hi"
android:text ="@string/hi"
/>
</ LinearLayout >
第2步,然后呢,需要把上面提到的字符变量声明一下:
res/values/strings.xml
res/values/strings.xml
<?
xml
version
="1.0"
encoding
="utf-8"
?>
< resources >
< string name ="hi" >Hi, welcome to get aboard Android adventure! </ string >
< string name ="app_name" >Adventure </ string >
< string name ="sayhi_dialog" >Hi(Dialog) </ string >
< string name ="sayhi_activity" >Hi(Activity) </ string >
</ resources >
< resources >
< string name ="hi" >Hi, welcome to get aboard Android adventure! </ string >
< string name ="app_name" >Adventure </ string >
< string name ="sayhi_dialog" >Hi(Dialog) </ string >
< string name ="sayhi_activity" >Hi(Activity) </ string >
</ resources >
第3步,创建Activity,这里和第一步一样,虽然有三个Activity,但是看起来用两个其实就够了。然而事实并非如此,具体原因后续,我们需要定义三个Activity:分别将他们定义为:Adventure(用来显示主画面)、 AlertDialog(用来显示对话框)和AlertActivity(用来显示那个全屏的Activity)。
主Activity(Adventure.java):
package com.penguin.adventure;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class Adventure extends Activity {
private static final String TAG = "Main Activity";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "onCreate ...");
setContentView(R.layout.main);
final EditText edt = (EditText) findViewById(R.id.textview_input);
// try to display a dialog
Button btnDialog = (Button) findViewById(R.id.button_hi_dialog);
btnDialog.setOnClickListener( new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.putExtra( "str", edt.getText().toString());
intent.setClass(Adventure. this, AlertDialog. class);
startActivity(intent);
}
});
// try to call and display a new activity
Button btnActivity = (Button) findViewById(R.id.button_hi_activity);
btnActivity.setOnClickListener( new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.putExtra( "str", edt.getText().toString());
intent.setAction( "com.penguin.action.Main");
startActivity(intent);
}
});
}
@Override
public void onStart() {
super.onStart();
Log.d(TAG, "onStart ...");
}
@Override
public void onResume(){
super.onResume();
Log.d(TAG, "onResume ...");
}
@Override
public void onPause(){
super.onPause();
Log.d(TAG, "onPause ...");
}
@Override
public void onStop(){
super.onStop();
Log.d(TAG, "onStop ...");
}
@Override
public void onDestroy(){
super.onDestroy();
Log.d(TAG, "onDestroy ...");
}
@Override
public void onRestart(){
super.onRestart();
Log.d(TAG, "onRestart ...");
}
}
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class Adventure extends Activity {
private static final String TAG = "Main Activity";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "onCreate ...");
setContentView(R.layout.main);
final EditText edt = (EditText) findViewById(R.id.textview_input);
// try to display a dialog
Button btnDialog = (Button) findViewById(R.id.button_hi_dialog);
btnDialog.setOnClickListener( new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.putExtra( "str", edt.getText().toString());
intent.setClass(Adventure. this, AlertDialog. class);
startActivity(intent);
}
});
// try to call and display a new activity
Button btnActivity = (Button) findViewById(R.id.button_hi_activity);
btnActivity.setOnClickListener( new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.putExtra( "str", edt.getText().toString());
intent.setAction( "com.penguin.action.Main");
startActivity(intent);
}
});
}
@Override
public void onStart() {
super.onStart();
Log.d(TAG, "onStart ...");
}
@Override
public void onResume(){
super.onResume();
Log.d(TAG, "onResume ...");
}
@Override
public void onPause(){
super.onPause();
Log.d(TAG, "onPause ...");
}
@Override
public void onStop(){
super.onStop();
Log.d(TAG, "onStop ...");
}
@Override
public void onDestroy(){
super.onDestroy();
Log.d(TAG, "onDestroy ...");
}
@Override
public void onRestart(){
super.onRestart();
Log.d(TAG, "onRestart ...");
}
}
在这里面:
以下语句,前文都提到过:
以下语句,前文都提到过:
- final EditText edt = (EditText) findViewById(R.id.textview_input)
- btn_Dialog.setOnClickListener(new OnClickListener())
主要说一下这些:
- Log.d(TAG, “onStart …”);
这是Android中日志的处理方式,用法于Log4j类似,但是要比Log4j简单的多,因为用的几乎都是static的方法。分别是Log.v 、 Log.d 、 log.i、 log.w、 log.e。使用Log打印的日志通过LogCat可以看到。方法中的第一个属性一般用来标识一下日志是谁打印出来的,便于查找。 - Intent intent = new Intent();
对UI来说,这里面的Intent是一个非常重要的概念。并且要理解它会比理解View、Activity什么的要困难一些,在这里,只需要知道:通过它,可以找到下一个要显示的Activity,并为这个Activity携带了一些数据。也就是说它有两个重要的使命:定位下一个Activity;并捎带一些数据。所以在Android的文档中,称Intent为Activity之间的双面胶。 - startActivity(intent);
通过startActivity(intent)可以启动并显示另外一个Activity。
第2个Activity(AlertDialog.java):
package com.penguin.adventure;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class AlertDialog extends Activity {
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.dialog);
Intent intent = getIntent();
String str = (String) intent.getExtras().get( "str");
TextView txv = (TextView) findViewById(R.id.textview_hi);
if( null != str){
String hi = getResources().getString(R.string.hi);
txv.setText(hi + "/n" +str);
}
}
}
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class AlertDialog extends Activity {
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.dialog);
Intent intent = getIntent();
String str = (String) intent.getExtras().get( "str");
TextView txv = (TextView) findViewById(R.id.textview_hi);
if( null != str){
String hi = getResources().getString(R.string.hi);
txv.setText(hi + "/n" +str);
}
}
}
在这个Activity里面,只有一个语句需要解释一下:
- String name = (String) intent.getExtras().get(”str”);
用来获得前面Activity的intent.putExtra("str", edt.getText().toString());捎带过来的数据
第3个Activity(AlertActivity.java):
package com.penguin.adventure;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class AlertActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.dialog);
Intent intent = getIntent();
String str = (String) intent.getExtras().get( "str");
TextView txv = (TextView) findViewById(R.id.textview_hi);
if( null != str){
String hi = getResources().getString(R.string.hi);
txv.setText(hi + "/n" + str);
}
}
}
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class AlertActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.dialog);
Intent intent = getIntent();
String str = (String) intent.getExtras().get( "str");
TextView txv = (TextView) findViewById(R.id.textview_hi);
if( null != str){
String hi = getResources().getString(R.string.hi);
txv.setText(hi + "/n" + str);
}
}
}
到这一步,所有的事情似乎都已经完成了,可以运行了,如果在这时你等不及而去运行程序,将会得到一些 ActivityNotFoundException 之类的异常。这里还需要一步,如果以前写的程序都是单Activity的,那么Eclipse的Android插件会自动完成这一步,如果是多 Activity的,或者需要对Activity作更仔细的控制,那么就要这一步了。
第4步,编辑AndroidManifest.xml
这个文件像一个户口簿一样记录每一个Activity的信息,Android系统会通过这个文件得到:执行这个程序的时候,启动的是那个Activity 等信息。不仅如此,这个还包括Intent、Provider等其他一些信息。如果你用过一些Web MVC框架的话,这个文件的功能就像是那个MVC的配置文件。
这个文件像一个户口簿一样记录每一个Activity的信息,Android系统会通过这个文件得到:执行这个程序的时候,启动的是那个Activity 等信息。不仅如此,这个还包括Intent、Provider等其他一些信息。如果你用过一些Web MVC框架的话,这个文件的功能就像是那个MVC的配置文件。
<?
xml
version
="1.0"
encoding
="utf-8"
?>
< manifest xmlns:android ="http://schemas.android.com/apk/res/android"
package ="com.penguin.adventure"
android:versionCode ="1"
android:versionName ="1.0" >
< application android:icon ="@drawable/icon" android:label ="@string/app_name" >
< activity android:name =".Adventure"
android:label ="@string/app_name" >
< intent-filter >
< action android:name ="android.intent.action.MAIN" />
< category android:name ="android.intent.category.LAUNCHER" />
</ intent-filter >
</ activity >
< activity android:name =".AlertDialog"
android:theme ="@android:style/Theme.Dialog"
android:label ="@string/app_name" >
</ activity >
< activity android:name =".AlertActivity"
android:label ="@string/app_name" >
< intent-filter >
< action android:name ="com.penguin.action.Main" />
< category android:name ="android.intent.category.DEFAULT" />
</ intent-filter >
</ activity >
</ application >
< uses-sdk android:minSdkVersion ="3" />
</ manifest >
< manifest xmlns:android ="http://schemas.android.com/apk/res/android"
package ="com.penguin.adventure"
android:versionCode ="1"
android:versionName ="1.0" >
< application android:icon ="@drawable/icon" android:label ="@string/app_name" >
< activity android:name =".Adventure"
android:label ="@string/app_name" >
< intent-filter >
< action android:name ="android.intent.action.MAIN" />
< category android:name ="android.intent.category.LAUNCHER" />
</ intent-filter >
</ activity >
< activity android:name =".AlertDialog"
android:theme ="@android:style/Theme.Dialog"
android:label ="@string/app_name" >
</ activity >
< activity android:name =".AlertActivity"
android:label ="@string/app_name" >
< intent-filter >
< action android:name ="com.penguin.action.Main" />
< category android:name ="android.intent.category.DEFAULT" />
</ intent-filter >
</ activity >
</ application >
< uses-sdk android:minSdkVersion ="3" />
</ manifest >
好了,我们可以运行这个程序了。

