什么是Intent
Intent可以理解为信使(意图)
由Intent来协助完成Android各个组件之间的通讯
Intent实现页面逐渐的跳转
1.startActivity(inetnt)
2.startActivityForResult(intent, requestCode);
onAcitivtyResult(int requestCode, int resultCode, Intent data)
setResult(resultCode, data);
先创建两个xml文件firstactivity.xml和secondactivity.xml。
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="通过Intent实现页面之间的跳转 通过按钮1实现无返回结果的页面跳转 通过按钮2实现有返回结果的页面跳转" /> <Button android:id="@+id/button1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="按钮1" /> <Button android:id="@+id/button2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="按钮2" /> <TextView android:id="@+id/textView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="页面跳转后的结果将会显示在这里" /> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="点击返回第一个Activity" /> <Button android:id="@+id/button1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Button" /> </LinearLayout>
在AndroidManifest.xml中注册这两个xml,并设置firstactivity.xml是启动的时候加载的xml。
<activity android:name="com.example.intentdemo.FirstActivity" 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="com.example.intentdemo.SecondActivity" android:label="@string/app_name" > </activity>
FirstActivity.java的buttton1用于通过startActivity()方法实现跳转。
button1.setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(FirstActivity.this, SecondActivity.class); startActivity(intent); } });
FirstActivity.java的buttton2用于通过startActivityWithResult()方法实现跳转。
button2.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { Intent intent = new Intent(FirstActivity.this, SecondActivity.class); int requestCode = 1; // 自己取的 startActivityForResult(intent, requestCode); } });
第二个Activity返回结果并通过finish()销毁自己。
button.setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { int resultCode = 2; Intent data = new Intent(); data.putExtra("data", "这是第二个Activity返回的结果"); setResult(resultCode, data); finish(); } }); }
第一个Activity通过onActivityResult()方法得到返回的结果并显示在TextView中。
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == 1 && resultCode == 2) { textView.setText(data.getStringExtra("data")); } }
package com.example.intentdemo; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; public class FirstActivity extends Activity { private Button button1; private Button button2; private TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.firstactivity); textView = (TextView) findViewById(R.id.textView1); button1 = (Button) findViewById(R.id.button1); button1.setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(FirstActivity.this, SecondActivity.class); startActivity(intent); } }); button2 = (Button) findViewById(R.id.button2); button2.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { Intent intent = new Intent(FirstActivity.this, SecondActivity.class); int requestCode = 1; // 自己取的 startActivityForResult(intent, requestCode); } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == 1 && resultCode == 2) { textView.setText(data.getStringExtra("data")); } } }
package com.example.intentdemo; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class SecondActivity extends Activity { private Button button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.secondactivity); button = (Button) findViewById(R.id.button1); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { int resultCode = 2; Intent data = new Intent(); data.putExtra("data", "这是第二个Activity返回的结果"); setResult(resultCode, data); finish(); } }); } }
效果: