您好,登錄后才能下訂單哦!
main端:
/** * 實現:主頁面點擊按鈕1,跳轉到頁面二輸入內容,點擊頁面二按鈕返回主頁面并顯示輸入信息; * 主頁面點擊按鈕2,跳轉到頁面三輸入內容,點擊頁面三按鈕返回主頁面并顯示輸入信息. * 主要步驟: * 1) startActivityForResult(intent, 0); * 2) 回傳數據時,調用setResult(0, data)方法,然后執行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);//有參數的super必須重寫 TextView textView = (TextView) findViewById(R.id.textView1_main); if(requestCode == BTN_ONE)//根據請求碼判斷接收內容 { 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); //監聽button1,跳轉到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); } }); //監聽button2,跳轉到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) { //得到用戶數據 EditText editText=(EditText) findViewById(R.id.editText1_next); String strone = editText.getText().toString(); //回傳數據 Intent data = new Intent(); data.putExtra("one", strone); setResult(1, data ); //關閉當前窗口 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; } }
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。