前面的文章已经讲到了如何在一个Activity中与用户进行交互。这篇文章将解释如何创建多个Activity的用户界面。
- 在主页面上有三个元素:一个文本输入框,两个按钮。用户可以在文本框中输入一些字符,如"This is PalmCity"
- 点击第一个按钮,将弹出一个对话框形式的Activity,显示”Hi, welcome to get aboard Android adventure!+回车+你输入的内容”
- 如果点击的是第二个按钮,将显示另一个全屏的Activity,显示”Hi, welcome to get aboard Android adventure!+回车+你输入的内容”
OK,按照前面的规矩,分成几步:
res/layout/main.xml
<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: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>
res/values/strings.xml
<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>
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):
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);
}
}
}
- String name = (String) intent.getExtras().get(”str”);
用来获得前面Activity的intent.putExtra("str", edt.getText().toString());捎带过来的数据
第3个Activity(AlertActivity.java):
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);
}
}
}
这个文件像一个户口簿一样记录每一个Activity的信息,Android系统会通过这个文件得到:执行这个程序的时候,启动的是那个Activity 等信息。不仅如此,这个还包括Intent、Provider等其他一些信息。如果你用过一些Web MVC框架的话,这个文件的功能就像是那个MVC的配置文件。
<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>
注意这里面的第二个和第三个Activity声明方式不一样,这个留待讲述Intent的时候再讲。
好了,我们可以运行这个程序了。