如何在Android中的TextViews数组中放置按钮的值

时间:2013-12-30 10:20:51

标签: android

我有6个Button和6个TextView被放入2个不同的数组中。我想要做的是当我点击Button时,Button的值将设置在第一个TextView上,下一个Button我点击将设置在下一个TextView等等。我不知道这样做的正确方法。我知道下面的代码是错误的,因为我将'x'设置为'0'但是我被卡住了所以请帮忙。

    Button btn1 = (Button) findViewById(R.id.btn1);
    Button btn2 = (Button) findViewById(R.id.btn2);
    Button btn3 = (Button) findViewById(R.id.btn3);
    Button btn4 = (Button) findViewById(R.id.btn4);
    Button btn5 = (Button) findViewById(R.id.btn5);
    Button btn6 = (Button) findViewById(R.id.btn6);
    Button submit = (Button) findViewById(R.id.btnsubmit);

    btn1.setOnClickListener(this);    
    btn2.setOnClickListener(this);             
    btn3.setOnClickListener(this);
    btn4.setOnClickListener(this);
    btn5.setOnClickListener(this);
    btn6.setOnClickListener(this);


    btn1.setText("A");
    btn2.setText("M");
    btn3.setText("O");
    btn4.setText("N");  
    btn5.setText("L");
    btn6.setText("S");
    submit.setText("Submit");
}

public void onClick(View v) {
    final TextView[] textView = new TextView[6];
        textView[0] = (TextView)findViewById(R.id.t1);
        textView[1] = (TextView)findViewById(R.id.t2);
        textView[2] = (TextView)findViewById(R.id.t3);
        textView[3] = (TextView)findViewById(R.id.t4);
        textView[4] = (TextView)findViewById(R.id.t5);
        textView[5] = (TextView)findViewById(R.id.t6);
    final Button button[] = new Button[6];
        button[0] = (Button)findViewById(R.id.btn1);
        button[1] = (Button)findViewById(R.id.btn2);
        button[2] = (Button)findViewById(R.id.btn3);
        button[3] = (Button)findViewById(R.id.btn4);
        button[4] = (Button)findViewById(R.id.btn5);
        button[5] = (Button)findViewById(R.id.btn6);



   int x=0;   
   for(int i = 0; i <6; i++){
 if (button[i].getId() == v.getId()){
     textView[x].setText(button[i].getText());
 }
   }
}

3 个答案:

答案 0 :(得分:0)

尝试这种方式:

for(int i = 0; i <6; i++){
 if (button[i].getId() == v.getId()){
     textView[i].setText(button[i].getText());
 }
}

答案 1 :(得分:0)

试试这个。

将x初始化为全局变量。

int x=0; // in onCreate instead of onClick

将onClick中的代码更改为

for(int i = 0; i <6; i++){
    if (button[i].getId() == v.getId()){
        textView[x].setText(button[i].getText());
        if(x<6)
            x++;
        else
        {
            //You will have to think of logic in case any button is clicked second time after 6 clicks.
        }
    }
}

希望这能给出一个提示。

答案 2 :(得分:0)

请使用此代码,因为我在我的机器上检查了您的问题并且此代码正常。

使用此代码: -

final TextView[] textView = new TextView[6];
Button btn1, btn2, btn3, btn4, btn5, btn6;
int counter = 0;

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

    textView[0] = (TextView) findViewById(R.id.t1);
    textView[1] = (TextView) findViewById(R.id.t2);
    textView[2] = (TextView) findViewById(R.id.t3);
    textView[3] = (TextView) findViewById(R.id.t4);
    textView[4] = (TextView) findViewById(R.id.t5);
    textView[5] = (TextView) findViewById(R.id.t6);
    btn1 = (Button) findViewById(R.id.btn1);
    btn1.setOnClickListener(this);
    btn2 = (Button) findViewById(R.id.btn2);
    btn2.setOnClickListener(this);
    btn3 = (Button) findViewById(R.id.btn3);
    btn3.setOnClickListener(this);
    btn4 = (Button) findViewById(R.id.btn4);
    btn4.setOnClickListener(this);
    btn5 = (Button) findViewById(R.id.btn5);
    btn5.setOnClickListener(this);
    btn6 = (Button) findViewById(R.id.btn6);
    btn6.setOnClickListener(this);

}
    @Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    if (counter >= 6) {
        counter = 0;
    }
    switch (v.getId()) {
    case R.id.btn1:
        textView[counter++].setText(btn1.getText().toString());
        break;
    case R.id.btn2:
        textView[counter++].setText(btn2.getText().toString());
        break;
    case R.id.btn3:
        textView[counter++].setText(btn3.getText().toString());
        break;
    case R.id.btn4:
        textView[counter++].setText(btn4.getText().toString());
        break;
    case R.id.btn5:
        textView[counter++].setText(btn5.getText().toString());
        break;
    case R.id.btn6:
        textView[counter++].setText(btn6.getText().toString());
        break;
    default:
        break;
    }

}