Android 按钮数组的侦听器

Android 按钮数组的侦听器,android,Android,在我自己的实践中,我正在实例字段中创建一个由3个按钮组成的数组,我希望所有按钮都具有setOnClickListeners,允许每个按钮更改文本视图的背景颜色。任何人都可以引导我走向正确的方向。以下是我的代码: public class MainActivity extends Activity { Button b = {(Button)findViewById(R.id.button1), (Button)findView

在我自己的实践中,我正在实例字段中创建一个由3个按钮组成的数组,我希望所有按钮都具有setOnClickListeners,允许每个按钮更改文本视图的背景颜色。任何人都可以引导我走向正确的方向。以下是我的代码:

         public class MainActivity extends Activity {

      Button b = {(Button)findViewById(R.id.button1), 
                  (Button)findViewById(R.id.button2),
                   (Button)findViewById(R.id.button3),};

     TextView tv;

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

    tv = (TextView) findViewById(R.id.textView1);

   for(int i=0; i < b.length;i++){





    b[i].setOnClickListener(new OnClickListener() {

    @Override
        public void onClick(View v) {

            if(butt[0].isPressed()){
        tv.setBackgroundColor(Color.BLACK);
            }


            if(b[1].isPressed()){
                tv.setBackgroundColor(Color.BLUE);
                }
            if(b[2].isPressed()){
                tv.setBackgroundColor(Color.RED);
                }

                }
               });


               }
            }

       }
公共类MainActivity扩展活动{
按钮b={(按钮)findViewById(R.id.button1),
(按钮)findViewById(R.id.button2),
(按钮)findViewById(R.id.button3),};
文本视图电视;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv=(TextView)findViewById(R.id.textView1);
for(int i=0;i
您没有为
按钮声明
数组。我不确定这会做什么,或者它是否会编译,但我不这么认为

Button b = {(Button)findViewById(R.id.button1), 
              (Button)findViewById(R.id.button2),
               (Button)findViewById(R.id.button3),};
换成

Button[] b = {(Button)findViewById(R.id.button1), 
              (Button)findViewById(R.id.button2),
               (Button)findViewById(R.id.button3),};
此外,此代码必须紧跟
setcontentView()
之后,否则您将获得
NPE
,因为您的
按钮存在于
版面中
,而您的
版面在通过调用
setcontentView()
对其进行充气之前不存在

您可以在
onCreate()
之前声明您的
数组
,但只有在充气
布局

所以你可以这样做

public class MainActivity extends Activity {

  Button[] b = new Button[3];  //initialize as an array

 TextView tv;

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

b = {(Button)findViewById(R.id.button1), 
          (Button)findViewById(R.id.button2),
           (Button)findViewById(R.id.button3),};  //add buttons AFTER calling setContentView()
...
编辑由于@pragnani删除了他的答案,我将编辑一点,这是个好主意

您可以通过在
for循环中执行以下操作来选择使用
开关单击的
按钮
,从而简化逻辑

b[i].setOnClickListener(new OnClickListener() {

@Override
    public void onClick(View v) {   / v is the button that was clicked

        switch (v.getId())    //so we get its id here
        {
             case (R.id.button1):
             tv.setBackgroundColor(Color.BLACK);
             break;
             case (R.id.button2):
             tv.setBackgroundColor(Color.BLUE);
             break;
             case (R.id.button3):
             tv.setBackgroundColor(Color.RED);
             break;
        }

您正在为按钮设置侦听器。因此,当按下该按钮时,只包含要在该特定按钮上运行的代码。按钮b=…
应该是
button[]b=…
。@chris cooney,非常感谢。@HRo我编辑了我的答案,添加了多一点,这将使它更容易…根据pragnani删除的答案