当前位置: 代码迷 >> Android >> 对象值更改后,Android中的更新按钮?
  详细解决方案

对象值更改后,Android中的更新按钮?

热度:88   发布时间:2023-08-04 09:43:35.0

我有一个存储类型为Cup的对象的数组。 该按钮显示杯子中的numberOfPebbles值。 单击该按钮会使所有按钮增加1-我已经登录logcat并更新了数组,但是使用仿真器时我无法使用新的数组值来更新按钮。 这是我的代码:请记住它们在单独的类中

button1 = (Button)findViewById(R.id.button1);
button1.setText(move.cups.get(0).toString());
button1.setOnClickListener(new Button.OnClickListener() {
    public void onClick(View v) {
        move.makeMove(move.cups.get(0));
    }
});

public List<Cup> cups;
public List<Integer> cupValues;

public void newGame(){

    cups = new ArrayList<>();
    cupValues = new ArrayList<>();

    //Reset all cups and values
    for(int i=0; i<16; i++){
        Cup cup = new Cup(7);
        cups.add(i, cup);
        Log.d("Move.java", "Printing array valuess:" + cups.get(i));
    }
}

您需要在每次单击时更新按钮的文本

button1 = (Button) findViewById(R.id.button1);
button1.setText(move.cups.get(0).toString());
button1.setOnClickListener(new Button.OnClickListener() {
    public void onClick(View v) {
        move.makeMove(move.cups.get(0));
        button1.setText(move.cups.get(0).toString()); //or whatever you want to display in the button after each click
    }
});
  相关解决方案