android startActivityForResult的使用方法介紹_Android

來源:互聯網
上載者:User

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.