我是新的android開發.. 我有這樣的代碼在我的主類:Android - 如何增加EditText值?
Button prevBtn, pauseBtn, nextBtn;
EditText counterTxt;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_affirmations);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
prevBtn = (Button)findViewById(R.id.prevBtn);
pauseBtn = (Button)findViewById(R.id.pauseBtn);
nextBtn = (Button)findViewById(R.id.nextBtn);
counterTxt = (EditText)findViewById(R.id.counterTxt);
prevBtn.setOnClickListener(new View.OnClickListener() {
int t = Integer.parseInt(counterTxt.getText().toString());
public void onClick(View v) {
counterTxt.setText(String.valueOf(t-1));
}
});
nextBtn.setOnClickListener(new View.OnClickListener() {
int t = Integer.parseInt(counterTxt.getText().toString());
public void onClick(View v) {
counterTxt.setText(String.valueOf(t+1));
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_affirmations, menu);
return true;
}
當我點擊「上一步」,則文本字段值變爲19
當我點擊「下一步「,文本字段值將變爲21.
但它只顯示這兩個值,沒有別的,無論我再次單擊。我想減去或加1,只要我點擊相應的按鈕。
我認爲這是因爲事件監聽器在onCreate()方法內?每次點擊時如何更新它的想法?
謝謝你,我不能相信我沒有」看到這個:)這是因爲Android開發(和Java)仍然讓我困惑。非常感謝@Ralgha :) –