当我点击添加按钮时,它会从0开始计算数字并一次又一次显示,但是当我点击时 删除按钮它干净编辑文本,但当开始再次单击添加按钮时,它从它停止的最后一个数字开始计数,所以当我从(0)开始计数的每一次点击它时,我想要。
final Button add = (Button) findViewById(R.id.button1);
final Button delet = (Button) findViewById(R.id.button2);
final EditText edit = (EditText) findViewById(R.id.editText1);
add.setOnClickListener(new View.OnClickListener() {
private int number = 1;
public void onClick(View v) {
// TODO Auto-generated method stub
// random = random();
edit.setText("" + number++); // i want when i click on it in every single time it beginning to count from( 0 )
//because when i delete it and start to click again on add button it count from the last number that stop on it
}
});
delet.setOnClickListener(new View.OnClickListener() {
private int numberr;
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
edit.setText("0"); // should i change this clean code to make it count from the beginning in every single click ?
}
});
}
答案 0 :(得分:0)
您必须使数字变量为全局才能使代码正常运行。
单击按钮后,它将始终初始化为1.
add.setOnClickListener(new View.OnClickListener() {
// make it global or store in intent to retain its value after incrementing and intialize it globally and not here else again will be same case.
// number = 1;
public void onClick(View v) {
// TODO Auto-generated method stub
// random = random();
edit.setText("" + number++); // i want when i click on it in every single time it beginning to count from( 0 )
//because when i delete it and start to click again on add button it count from the last number that stop on it
}
});