我已声明全局整数变量'add',其目的是添加在edittext'e1'中输入的值。值显示在textview't11'中。创建一个按钮以重置textview't11'的值 我如何清除以前由“添加”持有的整数值。
public class MainActivity extends Activity {
TextView t11;
Button b3;
EditText e1;
int add,tankfuel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
t11=(TextView)findViewById(R.id.textView11);
b3=(Button) findViewById(R.id.button3);
b4=(Button) findViewById(R.id.button4);
b4.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
tankfuel=Integer.parseInt(e1.getText().toString());
add=add+tankfuel;
t11.setText(String.valueOf(add));
e1.setText("");
}
});
b3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
t11.setText("");
//here the value held by add should be cleared
}
});
}
}
答案 0 :(得分:0)
尝试使用
b3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
t11.setText("");
}
}