溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》
  • 首頁 > 
  • 教程 > 
  • 開發(fā)技術(shù) > 
  • Activity初級:startActivityForResult、重寫onActivityResult、setResult回傳數(shù)據(jù)、requestCode請求碼

Activity初級:startActivityForResult、重寫onActivityResult、setResult回傳數(shù)據(jù)、requestCode請求碼

發(fā)布時間:2020-07-14 12:55:48 來源:網(wǎng)絡(luò) 閱讀:1223 作者:glblong 欄目:開發(fā)技術(shù)

main端:

/**
 * 實現(xiàn):主頁面點擊按鈕1,跳轉(zhuǎn)到頁面二輸入內(nèi)容,點擊頁面二按鈕返回主頁面并顯示輸入信息;
 *     主頁面點擊按鈕2,跳轉(zhuǎn)到頁面三輸入內(nèi)容,點擊頁面三按鈕返回主頁面并顯示輸入信息.
 * 主要步驟:
 * 1) startActivityForResult(intent, 0);
 * 2) 回傳數(shù)據(jù)時,調(diào)用setResult(0, data)方法,然后執(zhí)行finish()
 * 3) onActivityResult(int, int, Intent data)
 */
public class MainActivity extends Activity
{
    private static final int BTN_ONE = 1;//將常量封裝定義
    private static final int BTN_TWO = 2;
                                 
    @Override//重寫onActivityResult
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        super.onActivityResult(requestCode, resultCode, data);//有參數(shù)的super必須重寫
        TextView textView = (TextView) findViewById(R.id.textView1_main);
                                     
        if(requestCode == BTN_ONE)//根據(jù)請求碼判斷接收內(nèi)容
        {
            String str = data.getStringExtra("one");
            textView.setText(str);
        }
        else if(requestCode == BTN_TWO)
        {
            String str = data.getStringExtra("two");
            textView.setText(str);
        }
                                     
    }
                                 
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
                                     
        //監(jiān)聽button1,跳轉(zhuǎn)到NextActivity,等待result
        Button button1=(Button) findViewById(R.id.button1_main);
        button1.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                Intent intent = new Intent(MainActivity.this,NextActivity.class);
                startActivityForResult(intent, BTN_ONE);
            }
        });
                                     
        //監(jiān)聽button2,跳轉(zhuǎn)到ThirdActivity,等待result
        Button button2 =(Button) findViewById(R.id.button2_main);
        button2.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
               Intent intent = new Intent(MainActivity.this,ThirdActivity.class);
               startActivityForResult(intent, BTN_TWO);
            }
        });
    }
                                 
    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}


next端:

public class NextActivity extends Activity
{
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_next);
                                  
        Button button =(Button) findViewById(R.id.button1_next);
        button.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                //得到用戶數(shù)據(jù)
                EditText editText=(EditText) findViewById(R.id.editText1_next);
                String strone = editText.getText().toString();
                                          
                //回傳數(shù)據(jù)
                Intent data = new Intent();
                data.putExtra("one", strone);
                setResult(1, data );
                                          
                //關(guān)閉當前窗口
                finish();
            }
        });
    }
                              
    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        getMenuInflater().inflate(R.menu.next, menu);
        return true;
    }
}


third端:

public class ThirdActivity extends Activity
{
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_third);
                               
        Button button = (Button) findViewById(R.id.button1_third);
        button.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                EditText editText = (EditText) findViewById(R.id.editText1_third);
                String strtwo = editText.getText().toString();
                Intent data = new Intent();
                data.putExtra("two", strtwo);
                setResult(2,data );
                finish();
            }
        });
    }
                           
    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        getMenuInflater().inflate(R.menu.third, menu);
        return true;
    }
}




向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI