这篇文章主要为大家详细介绍了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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。