如何只获得特定按钮的点击

How to get only a specific button's click

我在 MainActivity 上有 2 个按钮。我想要第一个按钮将我发送到 TestPage1.class(它已经这样做了)。我想要第二个按钮将我发送到 TestPage2.class。每当我单击第二个按钮时,它都会将我直接发送到 TestPage1 而不是 TestPage2。我该如何纠正?

import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.view.View.OnClickListener;


public class MainActivity extends ActionBarActivity implements OnClickListener {
    Button button1, button2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button1 = ((Button) findViewById(R.id.button1));
        button1.setOnClickListener(this);
        button2 = ((Button) findViewById(R.id.button2));
        button2.setOnClickListener(this);
    }
        @Override public void onClick(View v) {

                startActivity(new Intent(MainActivity.this, TestPage1.class));


        }

 public void onClick2(View v){
     startActivity(new Intent(MainActivity.this, TestPage2.class));

}





    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

使用v.getId()检查在onClick方法中点击了哪个按钮:

@Override 
public void onClick(View v) {

  if(v.getId()==R.id.button1)
   startActivity(new Intent(MainActivity.this, TestPage1.class));

 }

或使用 switch-case 处理同一个监听器的多次按钮点击:

  @Override 
  public void onClick(View v) {

     switch(v.getId()) {
       case R.id.button1:
       // ... your code here...
       break;
       case R.id.button2:
       // ... your code here...
       break;
       default:
       //...

     }

 }

在里面使用这个onClick(View v)

switch(v.getId()) {
       case R.id.button1:
       // do stuff;
       break;
       case R.id.button2:
       // do stuff;
       break;
   ...
   // declare buttons b1,b2
    // remove "implements onclick listener" and use this

 enter code here
    b1.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View arg0) {
                    Intent inten = new Intent(this,
                            TestPage1.class);
                    startActivity(inten);
                }
            });

    b2.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View arg0) {
                    Intent inten = new Intent(this,
                            TestPage2.class);
                    startActivity(inten);
                    finish();
                }
            });