欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android Intent實現(xiàn)頁面跳轉(zhuǎn)的兩種方法

 更新時間:2018年05月03日 11:35:33   作者:好雨天堂  
這篇文章主要介紹了Android Intent實現(xiàn)頁面跳轉(zhuǎn)的兩種方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了Intent實現(xiàn)頁面跳轉(zhuǎn)的兩種的方法,供大家參考,具體內(nèi)容如下

下圖中兩個不同的方法就是兩種頁面之間跳轉(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ù):目標(biāo)文件 
         */ 
        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ù):請求的一個標(biāo)識 
         */ 
        startActivityForResult(intent,1); 
      } 
    }); 
  } 
 
  /* 
  通過startActivityForResult的方式接受返回數(shù)據(jù)的方法 
  requestCode:請求的標(biāo)志,給每個頁面發(fā)出請求的標(biāo)志不一樣,這樣以后通過這個標(biāo)志接受不同的數(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進(jìn)行下面操作 
      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(); 
   } 
  }); 
 } 
} 

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android自定義圓形進(jìn)度條效果

    Android自定義圓形進(jìn)度條效果

    這篇文章主要為大家詳細(xì)介紹了Android自定義圓形進(jìn)度條效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-04-04
  • Android處理視圖圓角和色彩的工具類

    Android處理視圖圓角和色彩的工具類

    這篇文章主要為大家詳細(xì)介紹了Android處理視圖圓角和色彩的工具類,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-07-07
  • android 7自定義相機(jī)預(yù)覽及拍照功能

    android 7自定義相機(jī)預(yù)覽及拍照功能

    這篇文章主要為大家詳細(xì)介紹了android 7自定義相機(jī)預(yù)覽及拍照功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-05-05
  • Android應(yīng)用程序窗口(Activity)窗口對象(Window)創(chuàng)建指南

    Android應(yīng)用程序窗口(Activity)窗口對象(Window)創(chuàng)建指南

    本文將詳細(xì)介紹Android應(yīng)用程序窗口(Activity)的窗口對象(Window)的創(chuàng)建過程,需要了解的朋友可以參考下
    2012-12-12
  • Android 中LayoutInflater.inflate()方法的介紹

    Android 中LayoutInflater.inflate()方法的介紹

    這篇文章主要介紹了Android 中LayoutInflater.inflate()方法的介紹的相關(guān)資料,希望通過本文大家能掌握這部分內(nèi)容,需要的朋友可以參考下
    2017-09-09
  • Android使用動畫設(shè)置ProgressBar進(jìn)度的方法

    Android使用動畫設(shè)置ProgressBar進(jìn)度的方法

    這篇文章主要為大家詳細(xì)介紹了Android使用動畫設(shè)置ProgressBar進(jìn)度的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-01-01
  • Android?Flutter實現(xiàn)任意拖動的控件

    Android?Flutter實現(xiàn)任意拖動的控件

    使用flutter開發(fā)是需要控件能拖動,比如畫板中的元素,或者工具條等,所以本文為大家準(zhǔn)備了Flutter實現(xiàn)任意拖動控件的示例代碼,希望對大家有所幫助
    2023-07-07
  • Android中ImageView用法實例分析

    Android中ImageView用法實例分析

    這篇文章主要介紹了Android中ImageView用法,結(jié)合實例形式較為詳細(xì)的分析了ImageView控件的功能,屬性設(shè)置與使用相關(guān)技巧,需要的朋友可以參考下
    2016-02-02
  • 完美解決EditText和ScrollView的滾動沖突(上)

    完美解決EditText和ScrollView的滾動沖突(上)

    這篇文章主要為大家詳細(xì)介紹了完美解決EditText和ScrollView滾動沖突的方法,感興趣的小伙伴們可以參考一下
    2016-06-06
  • 簡單談?wù)勎业腁ndroid屏幕適配之路

    簡單談?wù)勎业腁ndroid屏幕適配之路

    我相信Android碎片化問題是讓所有的Android開發(fā)者都比較頭疼的問題.尤其是屏幕適配這一塊兒.想要自己的app在不同的設(shè)備上面都有一個比較好的顯示效果.就必須做好相應(yīng)的屏幕適配.
    2017-11-11

最新評論