您好,登錄后才能下訂單哦!
這篇文章主要為大家詳細介紹了resultCode返回碼和onclick代碼重構的實現,文中示例代碼介紹的非常詳細,零基礎也能參考此文章,感興趣的小伙伴們可以參考一下。
主頁:
public class MainActivity extends Activity { /** * 主頁點擊按鈕跳轉到第二頁 * 第二頁點擊按鈕1將字符串內容顯示到主頁,點擊按鈕2將數字內容顯示到主頁 */ private final static int RESULT_CODE_ONE = 1; private final static int RESULT_CODE_TWO = 2; @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); TextView textView = (TextView) findViewById(R.id.textView1_main); String text = null; switch (resultCode)//判斷返回碼 { case RESULT_CODE_ONE: text = "from按鈕1:" + data.getStringExtra("btnone"); break; case RESULT_CODE_TWO: int age = data.getIntExtra("btntwo", 0);//0為默認值,當data沒有接收到返回的int時,返回這個默認值 text ="from按鈕2:"+ String.valueOf(age); break; default: break; } textView.setText(text); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findViewById(R.id.button1_main).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MainActivity.this, SecondActivity.class); startActivityForResult(intent , 0); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true; } }
第二頁
public class SecondActivity extends Activity { private final static int RESULT_CODE_ONE = 1; private final static int RESULT_CODE_TWO = 2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); final Intent data = new Intent(); findViewById(R.id.button1).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { data.putExtra("btnone", "name"); setResult(RESULT_CODE_ONE, data );//設置返回碼 finish(); } }); findViewById(R.id.button2).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { data.putExtra("btntwo", 12); setResult(RESULT_CODE_TWO, data );//設置返回碼 finish(); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.second, menu); return true; } }
第二頁按鈕事件重構過程一:
1.實現OnClickListener接口
2.重寫onClick(View v)
3.findViewById(R.id.button1).setOnClickListener(this)
public class SecondActivity extends Activity implements OnClickListener//實現監聽者接口 { private final static int RESULT_CODE_ONE = 1; private final static int RESULT_CODE_TWO = 2; @Override public void onClick(View v)//重寫點擊方法 { Intent data = new Intent(); if(v.getId() == R.id.button1)//判斷view的id { data.putExtra("btnone", "name"); setResult(RESULT_CODE_ONE, data ); finish(); } if(v.getId() == R.id.button2) { data.putExtra("btntwo", 12); setResult(RESULT_CODE_TWO, data ); finish(); } } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); findViewById(R.id.button1).setOnClickListener(this);//監聽按鈕 findViewById(R.id.button2).setOnClickListener(this); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.second, menu); return true; } }
第二頁按鈕事件重構過程二:將事件內容以方法代替
public class SecondActivity extends Activity implements OnClickListener//實現監聽者接口 { private final static int RESULT_CODE_ONE = 1; private final static int RESULT_CODE_TWO = 2; @Override public void onClick(View v)//重寫點擊方法 { Intent data = new Intent(); if(v.getId() == R.id.button1)//判斷view的id { // data.putExtra("btnone", "name"); // setResult(RESULT_CODE_ONE, data ); // finish(); sendResult("btnone", "name", RESULT_CODE_ONE); } if(v.getId() == R.id.button2) { // data.putExtra("btntwo", 12); // setResult(RESULT_CODE_TWO, data ); // finish(); sendResult("btntwo", Integer.valueOf(12), RESULT_CODE_TWO); } } private void sendResult(String extraKey, Object content, int resultCode) { Intent data = new Intent(); if (content instanceof String) { data.putExtra(extraKey, (String) content); } else if (content instanceof Integer) { data.putExtra(extraKey, (Integer) content); } setResult(resultCode, data); finish(); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); findViewById(R.id.button1).setOnClickListener(this);//監聽按鈕 findViewById(R.id.button2).setOnClickListener(this); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.second, menu); return true; } }
以上就是resultCode返回碼和onclick代碼重構的代碼,如果在日常工作遇到這個問題,希望你能通過這篇文章解決問題。如果想了解更多相關內容,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。