0

私自身の練習では、インスタンス フィールドに 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);
                }

                }
               });


               }
            }

       }
4

1 に答える 1