0

assume that i have three buttons all of whose listeners point to (this) . So if I press any of the button is it going to start from the beginning of the activity, irrespective of the button pressed?

4

3 回答 3

3

No, it will go on override method onClick() of your Activity.

listeners point to (this)

you must have to implements onClickListener to your activity which will override onClick()

To verify which view had been clicked, you have to do something like

@Override
public void onClick(View v)
{
    if(v== controlName)
       // controlName is clicked
}
于 2012-04-23T07:26:12.843 回答
0

您可以做的是使用开关盒并了解单击的按钮是什么。

尝试这个:

class stuff extends Activity implements onClickListener {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        Button button1 = (Button) findViewById(R.id.button1);
        Button button2 = (Button) findViewById(R.id.button2);
        Button button3 = (Button) findViewById(R.id.button3);

        button1.setOnClickListener(this);
        button2.setOnClickListener(this); 
        button3.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.button1:
             // do something
             break;
        case R.id.button2:
             // do something else
             break;
        case R.id.button3:
             // do yet another thing
             break;
        default:
            // do nothing
            break;
        }
}
于 2012-04-23T07:40:14.723 回答
-1

令人困惑的问题...

无论如何,这里有一个监听器示例。

public class MyClass extends Activity {
   private Button firstBtn;
   private Button secondBtn;
   private Button thirdBtn;

   public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    firstBtn  = (Button) findViewById(R.id.firstBtn);
    secondBtn = (Button) findViewById(R.id.secondBtn);
    thirdBtn  = (Button) findViewById(R.id.thirdBtn); 

    firstBtn.setOnClickListener(new OnClickListener()
    {           
        public void onClick(View v)
        {
            Log.d("MyClass", "first button clicked!");
        }
    });

    secondBtn.setOnClickListener(new OnClickListener()
    {           
        public void onClick(View v)
        {
            Log.d("MyClass", "second button clicked!");
        }
    });

    thirdBtn.setOnClickListener(new OnClickListener()
    {           
        public void onClick(View v)
        {
            Log.d("MyClass", "third button clicked!");
        }
    });
   }

}

希望能帮助到你。干杯

于 2012-04-23T07:36:02.927 回答