代码:
package com.oyzz.ch3_5;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class Ch3_5 extends Activity {
/** Called when the activity is first created. */
public EditText ev = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn = (Button)findViewById(R.id.button1);
btn.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
//实现跳转到另外一个Activity
Intent it = new Intent();
it.setClass(Ch3_5.this, Ch3_5_1.class);
ev =(EditText)findViewById(R.id.ev1);
String name = ev.getText().toString();
//创建一个Bundle用来传递数据
Bundle b = new Bundle();
b.putString("name", name);
b.putString("sex", "男");
//讲Bundle对象放入Intent里面
it.putExtras(b);
//startActivity(it);//开启另外一个Activity
startActivityForResult(it, 0);
//Ch3_5.this.finish();//释放自己
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (resultCode) {
case RESULT_OK:
//得到Bundle并且取出数据
Bundle b= data.getExtras();
String name = b.getString("name");
ev.setText(name);
break;
default:
break;
}
}
}
package com.oyzz.ch3_5;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class Ch3_5_1 extends Activity {
/** Called when the activity is first created. */
Bundle bunde;
Intent intent;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main1);
//先得到Bunle对象
intent =this.getIntent();
bunde = intent.getExtras();
//讲里面的值取出
String name = bunde.getString("name");
String sex = bunde.getString("sex");
TextView tv = (TextView)findViewById(R.id.text2);
tv.setText(name+":"+sex);
Button btn = (Button)findViewById(R.id.button2);
btn.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
//创建一个Intent,并且指定要启动的类
//Intent it = new Intent();
//it.setClass(Ch3_5_1.this, Ch3_5.class);
//重新启动一个Activity
//startActivity(it);
Ch3_5_1.this.setResult(RESULT_OK,intent);
//释放当前Activity
Ch3_5_1.this.finish();
}
});
Button btn1 = (Button)findViewById(R.id.button3);
btn1.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
new AlertDialog.Builder(Ch3_5_1.this)//到当前窗口显示
.setTitle(R.string.Dialog_title)//标题
.setMessage(R.string.Dialog_content)//内容
.setPositiveButton(R.string.Dialog_btn, new DialogInterface.OnClickListener() {//添加按钮,和实现事件
public void onClick(DialogInterface dialog, int which) {
TextView tv = (TextView)findViewById(R.id.text2);
tv.setText("点击成功.......");
}
}).show();
}
});
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/text1"
android:textSize="24sp"
android:layout_width="186px"
android:layout_height="29px"
android:layout_x="70px"
android:layout_y="32px"
android:text="@string/act1"
>
</TextView>
<EditText
android:id="@+id/ev1"
android:textSize="24sp"
android:layout_width="186px"
android:layout_height="wrap_content"
android:layout_x="70px"
android:layout_y="65px"
android:numeric="decimal"
>
</EditText>
<Button
android:id="@+id/button1"
android:layout_width="118px"
android:layout_height="wrap_content"
android:layout_x="100px"
android:layout_y="130px"
android:text="Go to Activity1"
android:numeric="decimal"
>
</Button>
</AbsoluteLayout>
main1.xml
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/text2"
android:textSize="24sp"
android:layout_width="186px"
android:layout_height="29px"
android:layout_x="70px"
android:layout_y="32px"
android:text="@string/act2"
>
</TextView>
<Button
android:id="@+id/button2"
android:layout_width="118px"
android:layout_height="wrap_content"
android:layout_x="100px"
android:layout_y="82px"
android:text="Go to Activity2"
>
</Button>
<Button
android:id="@+id/button3"
android:layout_width="118px"
android:layout_height="wrap_content"
android:layout_x="100px"
android:layout_y="150px"
android:text="我弹出对话框"
>
</Button>
</AbsoluteLayout>
strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="act1">Hi,I am Activity1</string>
<string name="act2">Hi,I am Activity2</string>
<string name="app_name">Ch3_5</string>
<string name="app_name1">Ch3_5_1</string>
<string name="app_title">Activity</string>
<string name="Dialog_title">提示</string>
<string name="Dialog_content">我是一个对话框</string>
<string name="Dialog_btn">确定</string>
</resources>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.oyzz.ch3_5"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_title">
<activity android:name=".Ch3_5"
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="Ch3_5_1" android:label="@string/app_name1"></activity>
</application>
<uses-sdk android:minSdkVersion="5" />
</manifest>
学习体会:
1.Activity之间的跳转:
//实现跳转到另外一个Activity
Intent it = new Intent();
it.setClass(Ch3_5.this, Ch3_5_1.class);//参数是第一个是自己的,第二个是将要跳转到的地方
startActivity(it);//开启另外一个Activity
Ch3_5.this.finish();//释放自己
2.Activity之间的传值:主要是使用了一个Bundle对象,然后保存到Intent里面
传递值:
Intent it = new Intent();
it.setClass(Ch3_5.this, Ch3_5_1.class);
Bundle b = new Bundle();
b.putString("name", name);//保存值,有好多的方法
it.putExtras(b);//讲Bundle对象放入Intent里面
startActivity(it);//开启另外一个Activity
Ch3_5.this.finish();//释放自己
取出值:
//先得到Bunle对象
Intent intent =this.getIntent();
Bundle bunde = intent.getExtras();
//讲里面的值取出
String name = bunde.getString("name");
3.返回到上一个Activity并且将值保存下来
1.
传递值:
Intent it = new Intent();
it.setClass(Ch3_5.this, Ch3_5_1.class);
Bundle b = new Bundle();
b.putString("name", name);//保存值,有好多的方法
it.putExtras(b);//讲Bundle对象放入Intent里面
startActivityForResult(it, 0);//开启另外一个Activity
Ch3_5.this.finish();//释放自己
2.
重写 protected void onActivityResult(int requestCode, int resultCode, Intent data) {}方法
3.到返回事件里调用
//先得到Bunle对象
Intent intent =this.getIntent();
Bundle bunde = intent.getExtras();
Ch3_5_1.this.setResult(RESULT_OK,intent);//返回
Ch3_5_1.this.finish();//释放当前Activity
4.弹出对话框:
new AlertDialog.Builder(Ch3_5_1.this)//到当前窗口显示
.setTitle(R.string.Dialog_title)//标题
.setMessage(R.string.Dialog_content)//内容
.setPositiveButton(R.string.Dialog_btn, new DialogInterface.OnClickListener() {//添加按钮,和实现事件
public void onClick(DialogInterface dialog, int which) {
TextView tv = (TextView)findViewById(R.id.text2);
tv.setText("点击成功.......");
}
}).show();
5.关于AndroidManifest.xml文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.oyzz.ch3_5"//包名
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon"//程序的图标 android:label="@string/app_title"//程序的标题>
<activity android:name=".Ch3_5"
android:label="@string/app_name">//第一个activity
<intent-filter>//说明是主activity,第一个启动的activity 这两个参数必须指出
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="Ch3_5_1" android:label="@string/app_name1"></activity>//第二个activity
</application>
<uses-sdk android:minSdkVersion="5" />
</manifest>