溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

怎么在Android中利用Intent實現(xiàn)一個頁面跳轉(zhuǎn)功能

發(fā)布時間:2021-02-22 17:33:14 來源:億速云 閱讀:152 作者:戴恩恩 欄目:移動開發(fā)

這篇文章主要介紹了怎么在Android中利用Intent實現(xiàn)一個頁面跳轉(zhuǎn)功能,此處給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考價值,需要的朋友可以參考下:

怎么在Android中利用Intent實現(xiàn)一個頁面跳轉(zhuǎn)功能

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

1).跳轉(zhuǎn)不返回數(shù)據(jù)

2).跳轉(zhuǎn)返回數(shù)據(jù)

怎么在Android中利用Intent實現(xiàn)一個頁面跳轉(zhuǎn)功能

實例:

第一種啟動方式(跳轉(zhuǎn)不返回數(shù)據(jù))

第二種啟動方式(跳轉(zhuǎn)返回數(shù)據(jù))

先看第一種:

怎么在Android中利用Intent實現(xiàn)一個頁面跳轉(zhuǎn)功能怎么在Android中利用Intent實現(xiàn)一個頁面跳轉(zhuǎn)功能

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

再看第二種啟動方式

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

怎么在Android中利用Intent實現(xiàn)一個頁面跳轉(zhuǎn)功能怎么在Android中利用Intent實現(xiàn)一個頁面跳轉(zhuǎn)功能怎么在Android中利用Intent實現(xiàn)一個頁面跳轉(zhuǎn)功能

下面是所有代碼

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){//當(dāng)請求碼是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相當(dāng)于一個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é)束當(dāng)前頁面 
    finish(); 
   } 
  }); 
 } 
}

到此這篇關(guān)于怎么在Android中利用Intent實現(xiàn)一個頁面跳轉(zhuǎn)功能的文章就介紹到這了,更多相關(guān)的內(nèi)容請搜索億速云以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持億速云!

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI