Android使用Intent實(shí)現(xiàn)頁面跳轉(zhuǎn)
什么是Intent
Intent可以理解為信使(意圖)
由Intent來協(xié)助完成Android各個(gè)組件之間的通訊
Intent實(shí)現(xiàn)頁面之間的跳轉(zhuǎn)
1>startActivity(intent)
2>startActivityForResult(intent,requestCode)
onActivityResult(int requestCode,int resultCode,Intent data)
setResult(resultCode,data)
第二種啟動(dòng)方式可以返回結(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) { /* * 第一個(gè)參數(shù):上下文對(duì)象.this 匿名內(nèi)部類中不能直接.this * 第二個(gè)參數(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); /* * 第一個(gè)參數(shù)是Intent對(duì)象 * 第二個(gè)參數(shù)是請(qǐng)求的一個(gè)標(biāo)識(shí) * public void startActivityForResult(Intent intent, int requestCode) */ startActivityForResult(intent,1); } }); } /* * 通過startActivityForResult方法跳轉(zhuǎn),接收返回?cái)?shù)據(jù)的方法 * requestCode:請(qǐng)求的標(biāo)識(shí) * resultCode:第二個(gè)頁面返回的標(biāo)志 * data:第二個(gè)頁面回傳的數(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); /* * 第二個(gè)頁面什么時(shí)候給第一個(gè)頁面回傳數(shù)據(jù) * 回傳第一個(gè)頁面實(shí)際上是一個(gè)Intent對(duì)象 * */ 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="第一種啟動(dòng)方式" /> <Button android:id="@+id/button_second" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="第二種啟動(dòng)方式" /> <TextView android:id="@+id/textView" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="把第二個(gè)頁面回傳的數(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)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android使用Intent隱式實(shí)現(xiàn)頁面跳轉(zhuǎn)
- Android Intent實(shí)現(xiàn)頁面跳轉(zhuǎn)的兩種方法
- Android Intent實(shí)現(xiàn)頁面跳轉(zhuǎn)的方法示例
- Android 實(shí)現(xiàn)頁面跳轉(zhuǎn)
- Android使用Circular Reveal動(dòng)畫讓頁面跳轉(zhuǎn)更炫酷
- Android編程中Intent實(shí)現(xiàn)頁面跳轉(zhuǎn)功能詳解
- Android Activity中使用Intent實(shí)現(xiàn)頁面跳轉(zhuǎn)與參數(shù)傳遞的方法
- Android使用Intent顯示實(shí)現(xiàn)頁面跳轉(zhuǎn)
相關(guān)文章
Android應(yīng)用開發(fā)SharedPreferences存儲(chǔ)數(shù)據(jù)的使用方法
SharedPreferences是Android中最容易理解的數(shù)據(jù)存儲(chǔ)技術(shù),實(shí)際上SharedPreferences處理的就是一個(gè)key-value(鍵值對(duì))SharedPreferences常用來存儲(chǔ)一些輕量級(jí)的數(shù)據(jù)2012-11-11Android實(shí)現(xiàn)關(guān)機(jī)與重啟的幾種方式(推薦)
這篇文章主要介紹了Android實(shí)現(xiàn)關(guān)機(jī)與重啟的幾種方式(推薦)的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-07-07Android自定義實(shí)現(xiàn)循環(huán)滾輪控件WheelView
滾輪布局WheelView大家經(jīng)常使用,比如在選擇生日的時(shí)候,風(fēng)格類似系統(tǒng)提供的DatePickerDialog,這篇文章主要為大家詳細(xì)介紹了Android自定義實(shí)現(xiàn)循環(huán)滾輪控件WheelView,感興趣的小伙伴們可以參考一下2016-07-07Android 一個(gè)日歷控件的實(shí)現(xiàn)代碼
本篇文章主要介紹了Android 一個(gè)日歷控件的實(shí)現(xiàn)代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-05-05解決Android 沉浸式狀態(tài)欄和華為虛擬按鍵沖突問題
對(duì)于現(xiàn)在的 App 來說,布局頁面基本都會(huì)用到沉浸式狀態(tài)欄,單純的沉浸式狀態(tài)欄很容易解決,但是在華為手機(jī)上存在一個(gè)底部虛擬按鍵的問題,會(huì)導(dǎo)致頁面底部和頂部出現(xiàn)很大的問題,下面通過本文給大家分享Android 沉浸式狀態(tài)欄和華為虛擬按鍵沖突問題,一起看看吧2017-07-07listview的上滑下滑監(jiān)聽,上下滑監(jiān)聽隱藏頂部選項(xiàng)欄的實(shí)例
下面小編就為大家分享一篇listview的上滑下滑監(jiān)聽,上下滑監(jiān)聽隱藏頂部選項(xiàng)欄的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-01-01簡析Android五大布局(LinearLayout、FrameLayout、RelativeLayout等)
這篇文章主要為大家簡單分析了Android五大布局,內(nèi)容有LinearLayout、FrameLayout、RelativeLayout、AbsoluteLayout和TableLayout的相關(guān)資料,感興趣的小伙伴們可以參考一下2016-06-06Android 8.0系統(tǒng)中應(yīng)用圖標(biāo)的適配微技巧
這篇文章主要介紹了Android 8.0系統(tǒng)中應(yīng)用圖標(biāo)的適配微技巧 ,需要的朋友可以參考下2018-04-04