动态创建EditText

时间:2015-06-07 05:31:43

标签: android android-edittext saving-data

我有Button动态创建EditText。我在运行应用程序时创建它们没有任何问题,但是当我关闭应用程序并重新打开它时,没有创建的EditText被保存。这是我的代码:

 public class app extends ActionBarActivity {
     Button add;
     LinearLayout linearLayout1;
     static int createEditText;

     public static final String TAG = "MyPrefs" ;
     public static final String key1 = "numberOfEditTexts"; //Added this
     SharedPreferences sharedPreferences; 

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

        linearLayout1 = (LinearLayout)findViewById(R.id.linearLayout1);
        add = (Button) findViewById(R.id.add);
        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                createEditText();
            }
        }); 
    }

    public void createEditText() {
        Log.i("ET", "ET created");
        createEditText++;
        if(createEditText > 6) {
            Toast.makeText(this, "You have reached the maximum fields",     
                           Toast.LENGTH_LONG).show();
            return;
        }  

        LinearLayout layout = (LinearLayout) findViewById(R.id.linearLayout1);
        LinearLayout.LayoutParams params = new   LinearLayout.LayoutParams(
                       LinearLayout.LayoutParams.MATCH_PARENT,
                       LinearLayout.LayoutParams.MATCH_PARENT);
        params.setMargins(0, 12, 0, 0);

        EditText edtTxt = new EditText(this);
        int maxLength = 8;
        edtTxt.setHint("New ET1");
        edtTxt.setLayoutParams(params);
        edtTxt.setBackgroundColor(Color.WHITE);
        edtTxt.setInputType(InputType.TYPE_CLASS_DATETIME);
        edtTxt.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
        InputFilter[] fArray = new InputFilter[1];
        fArray[0] = new InputFilter.LengthFilter(maxLength);
        edtTxt.setFilters(fArray);
        edtTxt.setGravity(Gravity.CENTER);
        layout.addView(edtTxt);

        edtTxt.setId(createEditText);

    }


   @Override //Added this
   protected void onPause() {
   super.onPause();
   Log.i("Pause", "onPause()");
   System.out.println("EditText = " + createEditText);
   SharedPreferences.Editor editor = sharedPreferences.edit();
   editor.putInt(key1, createEditText);
   editor.commit();
   }


  @Override //Added this
  protected void onResume(){
  super.onResume();
  Log.i("Resume", "onResume()");
  System.out.println("EditText = " + createEditText);
  sharedPreferences.getInt(key1, createEditText);
  }

1 个答案:

答案 0 :(得分:0)

public static final String TAG = "MyPrefs" ;
public static final String key1 = "numberOfEditTexts";

你应该先把这两个留在你的班级。接下来,在类中创建一个SharedPreference对象。

SharedPreferences sharedPreferences;

onCreate()方法中,初始化:

sharedPreferences = getSharedPreferences(TAG, Context.MODE_PRIVATE);

您可以在按下按钮或onPause方法上为此添加值。

SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putInt(key1, createEditText);
editor.commit();

要在下次开始时想要显示离开该程序的EditText个数时检索此值,请执行以下操作:

storedNumber=editor.getInt(key1,0);

同样,如果要存储EditText的内容,可以将它们存储在同一个SharedPreference对象中。

修改

 @Override //Added this
 protected void onResume() {
     super.onResume();
     Log.i("Resume", "onResume()");
     System.out.println("EditText = " + createEditText);
     //store the value in an integer.
     int prevVal = sharedPreferences.getInt(key1, 0);

     // now use this in a for loop.
     for (int i=0; i<prevVal; i++)
         createEditText();

 }

您添加的for循环(从0运行到1)不是必需的。