简体   繁体   English

如何检查使用Android单击的按钮?

[英]How to check which button is Clicked with android?

I have 2 activities, the first activity include two buttons and the second activity also include 2 buttons. 我有2个活动,第一个活动包含两个按钮,第二个活动也包含2个按钮。 I need when I click on the first button of the first activity, the second activity started and the first button of the second activity become "invisible" and the second button "visible". 当我单击第一个活动的第一个按钮时,我需要启动第二个活动,第二个活动的第一个按钮变为“不可见”,第二个按钮变为“可见”。 again if the user back to the first activity and click the second button, then the second activity started where the second button become "invisible" and first one "visible". 再次,如果用户返回到第一个活动并单击第二个按钮,则第二个活动开始,其中第二个按钮变为“不可见”,第一个按钮变为“可见”。

I just need to know how to decide that which button from the first activity is pressed by the user. 我只需要知道如何确定用户按下第一个活动中的哪个按钮即可。 is there any specific method , or if any way? 有什么具体方法,或者有什么方法吗?

I tried doing it by set a variable (int i) as a global variable in the first activity and if the user click the first button then this (i) changes to 1 (Same Thing to Second Button), and I set a condition in the second activity that call this (i) variable as: 我尝试通过在第一个活动中将变量(int i)设置为全局变量来完成此操作,如果用户单击第一个按钮,则此(i)更改为1(Same Thing to Second Button),并且在第二个活动将此(i)变量称为:

MainActivity a=new MainActivity();
         if(a.i==0){
              //do this
         }
         else if(a.i==1){
              //do this
         }

but this doesn't work and the second activity always get this (i) as it's equal to (0). 但这是行不通的,并且第二个活动始终获得此(i),因为它等于(0)。

You should start your second activity by using an Intent. 您应该使用Intent开始第二个活动。 For this Intent you can put some Arguments. 为此,您可以添加一些参数。 In your First Activity: 在您的第一个活动中:

Button b1 = (Button)findViewById(R.id.button1);
Button b2 = (Button)findViewById(R.id.button2);
// Set OnClickListeners
b1.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View view) {
       // Start Activity
       Intent intent = new Intent(this, Activity2.class);
       intent.putExtra("buttontohide", 0); // Hide Button 0
       startActivity(intent);
   }
});

b2.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View view) {
       // Start Activity
       Intent intent = new Intent(this, Activity2.class);
       intent.putExtra("buttontohide", 1); // Hide Button 1
       startActivity(intent);
   }
});

In your Second Activity: 在您的第二个活动中:

Intent myIntent = getIntent();
int i = myIntent.getExtras().getInt("buttontohide");

You can pass parameters into extra before starting second activity. 您可以在开始第二个活动之前将参数传递给extra。 Use it to tell to second activity which button is clicked. 用它告诉第二个活动单击了哪个按钮。 In onCreate() method of second activity analyze extra to define which button must be hidden. 在第二个活动的onCreate()方法中,进行额外分析,以定义必须隐藏的按钮。

 MainActivity a = new MainActivity(); 

You're initializing the Activity again. 您正在重新初始化Activity But in your case, take a static variable instead. 但在您的情况下,请改用static变量。

Do something like this. 做这样的事情。

In your MainActivity.java declare a static variable 在您的MainActivity.java声明一个static变量

public class MainActivity extends Activity {
  public static int a = 0;
  // Rest of your code
}

Now from the second activity check whether the value is 0 or 1 like this 现在从第二个活动中检查值是0还是1这样

public class SecondActivity extends Activity {
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    ...
    if(MainActivity.a == 0) { /*Do something*/ }
    else { /*Do something*/ }  
  }
}

I assumed that you know all the code how to make a button visible and invisible. 我假设您了解所有代码,如何使按钮可见和不可见。 first declare an int variable in your first activity and pass it to second activity using getIntent, after that using if else logic capture your variable value and put your code to make the button invisible. 首先在您的第一个活动中声明一个int变量,然后使用getIntent将其传递给第二个活动,然后使用if逻辑捕获您的变量值并放置代码以使按钮不可见。

You could use a Bundle to pass data from Activity A to Activity B: 您可以使用Bundle将数据从活动A传递到活动B:

In Activity A: 在活动A中:

... onClick() {
    ...

    Intent intent = new Intent(this, ActivityB.class);
    intent.putExtra("ButtonNR", iButtonNR);

    startActivity(intent);
}

In Activity B: 在活动B中:

public class ActivityB extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        ...

        Bundle bundle = getIntent().getExtras();
        int iButtonNR = bundle.getInt("ButtonNR");

        switch (iButtonNR) {
            case 0:
                //do something...
                break;
            case 1:
                //do another thing...
                break;
        }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM