如何检查,如果我的按钮被点击在另一个活动? [英] How to check if my button is clicked in another activity?

查看:126
本文介绍了如何检查,如果我的按钮被点击在另一个活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想这取决于在点击另一个按钮布局让文本(我有按钮1和按钮2)我不能做一个if语句语句,因为它不承认的按钮。

I want to get text depending on which button in another layout in clicked (I have button 1 and button 2) I can't do a if clause statement because it doesn't recognize the buttons.

推荐答案

您可以设置不同的一些变量值不同(如,如果你点击按钮1让其值设为1或者2)传递整数变量作为意图在previous活动,并使用GetExtras()来检索整型变量...现在使用的IF条件来确定哪个按钮被点击

You could set different set some variable to different values(eg. if you click on button1 make its value as 1 or else 2) pass that integer variable as an intent to the previous activity and use GetExtras() to retrieve the integer variable...now use the IF condition to determine which button was clicked

public void onClick(View view) {
int code;

switch(view.getId()){
case R.id.button1:
 code=1;
break;

case R.id.button2:
 code=2;
break;
}
    Intent i = new Intent(this, ResultActivity.class);
    i.putExtra("yourcode", code);
    startActivityForResult(i, REQUEST_CODE);
  }

现在回到你的老活动中使用

Now back to your old activity use

getIntent()getExtras(你的code)的toString()。检索它

getIntent().getExtras("yourcode").toString(); to retrieve it

这篇关于如何检查,如果我的按钮被点击在另一个活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆