国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

android startActivityForResult的使用方法介紹

2020-04-11 12:17:59
字體:
來源:轉載
供稿:網友

Activity 跳轉 都知道用startActivity(Intent)
但是如果下面情況呢?
Activity1 跳轉到 Activity2  但是還需要在Activity2 再回到 Activity1呢? 可能有人說: 那我在Activity2  再使用 startActivity() 不就可以了 是的 但是 startActivityForResult() 能夠直接完成這項工作
[示例]
Activity1: 有2個EditText 用于接收用戶輸入的2個字符串 要求把這2個字符串連接起來 我現在把連接的工作交給 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 然后發送之

復制代碼 代碼如下:

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:系統會根據這個確定目的Activity  

int requestCode:用于標識該Intent 回來后確定是不是想要的返回 


4. 注冊View監聽器

復制代碼 代碼如下:

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 還要接收數據并顯示之

復制代碼 代碼如下:

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);  
        }  
    } 

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 偃师市| 大竹县| 荔波县| 托克托县| 卫辉市| 五指山市| 满洲里市| 太康县| 松阳县| 迭部县| 乐亭县| 沙坪坝区| 南宫市| 泰顺县| 晋江市| 嘉善县| 罗定市| 增城市| 方正县| 娱乐| 阳江市| 寿宁县| 金寨县| 岐山县| 繁昌县| 陆丰市| 杂多县| 杭州市| 蒲江县| 堆龙德庆县| 财经| 天水市| 沂南县| 五原县| 河西区| 乐业县| 三亚市| 三亚市| 邓州市| 伊春市| 平陆县|