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

Android 中Activity 之間傳遞參數(shù)

 更新時(shí)間:2017年10月27日 14:32:04   作者:一人一城_Qian  
這篇文章主要介紹了Android 中Activity 之間傳遞參數(shù)的相關(guān)資料,希望通過(guò)本文能幫助到大家,讓大家實(shí)現(xiàn)這樣的方法,需要的朋友可以參考下

Android 中Activity 之間傳遞參數(shù)

1.傳遞簡(jiǎn)單數(shù)據(jù)

在A Activity中

findViewById(R.id.startBActicityBtn).setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        Intent i = new Intent(MainActivity.this,TheActivity.class);
//        對(duì)基礎(chǔ)的數(shù)據(jù)類型進(jìn)行傳遞
       i.putExtra("data","我是國(guó)人");
       startActivity(i);
      }
    });

在B Activity中接受數(shù)據(jù)

  tv =(TextView)findViewById(R.id.TheTextView);
    Intent i = getIntent();
    tv.setText(i.getStringExtra("data"));

這種傳值就是傳遞基本的數(shù)據(jù)類型

2.傳遞數(shù)據(jù) 包Bundle

在A Activity中

 findViewById(R.id.startBActicityBtn).setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        Intent i = new Intent(MainActivity.this,TheActivity.class);
        Bundle bundle = new Bundle();
        bundle.putString("name","qll");
        bundle.putInt("age",3);

//        i.putExtras(bundle); 
//        另種傳遞方式
        i.putExtra("data",bundle);
        startActivity(i);
      }
    });
  }

在B Activity中接受數(shù)據(jù)     

 tv =(TextView)findViewById(R.id.TheTextView);
    editText = (EditText) findViewById(R.id.editText3);
    Intent i = getIntent();
//    Bundle date = i.getExtras();
//     接受方式不同
    Bundle date = i.getBundleExtra("data");
    tv.setText(String.format("name=%s,age=%d",date.getString("name"),date.getInt("age")));

這總傳遞方式類似iOS中傳遞字典數(shù)據(jù)類型過(guò)來(lái) 。

3.傳遞值對(duì)象

自定義一個(gè)User類,傳遞自定義類需要對(duì)類進(jìn)行序列化

用Serializable進(jìn)行序列化

這種方法只需要類實(shí)現(xiàn)Serializable接口就可以了

User 類

import java.io.Serializable;

public class User implements Serializable{

  private String name;
  private int age;

  public int getAge(){
    return age;
  }

  public void setAge(int age){
    this.age = age;
  }

  public String getName(){
    return name;
  }

  public void setName(String name){
    this.name = name;
  }

  public User(String name,int age){
    this.name = name;
    this.age = age;
  }

在A Activity中   

  findViewById(R.id.startBActicityBtn).setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        Intent i = new Intent(MainActivity.this,TheActivity.class);
        i.putExtra("user",new User("qll",24));
        startActivity(i);
      }
    });
  }

在B Activity中

 tv =(TextView)findViewById(R.id.TheTextView);
 User user = (User)i.getSerializableExtra("user");
 tv.setText(String.format("user info(name=%s,age=%d)",user.getName(),user.getAge()));

用Parcelable實(shí)現(xiàn)

同樣的需要實(shí)現(xiàn)Parcelable接口

User 類

package com.example.wyhaiapple.transferdata1;

import android.os.Parcel;
import android.os.Parcelable;
import android.text.ParcelableSpan;

public class User implements Parcelable{
  private String name;
  private int age;

  public int getAge(){
    return age;
  }

  public void setAge(int age){
    this.age = age;
  }

  public String getName(){
    return name;
  }

  public void setName(String name){
    this.name = name;
  }

  public User(String name,int age){
    this.name = name;
    this.age = age;
  }

  @Override
  public int describeContents() {
    return 0;
  }

  @Override
  public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(getName());
    dest.writeInt(getAge());
  }


  public static final Creator<User> CREATOR = new Creator<User>() {
    @Override
    public User createFromParcel(Parcel source) {
      return new User(source.readString(),source.readInt());
    }

    @Override
    public User[] newArray(int size) {
      return new User[size];
    }
  };
}

在A Activity中 與上面的相同

在B Activity中

 tv =(TextView)findViewById(R.id.TheTextView);
 User user = (User)i.getParcelableExtra("user");
 tv.setText(String.format("user info(name=%s,age=%d)",user.getName(),user.getAge()));

4.獲取 Activity 的返回參數(shù)

在B Activity中

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_the);
    tv =(TextView)findViewById(R.id.TheTextView);
    editText = (EditText) findViewById(R.id.editText3);

    findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        Intent i = new Intent();
        i.putExtra("data",editText.getText().toString());
        setResult(1,i);
        finish();
      }
    });
  }
}

在A Activity中

startActivityForResult(i,0);

  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    textView.setText("返回的值:"+data.getStringExtra("data"));
  }

如有疑問(wèn)請(qǐng)留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!

相關(guān)文章

  • Android Studio3安裝圖文教程

    Android Studio3安裝圖文教程

    這篇文章主要為大家詳細(xì)介紹了Android Studio3安裝圖文教程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-07-07
  • RxJava 1升級(jí)到RxJava 2過(guò)程中踩過(guò)的一些“坑”

    RxJava 1升級(jí)到RxJava 2過(guò)程中踩過(guò)的一些“坑”

    RxJava2相比RxJava1,它的改動(dòng)還是很大的,那么下面這篇文章主要給大家總結(jié)了在RxJava 1升級(jí)到RxJava 2過(guò)程中踩過(guò)的一些“坑”,文中介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下來(lái)要一起看看吧。
    2017-05-05
  • Android 混合動(dòng)畫詳解及實(shí)現(xiàn)代碼

    Android 混合動(dòng)畫詳解及實(shí)現(xiàn)代碼

    這篇文章主要介紹了Android 混合動(dòng)畫詳解及實(shí)現(xiàn)代碼的相關(guān)資料,簡(jiǎn)單的一種動(dòng)畫(如旋轉(zhuǎn)、縮放、漸變、位移等)有時(shí)候并不能滿足我們項(xiàng)目的要求,這時(shí)候就需要運(yùn)用到混合動(dòng)畫,需要的朋友可以參考下
    2016-11-11
  • Android仿支付寶中余額寶的數(shù)字動(dòng)畫效果

    Android仿支付寶中余額寶的數(shù)字動(dòng)畫效果

    最近因?yàn)楣ぷ餍枰叻掠囝~寶數(shù)字動(dòng)畫效果,達(dá)到炫酷的數(shù)字動(dòng)畫效果,所以寫出了分享給大家,有需要的朋友可以直接拿來(lái)用,下面一起來(lái)看看。
    2016-08-08
  • Android自定義View控件實(shí)現(xiàn)刷新效果

    Android自定義View控件實(shí)現(xiàn)刷新效果

    這篇文章主要介紹了Android自定義View控件實(shí)現(xiàn)刷新效果的相關(guān)資料,非常不錯(cuò)具有參考借鑒價(jià)值,需要的朋友可以參考下
    2016-11-11
  • Android主線程和子線程區(qū)別詳解

    Android主線程和子線程區(qū)別詳解

    這篇文章主要為大家詳細(xì)介紹了Android主線程和子線程的區(qū)別,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-10-10
  • Android下2d物理引擎Box2d用法簡(jiǎn)單實(shí)例

    Android下2d物理引擎Box2d用法簡(jiǎn)單實(shí)例

    這篇文章主要介紹了Android下2d物理引擎Box2d用法,實(shí)例分析了在Android平臺(tái)上使用Box2d的基本技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-07-07
  • Flutter實(shí)現(xiàn)增強(qiáng)版的頁(yè)面懸浮按鈕的示例代碼

    Flutter實(shí)現(xiàn)增強(qiáng)版的頁(yè)面懸浮按鈕的示例代碼

    Flutter?自帶的?FloatingActionButton?為我們提供了一個(gè)懸浮在頂部的按鈕,這個(gè)按鈕始終在最頂層,因此可以做一些快捷的操作。本文就來(lái)和大家詳細(xì)聊聊
    2023-01-01
  • Android Studio 3.x安裝指南教程

    Android Studio 3.x安裝指南教程

    這篇文章主要為大家詳細(xì)介紹了Android Studio 3.x安裝指南教程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-07-07
  • Android仿微信和QQ多圖合并框架(類似群頭像)的實(shí)現(xiàn)方法

    Android仿微信和QQ多圖合并框架(類似群頭像)的實(shí)現(xiàn)方法

    這篇文章主要給大家介紹了關(guān)于Android仿微信和QQ多圖合并框架的相關(guān)資料,其實(shí)就是我們平時(shí)所見(jiàn)的群聊頭像,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)各位Android開(kāi)發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-12-12

最新評(píng)論