gpt4 book ai didi

android - 按钮 OnClickListener 只工作一次 android

转载 作者:太空狗 更新时间:2023-10-29 16:31:04 26 4
gpt4 key购买 nike

我有下面的 Activity 。当我运行该应用程序时,它只工作一次,当我第一次按下 storeBTNretrieveBTN 时,我看到 Textview 没有变化,也没有日志关于运行 store()retrieve() 函数。

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Button storeBTN = (Button) findViewById(R.id.button);
storeBTN.setOnClickListener(new View.OnClickListener(){

@Override
public void onClick(View v) {
EditText et = (EditText) findViewById(R.id.editText);
int i = Integer.parseInt(et.getText().toString());
store(i);
}
});
Button retrieveBTN = (Button) findViewById(R.id.button2);
retrieveBTN.setOnClickListener(new View.OnClickListener(){

@Override
public void onClick(View v) {
retrieve();
}
});

}
public void store(int newHighScore){
Log.d("Meeeeee", "store: "+newHighScore);
setContentView(R.layout.activity_main);
SharedPreferences sharedPref = this.getPreferences(this.MODE_APPEND);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt(getString(R.string.saved_high_score), newHighScore);
boolean b = editor.commit();
Log.d("Meeee", "store: commited? "+b);
retrieve();
return;
}
public void retrieve(){
SharedPreferences sharedPref = this.getPreferences(this.MODE_APPEND);
int defaultValue = getResources().getInteger(R.integer.saved_high_score_default);
long highScore = sharedPref.getInt(getString(R.string.saved_high_score), defaultValue);
Log.d("meeee", "retrieve: "+highScore+" in mode: "+this.MODE_APPEND);
TextView scoreTV = (TextView) findViewById(R.id.textView);
scoreTV.setText(highScore+"");
return;
}

}

最佳答案

删除

setContentView(R.layout.activity_main);

来自 store() 方法

关于android - 按钮 OnClickListener 只工作一次 android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38691983/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com