這篇文章主要介紹了怎么在Android中利用Intent實現(xiàn)一個頁面跳轉(zhuǎn)功能,此處給大家介紹的非常詳細,對大家的學習或工作具有一定的參考價值,需要的朋友可以參考下:
公司專注于為企業(yè)提供網(wǎng)站設(shè)計、成都網(wǎng)站設(shè)計、微信公眾號開發(fā)、商城網(wǎng)站制作,成都微信小程序,軟件定制網(wǎng)站建設(shè)等一站式互聯(lián)網(wǎng)企業(yè)服務(wù)。憑借多年豐富的經(jīng)驗,我們會仔細了解各客戶的需求而做出多方面的分析、設(shè)計、整合,為客戶設(shè)計出具風格及創(chuàng)意性的商業(yè)解決方案,創(chuàng)新互聯(lián)公司更提供一系列網(wǎng)站制作和網(wǎng)站推廣的服務(wù)。
下圖中兩個不同的方法就是兩種頁面之間跳轉(zhuǎn)的情況
1).跳轉(zhuǎn)不返回數(shù)據(jù)
2).跳轉(zhuǎn)返回數(shù)據(jù)
實例:
第一種啟動方式(跳轉(zhuǎn)不返回數(shù)據(jù))
第二種啟動方式(跳轉(zhuǎn)返回數(shù)據(jù))
先看第一種:
點擊第一種啟動方式按鈕會出現(xiàn)右邊的圖,然后再點擊Button按鈕返回左邊的界面,TextView中的內(nèi)容沒變。
再看第二種啟動方式
不同的是,點擊Button按鈕返回左邊的界面,TextView中的內(nèi)容變成了你好。
下面是所有代碼
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.lenovo.intent"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> </activity> <activity android:name="com.example.lenovo.intent.firstactivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name="com.example.lenovo.intent.Secondactivity"> </activity> </application> </manifest>
factivity
<?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" > <Button android:id="@+id/bt1__first" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="第一種啟動方式" /> <Button android:id="@+id/bt2__second" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="第二種啟動方式" /> <TextView android:id="@+id/textView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="吧第二個頁面回傳的數(shù)據(jù)顯示出來" /> </LinearLayout>
sactivity
<?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"> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="Button" /> </LinearLayout>
firstactivity.java
package com.example.lenovo.intent; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.TextView; /** * Created by lenovo on 2018/2/27. */ public class firstactivity extends Activity { private Button bt1; private Button bt2; private TextView tv; @Override protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.factivity); /* 通過點擊bt1實現(xiàn)界面之間的跳轉(zhuǎn) 1.通過startActivity的方式來實現(xiàn) 1>初始Intent(意圖) */ bt1=(Button) findViewById(R.id.bt1__first); bt2=(Button)findViewById(R.id.bt2__second); tv=(TextView) findViewById(R.id.textView1); //給bt1添加點擊事件 bt1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { /* 第一個參數(shù):上下文對象this 第二個參數(shù):目標文件 */ Intent intent = new Intent(firstactivity.this,Secondactivity.class); startActivity(intent); } }); /* 2.通過startActivityForResult的方式來實現(xiàn) */ //給bt2添加點擊事件 bt2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(firstactivity.this,Secondactivity.class); /* 第一個參數(shù):Intent對象 第二個參數(shù):請求的一個標識 */ startActivityForResult(intent,1); } }); } /* 通過startActivityForResult的方式接受返回數(shù)據(jù)的方法 requestCode:請求的標志,給每個頁面發(fā)出請求的標志不一樣,這樣以后通過這個標志接受不同的數(shù)據(jù) resultCode:這個參數(shù)是setResult(int resultCode,Intent data)方法傳來的,這個方法用在傳來數(shù)據(jù)的那個頁面 */ @Override protected void onActivityResult(int requestCode,int resultCode ,Intent data){ super.onActivityResult(requestCode,resultCode,data); if(requestCode==1&&resultCode==2){//當請求碼是1&&返回碼是2進行下面操作 String content=data.getStringExtra("data"); tv.setText(content); } } }
Secondactivity.java
package com.example.lenovo.intent; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; /** * Created by lenovo on 2018/2/27. */ public class Secondactivity extends Activity { private Button bt; String content="你好";//想返回的內(nèi)容 @Override protected void onCreate( Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.sactivity); /* 第二個頁面什么時候給第一個頁面回傳數(shù)據(jù) 回傳到第一個頁面的實際上是一個Intent對象 */ bt=(Button) findViewById(R.id.button); bt.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent data = new Intent(); //name相當于一個key,content是返回的內(nèi)容 data.putExtra("data",content); //resultCode是返回碼,用來確定是哪個頁面?zhèn)鱽淼臄?shù)據(jù),這里設(shè)置返回碼是2 //這個頁面?zhèn)鱽頂?shù)據(jù),要用到下面這個方法setResult(int resultCode,Intent data) setResult(2,data); //結(jié)束當前頁面 finish(); } }); } }
到此這篇關(guān)于怎么在Android中利用Intent實現(xiàn)一個頁面跳轉(zhuǎn)功能的文章就介紹到這了,更多相關(guān)的內(nèi)容請搜索創(chuàng)新互聯(lián)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持創(chuàng)新互聯(lián)!
網(wǎng)站題目:怎么在Android中利用Intent實現(xiàn)一個頁面跳轉(zhuǎn)功能
轉(zhuǎn)載注明:http://aaarwkj.com/article16/jjhodg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供建站公司、網(wǎng)站營銷、網(wǎng)站改版、網(wǎng)站排名、品牌網(wǎng)站設(shè)計、微信公眾號
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)