这是我的应用程序(基本),通过对话框TextView
设置EditText
的值。
我想在应用程序启动的第一次将TextView
设置为0,然后我想保存值的状态,这样当你打开它时,相同的值是那里。
我会给你留下我的代码:
public class MainActivity extends AppCompatActivity {
private int currentMoney;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
// Restore UI state from the savedInstanceState.
// This bundle has also been passed to onCreate.
SharedPreferences mDefaultPreferences = PreferenceManager.getDefaultSharedPreferences(this);
int current = savedInstanceState.getInt("current");
if (mDefaultPreferences.getBoolean("first_launch", true))
{
mDefaultPreferences.edit().putBoolean("first_launch", false).commit();
//Put the code here of your first launch
currentMoney = 0;
}
else
{
//Not the first launch
currentMoney = current;
}
}
我将它们分开以使任何事情更清楚
public void plus(View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(R.layout.pluslayout);
builder.setPositiveButton(R.string.add, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User clicked OK button
TextView money = (TextView) findViewById(R.id.textCool);
EditText transaction = (EditText) ((AlertDialog) dialog).findViewById(R.id.editText);
int earn = Integer.parseInt(transaction.getText().toString());
int finalEarn = currentMoney + earn;
String earnFinal = String.valueOf(finalEarn);
money.setText(earnFinal);
}
});
builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
AlertDialog dialog = builder.show();
}
public void minus(View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(R.layout.minuslayout);
builder.setPositiveButton(R.string.remove, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User clicked OK button
TextView money = (TextView) findViewById(R.id.textCool);
EditText transaction = (EditText) ((AlertDialog) dialog).findViewById(R.id.editText);
int loss = Integer.parseInt(transaction.getText().toString());
int currentMoney = Integer.parseInt(money.getText().toString());
int finalLoss = currentMoney - loss;
String lossFinal = String.valueOf(finalLoss);
money.setText(lossFinal);
}
});
builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
AlertDialog dialog = builder.show();
}
最后
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
// Save UI state changes to the savedInstanceState.
// This bundle will be passed to onCreate if the process is
// killed and restarted.
TextView money = (TextView) findViewById(R.id.textCool);
int currentMoney = Integer.parseInt(money.getText().toString());
savedInstanceState.putInt("current", currentMoney);
}
}
我会留下你的MainActivity
Java文件,因为这可能就是问题所在。谢谢。
答案 0 :(得分:2)
我建议使用SharedPreferences来存储和检索TextViews的值。您可能已经熟悉此过程,因为您从SharedPreferences对象中检索了一个值,但以防万一,这是我的某个应用程序的片段...
SharedPreferences.Editor editor = mDefaultPreferences.edit();
editor.putString("last known value", money.getText();
editor.apply(); //this will immediately write your changes in-memory, then start an asynchronous task to write the values to permanent storage on the disc.
让价值回归......
String storedValue = mDefaultPreferences.getString("last known value", 0);
money.setText(storedValue);
答案 1 :(得分:0)
在Developer guide中,它表示builder.create();应该在链接参数后出现。我认为builder.show()只显示值,不会存储它。