国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

Android使用Intent實(shí)現(xiàn)頁面跳轉(zhuǎn)

2019-10-22 18:26:01
字體:
供稿:網(wǎng)友

什么是Intent

Intent可以理解為信使(意圖)
由Intent來協(xié)助完成Android各個組件之間的通訊

Intent實(shí)現(xiàn)頁面之間的跳轉(zhuǎn)

1>startActivity(intent)
2>startActivityForResult(intent,requestCode)
onActivityResult(int requestCode,int resultCode,Intent data)
setResult(resultCode,data)
第二種啟動方式可以返回結(jié)果

代碼

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);    /*    * 通過點(diǎn)擊bt1實(shí)現(xiàn)頁面之間的跳轉(zhuǎn)    * 1.startActivity的方式來實(shí)現(xiàn)    * */    bt1.setOnClickListener(new View.OnClickListener() {      public void onClick(View view) {        /*        * 第一個參數(shù):上下文對象.this 匿名內(nèi)部類中不能直接.this        * 第二個參數(shù):目標(biāo)文件(注意是.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);        /*         * 第一個參數(shù)是Intent對象         * 第二個參數(shù)是請求的一個標(biāo)識         * public void startActivityForResult(Intent intent, int requestCode)         */        startActivityForResult(intent,1);      }    });  }  /*   * 通過startActivityForResult方法跳轉(zhuǎn),接收返回數(shù)據(jù)的方法   * requestCode:請求的標(biāo)識   * resultCode:第二個頁面返回的標(biāo)志   * data:第二個頁面回傳的數(shù)據(jù)   */  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="回傳數(shù)據(jù)";  protected void onCreate(@Nullable Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.second_layout);    bt1 = (Button) findViewById(R.id.button);    /*    * 第二個頁面什么時候給第一個頁面回傳數(shù)據(jù)    * 回傳第一個頁面實(shí)際上是一個Intent對象    * */    bt1.setOnClickListener(new View.OnClickListener() {      public void onClick(View view) {        Intent data = new Intent();//使用空參就行,因?yàn)槲覀儾惶D(zhuǎn)        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="把第二個頁面回傳的數(shù)據(jù)顯示出來" /></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>

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持VEVB武林網(wǎng)。


注:相關(guān)教程知識閱讀請移步到Android開發(fā)頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 措勤县| 锦屏县| 利川市| 裕民县| 宜良县| 湛江市| 普宁市| 南郑县| 西畴县| 四子王旗| 安阳县| 周口市| 枝江市| 永安市| 云龙县| 九江市| 定边县| 永靖县| 云和县| 白玉县| 安泽县| 蓝田县| 邵东县| 房产| 双峰县| 旺苍县| 临猗县| 武冈市| 江都市| 玉山县| 双辽市| 中宁县| 鹤山市| 丁青县| 通榆县| 庄河市| 汕尾市| 江西省| 阿拉善左旗| 阿拉善盟| 西藏|