首页 > 系统 > Android > 正文

Android使用Intent实现页面跳转

2019-12-12 01:55:06
字体:
来源:转载
供稿:网友

什么是Intent

Intent可以理解为信使(意图)
由Intent来协助完成Android各个组件之间的通讯

Intent实现页面之间的跳转

1>startActivity(intent)
2>startActivityForResult(intent,requestCode)
onActivityResult(int requestCode,int resultCode,Intent data)
setResult(resultCode,data)
第二种启动方式可以返回结果

代码

FirstActivity.java

public class FirstActivity extends AppCompatActivity {  private Button bt1;  private Button bt2;  private TextView tv;  private Context context=FirstActivity.this;  protected void onCreate(@Nullable Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.first_layout);    bt1 = (Button) findViewById(R.id.button_first);    bt2 = (Button) findViewById(R.id.button_second);    tv = (TextView) findViewById(R.id.textView);    /*    * 通过点击bt1实现页面之间的跳转    * 1.startActivity的方式来实现    * */    bt1.setOnClickListener(new View.OnClickListener() {      public void onClick(View view) {        /*        * 第一个参数:上下文对象.this 匿名内部类中不能直接.this        * 第二个参数:目标文件(注意是.class)        * Intent(Context packageContext, Class<?> cls)        * */        //Intent intent = new Intent(FirstActivity.this,SecondActivity.class);        Intent intent = new Intent(context,SecondActivity.class);        startActivity(intent);      }    });    /*    * 通过startActivityForResult    * */    bt2.setOnClickListener(new View.OnClickListener() {      public void onClick(View view) {        Intent intent = new Intent(context,SecondActivity.class);        /*         * 第一个参数是Intent对象         * 第二个参数是请求的一个标识         * public void startActivityForResult(Intent intent, int requestCode)         */        startActivityForResult(intent,1);      }    });  }  /*   * 通过startActivityForResult方法跳转,接收返回数据的方法   * requestCode:请求的标识   * resultCode:第二个页面返回的标志   * data:第二个页面回传的数据   */  protected void onActivityResult(int requestCode, int resultCode, Intent data) {    super.onActivityResult(requestCode, resultCode, data);    if(requestCode == 1 && resultCode == 3){      String value = data.getStringExtra("data");      tv.setText(value);    }  }}

SecondActivity.java

public class SecondActivity extends Activity {  private Button bt1;  private String value="回传数据";  protected void onCreate(@Nullable Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.second_layout);    bt1 = (Button) findViewById(R.id.button);    /*    * 第二个页面什么时候给第一个页面回传数据    * 回传第一个页面实际上是一个Intent对象    * */    bt1.setOnClickListener(new View.OnClickListener() {      public void onClick(View view) {        Intent data = new Intent();//使用空参就行,因为我们不跳转        data.putExtra("data",value);        setResult(3,data);        finish();//销毁本页面,也就是返回上一页面      }    });  }}

first_layout.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  android:orientation="vertical" android:layout_width="match_parent"  android:layout_height="match_parent">  <Button    android:id="@+id/button_first"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:text="第一种启动方式" />  <Button    android:id="@+id/button_second"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:text="第二种启动方式" />  <TextView    android:id="@+id/textView"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:text="把第二个页面回传的数据显示出来" /></LinearLayout>

second_layout.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <Button  android:id="@+id/button"  android:layout_width="match_parent"  android:layout_height="wrap_content"  android:text="Button" /></LinearLayout>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持武林网。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表