Android使用Intent實現(xiàn)頁面跳轉(zhuǎn)
什么是Intent
Intent可以理解為信使(意圖)
由Intent來協(xié)助完成Android各個組件之間的通訊
Intent實現(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);
/*
* 通過點擊bt1實現(xiàn)頁面之間的跳轉(zhuǎn)
* 1.startActivity的方式來實現(xiàn)
* */
bt1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
/*
* 第一個參數(shù):上下文對象.this 匿名內(nèi)部類中不能直接.this
* 第二個參數(shù):目標文件(注意是.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ù)是請求的一個標識
* public void startActivityForResult(Intent intent, int requestCode)
*/
startActivityForResult(intent,1);
}
});
}
/*
* 通過startActivityForResult方法跳轉(zhuǎn),接收返回數(shù)據(jù)的方法
* requestCode:請求的標識
* resultCode:第二個頁面返回的標志
* 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ù)
* 回傳第一個頁面實際上是一個Intent對象
* */
bt1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent data = new Intent();//使用空參就行,因為我們不跳轉(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)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
- Android使用Intent隱式實現(xiàn)頁面跳轉(zhuǎn)
- Android Intent實現(xiàn)頁面跳轉(zhuǎn)的兩種方法
- Android Intent實現(xiàn)頁面跳轉(zhuǎn)的方法示例
- Android 實現(xiàn)頁面跳轉(zhuǎn)
- Android使用Circular Reveal動畫讓頁面跳轉(zhuǎn)更炫酷
- Android編程中Intent實現(xiàn)頁面跳轉(zhuǎn)功能詳解
- Android Activity中使用Intent實現(xiàn)頁面跳轉(zhuǎn)與參數(shù)傳遞的方法
- Android使用Intent顯示實現(xiàn)頁面跳轉(zhuǎn)
相關(guān)文章
android實現(xiàn)ViewPager的Indicator的實例代碼
本篇文章主要介紹了android實現(xiàn)ViewPager的Indicator的實例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-02-02
Android Jetpack庫剖析之Lifecycle組件篇
本章也是帶來了Jetpack中我認為最重要的架構(gòu)組件Lifecycle的原理探索,至于為什么覺得它是最重要是因為像ViewModel,LiveData這些組件也依賴于Lifecycle來感知宿主的生命周期,那么本章我們帶著幾個問題來探索一下這個組件2022-07-07
Android BottomSheet效果的兩種實現(xiàn)方式
這篇文章主要介紹了Android BottomSheet效果的兩種實現(xiàn)方式,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-08-08
Android自定義View實現(xiàn)旋轉(zhuǎn)的圓形圖片
這篇文章主要為大家詳細介紹了Android自定義View實現(xiàn)旋轉(zhuǎn)的圓形圖片,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-01-01
Android應用開發(fā)之簡易、大氣音樂播放器實現(xiàn)專輯倒影效果
這篇文章主要介紹了Android應用開發(fā)之簡單、大氣音樂播放器實現(xiàn)專輯倒影效果,對android音樂播放器感興趣的朋友可以參考下2015-10-10

