最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android學(xué)習(xí)小結(jié)之獲取被啟動(dòng)的Activity傳回的數(shù)據(jù)

 更新時(shí)間:2016年08月12日 14:18:48   作者:xch_yang  
這篇文章主要介紹了獲取被啟動(dòng)的Activity傳回的數(shù)據(jù),非常不錯(cuò),介紹的非常詳細(xì),需要的朋友可以參考下

當(dāng)前Activity:包含一個(gè)Button和一個(gè)TextView,用于啟動(dòng)另一個(gè)Activity和顯示傳回的數(shù)據(jù),這里重寫了onActivityResult()方法。

public class MainActivity extends AppCompatActivity {
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//找到TextView
textView=(TextView)findViewById(R.id.textView);
findViewById(R.id.btnSend).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent=new Intent(MainActivity.this,AnotherActivity.class);
startActivityForResult(intent,0);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
textView.setText("另外一個(gè)Activity傳回來(lái)的數(shù)據(jù)是:"+data.getStringExtra("data"));
}
}

XML文件:activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.example.androidtest.MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="啟動(dòng)另一個(gè)Activity"
android:id="@+id/btnSend" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/textView" />
</LinearLayout>

被啟動(dòng)的Activity:包含一個(gè)Button和editText,用于將數(shù)據(jù)發(fā)送回去和輸入要傳的數(shù)據(jù)。

public class AnotherActivity extends AppCompatActivity {
private EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_another);
editText= (EditText) findViewById(R.id.editText);
Button button= (Button) findViewById(R.id.btnSendBack);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//返回結(jié)果
Intent i=new Intent();
i.putExtra("data",editText.getText().toString());
setResult(1,i);
finish();
}
});
}
}

xml文件:activity_another.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="com.example.androidtest.AnotherActivity">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editText" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="發(fā)送回去"
android:id="@+id/btnSendBack"/>
</LinearLayout> 

運(yùn)行結(jié)果:

補(bǔ)充:這里點(diǎn)擊發(fā)送回去按鈕返回上一個(gè)Activity沒(méi)有問(wèn)題,但是如果點(diǎn)系統(tǒng)自帶的返回鍵就會(huì)出錯(cuò)了,出現(xiàn)此bug的原因就是resultCode沒(méi)有判斷,點(diǎn)擊系統(tǒng)自帶的返回鍵的resultCode==RESULT_CANCELED,所以是不一樣的。

解決方法:所以這里的requestCode和resultCode就能發(fā)揮作用了,在上述程序中requestCode==0,resultCode==1,也就是需要判斷是否是跳轉(zhuǎn)到該Activity以及返回上一個(gè)Activity是通過(guò)該按鈕還是通過(guò)系統(tǒng)返回鍵來(lái)進(jìn)行的,所以MainActivity中的onActivityResult()方法可以改進(jìn)為:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==0){
if(resultCode==1){
textView.setText("另外一個(gè)Activity傳回來(lái)的數(shù)據(jù)是:"+data.getStringExtra("data"));
}
}
}

以上所述是小編給大家介紹的獲取被啟動(dòng)的Activity傳回的數(shù)據(jù),希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

  • android studio 3.6 中配置svn的教程

    android studio 3.6 中配置svn的教程

    這篇文章主要介紹了android studio 3.6 配置svn的教程,本文所用的as版本是3.6.1,通過(guò)圖文實(shí)例相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-03-03
  • Kotlin Thread線程與UI更新詳解

    Kotlin Thread線程與UI更新詳解

    本篇主要介紹Kotlin中Thread線程與UI更新,注意不是協(xié)程而是線程。Kotlin本身是支持線程的。同時(shí)協(xié)程也是運(yùn)行在線程中的
    2022-12-12
  • Android基于廣播事件機(jī)制實(shí)現(xiàn)簡(jiǎn)單定時(shí)提醒功能代碼

    Android基于廣播事件機(jī)制實(shí)現(xiàn)簡(jiǎn)單定時(shí)提醒功能代碼

    這篇文章主要介紹了Android基于廣播事件機(jī)制實(shí)現(xiàn)簡(jiǎn)單定時(shí)提醒功能代碼,較為詳細(xì)的分析了Android廣播事件機(jī)制及提醒功能的相關(guān)實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-10-10
  • Android基于reclyview實(shí)現(xiàn)列表回彈動(dòng)畫效果

    Android基于reclyview實(shí)現(xiàn)列表回彈動(dòng)畫效果

    這篇文章主要為大家詳細(xì)介紹了Android基于reclyview實(shí)現(xiàn)列表回彈動(dòng)畫效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-04-04
  • Android讀取資源文件的方法

    Android讀取資源文件的方法

    這篇文章主要介紹了Android讀取資源文件的方法的相關(guān)資料,這里提供兩種方法及實(shí)例,需要的朋友可以參考下
    2017-08-08
  • Android applicationId和包名的區(qū)別總結(jié)

    Android applicationId和包名的區(qū)別總結(jié)

    這篇文章主要給大家介紹了關(guān)于Android applicationId和包名的區(qū)別,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)各位Android開(kāi)發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-06-06
  • Kotlin中Suppress的非常規(guī)用法示例

    Kotlin中Suppress的非常規(guī)用法示例

    這篇文章主要給大家介紹了關(guān)于Kotlin中Suppress的非常規(guī)用法的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2018-12-12
  • Android判斷用戶是否允許了攝像頭權(quán)限實(shí)例代碼

    Android判斷用戶是否允許了攝像頭權(quán)限實(shí)例代碼

    本篇文章主要介紹了Android判斷用戶是否允許了攝像頭權(quán)限實(shí)例代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-04-04
  • Android getevent用法實(shí)例詳解

    Android getevent用法實(shí)例詳解

    這篇文章主要介紹了Android getevent用法實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下
    2017-06-06
  • 詳解Android 藍(lán)牙通信方式總結(jié)

    詳解Android 藍(lán)牙通信方式總結(jié)

    這篇文章主要介紹了詳解Android 藍(lán)牙通信方式總結(jié),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2013-11-11

最新評(píng)論

吉水县| 长乐市| 周至县| 云龙县| 司法| 禄丰县| 怀仁县| 海口市| 乳源| 商河县| 镇原县| 滦平县| 繁峙县| 铜陵市| 长寿区| 万州区| 博罗县| 鄢陵县| 湘阴县| 文昌市| 玉环县| 萝北县| 石林| 澄城县| 聊城市| 宿州市| 永靖县| 苍溪县| 时尚| 庄河市| 台山市| 宁夏| 城市| 南涧| 三亚市| 闵行区| 新晃| 怀来县| 喀喇沁旗| 娱乐| 河间市|