我有一個隨機生成的整數顯示在屏幕上,當用戶單擊按鈕時,我希望該數字用新數字更新或保持不變(取決於哪個按鈕),但保持相同的活動。Android:開啓按鈕單擊,維護或更改同一活動中的值
如何在保持相同活動的同時刷新/更新值?
public class ClassName extends Activity implements OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_name);
int currentNum = randNum();
TextView myTextView = (TextView) findViewById(R.id.current_number);
myTextView.setText("Current Number: " + String.valueOf(currentNum));
okButton = (Button) findViewById(R.id.num_confirmation);
okButton.setOnClickListener(this);
changeButton = (Button) findViewById(R.id.change_num);
changeButton.setOnClickListener(this);
// set 'currentNumber' accordingly
// reprint value
}
@Override
public void onClick(View v) {
}
一個我想要做的事情是保持相同的值時,單擊一個按鈕(即currentNum
保持不變)。另一件事是用我返回一個新數字的方法(即currentNumber = methodCall();
)改變數值(用不同的按鈕點擊)。
這是如何完成的?
謝謝。代碼繼續從我的按鈕語句正確之後的onCreate執行? –
是的。這是'回電'。每當按鈕點擊它將會觸發。 –