我坚持如何在屏幕方向上保存EditTexts的状态。目前,如果将文本输入到EditTexts并且屏幕朝向,则会擦除字段(如预期的那样)。
我已经调用了onSaveInstanceState并保存了一个String,但我不知道如何保存在代码中创建的EditTexts,然后检索它们并在重绘活动时将它们添加到EditTexts。
我的代码片段:
我的主要活动如下:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// get the multidim array
b = getIntent().getBundleExtra("obj");
m = (Methods) b.getSerializable("Methods");
// method to draw the layout
InitialiseUI();
// Restore UI state from the savedInstanceState.
if (savedInstanceState != null) {
String strValue = savedInstanceState.getString("light");
if (strValue != null) {
FLight = strValue;
}
}
try {
mCamera = Camera.open();
if (FLight.equals("true")) {
flashLight();
}
} catch (Exception e) {
Log.d(TAG, "Thrown exception onCreate() camera: " + e);
}
} // end onCreate
/** Called when the back button is pressed. */
@Override
public void onResume() {
super.onResume();
try {
mCamera = Camera.open();
if (FLight.equals("true")) {
flashLight();
}
} catch (Exception e) {
Log.d(TAG, "Thrown exception onCreate() camera: " + e);
}
} // end onCreate
/** saves data before leaving the screen */
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString("light", FLight);
}
/** called when exiting / leaving the screen */
@Override
protected void onPause() {
super.onPause();
Log.d(TAG, "onPause()");
if (mCamera != null) {
mCamera.stopPreview();
mCamera.release();
mCamera = null;
}
}
/*
* set up the UI elements - add click listeners to buttons used in
* onCreate() and onConfigurationChanged()
*
* Set the editTexts fields to show the previous readings as Hints
*/
public void InitialiseUI() {
Log.d(TAG, "Start of InitialiseUI, Main activity");
// get a reference to the TableLayout
final TableLayout myTLreads = (TableLayout) findViewById(R.id.myTLreads);
// Create arrays to hold the TVs and ETs
final TextView[] myTextViews = new TextView[m.getNoRows()]; // create an empty array;
final EditText[] myEditTexts = new EditText[m.getNoRows()]; // create an empty array;
for(int i =0; i<=m.getNoRows()-1;i++ ){
TableRow tr=new TableRow(this);
tr.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
// create a new textview / editText
final TextView rowTextView = new TextView(this);
final EditText rowEditText = new EditText(this);
// setWidth is needed otherwise my landscape layout is OFF
rowEditText.setWidth(400);
// this stops the keyboard taking up the whole screen in landscape layout
rowEditText.setImeOptions(EditorInfo.IME_FLAG_NO_EXTRACT_UI);
// add some padding to the right of the TV
rowTextView.setPadding(0,0,10,0);
// set colors to white
rowTextView.setTextColor(Color.parseColor("#FFFFFF"));
rowEditText.setTextColor(Color.parseColor("#FFFFFF"));
// if readings already sent today set color to yellow
if(m.getTransmit(i+1)==false){
rowEditText.setEnabled(false);
rowEditText.setHintTextColor(Color.parseColor("#FFFF00"));
}
// set the text of the TV to the meter name
rowTextView.setText(m.getMeterName(i+1));
// set the hint of the ET to the last submitted reading
rowEditText.setHint(m.getLastReadString(i+1));
// add the textview to the linearlayout
rowEditText.setInputType(InputType.TYPE_CLASS_PHONE);//InputType.TYPE_NUMBER_FLAG_DECIMAL);
tr.addView(rowTextView);
tr.addView(rowEditText);
myTLreads.addView(tr);
// add a reference to the textView
myTextViews[i] = rowTextView;
myEditTexts[i] = rowEditText;
}
final Button submit = (Button) findViewById(R.id.submitReadings);
// add a click listener to the button
try {
submit.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.d(TAG, "Submit button clicked, Main activity");
preSubmitCheck(m.getAccNo(), m.getPostCode(), myEditTexts); // method to do HTML getting and sending
}
});
} catch (Exception e) {
Log.d(TAG, "Exceptions (submit button)" + e.toString());
}
}// end of InitialiseUI
在单击按钮之前,我不需要对这些值执行任何操作。如果它们是ListView会更容易,我猜我仍然会有保存它们并在轮换时检索它们的问题。如果它有帮助我有一个对象m是一个字符串[] []我可以暂时以某种方式将它们存储在
中答案 0 :(得分:1)
我需要做的就是为我的edittexts添加一个id:
rowEditText.setId(i)
答案 1 :(得分:0)
如何保存在代码中创建的EditTexts
如果您的意思是在运行时将对象添加到布局中,请使用onRetainNonConfigurationInstance()或setRetainInstance()(如果使用片段
)