我有这段代码,我想在更改或开始新活动后从第一个输入中保留我的编辑框值。
这个代码中发生了什么:
editbox1 = 1 > start new activity > back to recent activity > editbox1 = null
我需要这件事发生:
editbox1 = 1 > start new activity > back to recent activity > editbox1 = 1
CODE
package org.example.touch;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.*;
import android.widget.EditText;
public class SettingsClass extends Activity {
private EditText Alpha;
private EditText Beta;
private EditText Charlie;
private EditText Delta;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings);
Alpha = (EditText) findViewById(R.id.editText1);
Beta = (EditText) findViewById(R.id.editText2);
Charlie = (EditText) findViewById(R.id.editText3);
Delta = (EditText) findViewById(R.id.editText4);
}
public void buttonSBHandler (View view){
String Aint = Alpha.getText().toString();
String Bint = Beta.getText().toString();
String Cint = Charlie.getText().toString();
String Dint = Delta.getText().toString();
Intent startNewActivityOpen = new Intent(SettingsClass.this, GameUi.class);
startNewActivityOpen.putExtra("Aint", Aint);
startNewActivityOpen.putExtra("Bint", Bint);
startNewActivityOpen.putExtra("Cint", Cint);
startNewActivityOpen.putExtra("Dint", Dint);
startActivityForResult(startNewActivityOpen, 0);
//startActivity(new Intent(view.getContext(), GameUi.class));
}
}
答案 0 :(得分:2)
1)有一点是你可以使用共享首选项将你的值存储在共享首选项中,并且oncreate()方法首先检查共享首选项是否为null,如果它不为null,则从共享首选项中获取值。 / p>
或
2)只需将编辑文本的数据设为静态,如 -
static String Aint = Alpha.getText().toString();
static String Bint = Beta.getText().toString();
static String Cint = Charlie.getText().toString();
static String Dint = Delta.getText().toString();
所以每当你回到活动时,编辑文本的先前数据都会显示在那里。 希望这些东西对你有用。 感谢