0

我被困在如何在屏幕方向上保存我的 EditTexts 的状态。目前,如果将文本输入到 EditTexts 并且屏幕被定向,则字段将被擦除(如预期的那样)。

我已经在调用 onSaveInstanceState 并保存一个字符串,但我不知道如何保存在代码中创建的 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 这是一个字符串 [] [] 我可以暂时以某种方式将它们存储在

4

2 回答 2

1

我需要做的就是在我的编辑文本中添加一个 id:

    rowEditText.setId(i)
于 2012-11-26T07:24:36.113 回答
0

如何保存在代码中创建的 EditText

如果您的意思是在运行时将对象添加到布局中,请使用onRetainNonConfigurationInstance()setRetainInstance()如果使用 Fragments

于 2012-11-25T23:02:52.900 回答