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

首頁 > 系統 > Android > 正文

Android Intent實現頁面跳轉的兩種方法

2019-10-22 18:12:11
字體:
來源:轉載
供稿:網友

本文實例為大家分享了Intent實現頁面跳轉的兩種的方法,供大家參考,具體內容如下

Android,Intent,頁面跳轉

 

下圖中兩個不同的方法就是兩種頁面之間跳轉的情況

1).跳轉不返回數據

2).跳轉返回數據

Android,Intent,頁面跳轉

實例:

第一種啟動方式(跳轉不返回數據)

第二種啟動方式(跳轉返回數據)

先看第一種:

Android,Intent,頁面跳轉Android,Intent,頁面跳轉

點擊第一種啟動方式按鈕會出現右邊的圖,然后再點擊Button按鈕返回左邊的界面,TextView中的內容沒變。

再看第二種啟動方式

不同的是,點擊Button按鈕返回左邊的界面,TextView中的內容變成了你好。

Android,Intent,頁面跳轉Android,Intent,頁面跳轉Android,Intent,頁面跳轉

下面是所有代碼

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="吧第二個頁面回傳的數據顯示出來" /></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實現界面之間的跳轉       1.通過startActivity的方式來實現       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) {         /*         第一個參數:上下文對象this         第二個參數:目標文件          */         Intent intent = new Intent(firstactivity.this,Secondactivity.class);         startActivity(intent);        }     });      /*     2.通過startActivityForResult的方式來實現      */     //給bt2添加點擊事件     bt2.setOnClickListener(new View.OnClickListener() {       @Override       public void onClick(View view) {         Intent intent = new Intent(firstactivity.this,Secondactivity.class);          /*         第一個參數:Intent對象         第二個參數:請求的一個標識          */         startActivityForResult(intent,1);       }     });   }    /*   通過startActivityForResult的方式接受返回數據的方法   requestCode:請求的標志,給每個頁面發出請求的標志不一樣,這樣以后通過這個標志接受不同的數據   resultCode:這個參數是setResult(int resultCode,Intent data)方法傳來的,這個方法用在傳來數據的那個頁面    */   @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="你好";//想返回的內容  @Override  protected void onCreate( Bundle savedInstanceState) {   super.onCreate(savedInstanceState);   setContentView(R.layout.sactivity);    /*   第二個頁面什么時候給第一個頁面回傳數據   回傳到第一個頁面的實際上是一個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是返回的內容     data.putExtra("data",content);     //resultCode是返回碼,用來確定是哪個頁面傳來的數據,這里設置返回碼是2     //這個頁面傳來數據,要用到下面這個方法setResult(int resultCode,Intent data)     setResult(2,data);     //結束當前頁面     finish();    }   });  } } 

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 新郑市| 永泰县| 信丰县| 新兴县| 太康县| 青冈县| 平凉市| 黔江区| 昆明市| 曲阳县| 临夏市| 泰安市| 公安县| 武平县| 阿荣旗| 蓝田县| 屯门区| 塔河县| 北碚区| 青浦区| 仲巴县| 广饶县| 襄城县| 东宁县| 淳化县| 洛宁县| 卫辉市| 永善县| 秦安县| 贵阳市| 彰武县| 加查县| 翁牛特旗| 宁明县| 安平县| 泸定县| 龙山县| 讷河市| 哈巴河县| 昌吉市| 泰宁县|