android startActivityForResult的使用方法介紹
Activity 跳轉 都知道用startActivity(Intent)
但是如果下面情況呢?
Activity1 跳轉到 Activity2 但是還需要在Activity2 再回到 Activity1呢? 可能有人說: 那我在Activity2 再使用 startActivity() 不就可以了 是的 但是 startActivityForResult() 能夠直接完成這項工作
[示例]
Activity1: 有2個EditText 用于接收用戶輸入的2個字符串 要求把這2個字符串連接起來 我現(xiàn)在把連接的工作交給 Activity2 來做 并且把連接好后的字符串再返回給 Activity1 并由它負責顯示
[代碼]
1. 構建 Activity1 所需的界面
Java代碼
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<EditText
android:id="@+id/first"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<EditText
android:id="@+id/second"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/start"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="start"
/>
<TextView
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="...is waiting"
/>
</LinearLayout>
2. 得到2個EditText的用戶輸入
first = (EditText) findViewById(R.id.first);
second = (EditText) findViewById(R.id.second);
3. 把字符串裝入Bundle 再放置于Intent 然后發(fā)送之
Intent i = new Intent(this, Activity2.class);
Bundle b = new Bundle();
b.putString("first", first.getText().toString());
b.putString("second", second.getText().toString());
i.putExtras(b);
startActivityForResult(i,10);
補充:
public void startActivityForResult (Intent intent, int requestCode)
Intent intent:系統(tǒng)會根據這個確定目的Activity
int requestCode:用于標識該Intent 回來后確定是不是想要的返回
4. 注冊View監(jiān)聽器
findViewById(R.id.start).setOnClickListener(new OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
sendCalculate();
}
});
5. 構建 Activity2 的界面 把處理的結果返回
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:id="@+id/reply"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="reply"
/>
</LinearLayout>
6. 得到傳入的Intent 以及傳過來的2個字符串 并連接之
Intent i = this.getIntent();
Bundle b = i.getExtras();
String v1 = b.getString("first");
String v2 = b.getString("second");
value = v1 + v2;
7. 定義Intent 并存放返回結果 并返回之
Intent i = new Intent();
Bundle b = new Bundle();
b.putString("CALCULATION", value);
i.putExtras(b);
this.setResult(RESULT_OK, i);
this.finish();
8. 事情完成了么? 當然沒有 別忘了 Activity1 還要接收數(shù)據并顯示之
protected void onActivityResult(int requestCode, int resultCode,
Intent data){
switch (resultCode){
case RESULT_OK:
Bundle b = data.getExtras();
String string = b.getString("CALCULATION");
updateText(string);
}
}
相關文章
Android 自定義View實現(xiàn)芝麻分曲線圖效果
這篇文章主要介紹了Android 自定義View實現(xiàn)芝麻分曲線圖效果的相關資料,非常不錯具有參考借鑒價值,需要的朋友可以參考下2016-11-11
適配AndroidQ拍照和讀取相冊圖片的實現(xiàn)方法
這篇文章主要介紹了適配AndroidQ拍照和讀取相冊圖片的實現(xiàn)方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-06-06
Android :okhttp+Springmvc文件解析器實現(xiàn)android向服務器上傳照片
這篇文章主要介紹了Android :okhttp+Springmvc文件解析器實現(xiàn)android向服務器上傳照片,需要的朋友可以參考下2020-05-05
Android 提交或者上傳數(shù)據時的dialog彈框動畫效果
我們在使用支付寶支付的時候會看到類似這種彈框動畫效果,下面通過實例代碼給大家分享android 提交或者上傳數(shù)據時的彈框動畫效果,感興趣的的朋友參考下2017-07-07

