Android學(xué)習(xí)小結(jié)之獲取被啟動的Activity傳回的數(shù)據(jù)
當(dāng)前Activity:包含一個Button和一個TextView,用于啟動另一個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("另外一個Activity傳回來的數(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="啟動另一個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>
被啟動的Activity:包含一個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ā)送回去按鈕返回上一個Activity沒有問題,但是如果點(diǎn)系統(tǒng)自帶的返回鍵就會出錯了,出現(xiàn)此bug的原因就是resultCode沒有判斷,點(diǎn)擊系統(tǒng)自帶的返回鍵的resultCode==RESULT_CANCELED,所以是不一樣的。
解決方法:所以這里的requestCode和resultCode就能發(fā)揮作用了,在上述程序中requestCode==0,resultCode==1,也就是需要判斷是否是跳轉(zhuǎn)到該Activity以及返回上一個Activity是通過該按鈕還是通過系統(tǒng)返回鍵來進(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("另外一個Activity傳回來的數(shù)據(jù)是:"+data.getStringExtra("data")); } } }
以上所述是小編給大家介紹的獲取被啟動的Activity傳回的數(shù)據(jù),希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
- Android Activity啟動模式之singleTask實(shí)例詳解
- Android入門之Activity四種啟動模式(standard、singleTop、singleTask、singleInstance)
- Android編程中activity啟動時出現(xiàn)白屏、黑屏問題的解決方法
- 詳解Activity之singletast啟動模式及如何使用intent傳值
- Android中Activity啟動默認(rèn)不顯示輸入法解決方法
- Android顯式啟動與隱式啟動Activity的區(qū)別介紹
- Android筆記之:App應(yīng)用之啟動界面SplashActivity的使用
相關(guān)文章
詳解Android的Socket通信、List加載更多、Spinner下拉列表
本文主要對Android的Socket通信、List加載更多、Spinner下拉列表進(jìn)行案例分析。具有很好的參考價值,需要的朋友一起來看下吧2016-12-12詳解Android App中的AsyncTask異步任務(wù)執(zhí)行方式
這篇文章主要介紹了Android App中的AsyncTask異步任務(wù)執(zhí)行方式,文中舉了一個打開網(wǎng)絡(luò)圖片的例子幫助大家直觀理解,需要的朋友可以參考下2016-04-04Android ConstraintLayout約束布局使用實(shí)例介紹
ConstraintLayout是Google在Google I/O 2016大會上發(fā)布的一種新的布局容器(ViewGroup),它支持以靈活的方式來放置子控件和調(diào)整子控件的大小,下面這篇文章主要給大家介紹了關(guān)于Android中ConstraintLayout約束布局詳細(xì)解析的相關(guān)資料,需要的朋友可以參考下2022-10-10android接收到藍(lán)牙配對請求時如何點(diǎn)亮屏幕具體實(shí)現(xiàn)
android 在接收到藍(lán)牙配對請求時如何自動點(diǎn)亮屏幕配對過程中很實(shí)用,具體的實(shí)現(xiàn)思路及代碼如下,感興趣的朋友可以參考下哈2013-06-06Android使用NumberPicker實(shí)現(xiàn)滑輪日期選擇器
這篇文章主要為大家介紹了如何使用Android中的NumberPicker控件,以一種簡單而直觀的方式實(shí)現(xiàn)滑輪式的日期選擇器,需要的小伙伴可以參考一下2023-06-06在Android上實(shí)現(xiàn)HttpServer的示例代碼
本篇文章主要介紹了在Android上實(shí)現(xiàn)HttpServer的示例代碼,實(shí)現(xiàn)Android本地的微型服務(wù)器,具有一定的參考價值,有興趣的可以了解一下2017-08-08Android使用AlertDialog實(shí)現(xiàn)對話框
本文主要介紹了Android使用AlertDialog實(shí)現(xiàn)對話框的相關(guān)知識,具有很好的參考價值。下面跟著小編一起來看下吧2017-03-03