如何在活动重新启动时保存编辑文本字段

时间:2016-12-28 10:33:49

标签: java android android-studio

试图创建我的应用程序的一部分,但已经撞墙了。从列表中添加下拉列表时,您将转到新活动以添加另一个活动。然后当您返回活动时,之前的下拉选择已经消失。

有没有办法保存第一个下拉选项,这样当你返回活动时它仍然存在?

以下是我从下拉列表中添加选项的代码。

public String s;

TextView myLocationText;
TextView myLocationText2;
TextView myLocationText3;
TextView myLocationText4;
TextView myLocationText5;

EditText textCheck;
EditText textCheck2;
EditText textCheck3;
EditText textCheck4;
EditText textCheck5;

static int i = 1;

TextView testView;

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

    s = getIntent().getStringExtra("SELECTED_ACTIVITY");

    //Display selected activity in log
    Log.d("SecondActivity::" + s, "Extra");

    textCheck = (EditText) this.findViewById(R.id.sampleTextData);
    textCheck2 = (EditText) this.findViewById(R.id.sampleTextData2);
    textCheck3 = (EditText) this.findViewById(R.id.sampleTextData3);
    textCheck4 = (EditText) this.findViewById(R.id.sampleTextData4);
    textCheck5 = (EditText) this.findViewById(R.id.sampleTextData5);

    //Display selected activity
    if ((textCheck.getText().toString().equals("")) && i == 1) {
        myLocationText = (TextView) findViewById(R.id.sampleTextData);
        myLocationText.setText(s);
        i++;
    } else if ((textCheck2.getText().toString().equals("")) && i == 2) {
        myLocationText2 = (TextView) findViewById(R.id.sampleTextData2);
        myLocationText2.setText(s);
        i++;
    } else if ((textCheck3.getText().toString().equals("")) && i == 3) {
        myLocationText3 = (TextView) findViewById(R.id.sampleTextData3);
        myLocationText3.setText(s);
        i++;
    } else if ((textCheck4.getText().toString().equals("")) && i == 4) {
        myLocationText4 = (TextView) findViewById(R.id.sampleTextData4);
        myLocationText4.setText(s);
        i++;
    } else if ((textCheck5.getText().toString().equals("")) && i == 5) {
        myLocationText5 = (TextView) findViewById(R.id.sampleTextData5);
        myLocationText5.setText(s);
        i++;
    }

我已经从上面的代码中编辑了我的代码,它更干净,我已经包含了我认为需要的保存功能,但仍然没有很多运气。见下文。

public String s;

EditText textCheck;
EditText textCheck2;
EditText textCheck3;
EditText textCheck4;
EditText textCheck5;

static int i = 1;

TextView testView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.view_activity_second);
    s = getIntent().getStringExtra("SELECTED_ACTIVITY");

    //Display selected activity in log
    Log.d("SecondActivity::" + s, "Extra");

    //Look for saved text from activity
    textCheck = (EditText) this.findViewById(R.id.sampleTextData);
    if(savedInstanceState != null) {
        String editTextString = savedInstanceState.getString("textCheck");
        textCheck.setText(editTextString);
    }
    textCheck2 = (EditText) this.findViewById(R.id.sampleTextData2);
    if(savedInstanceState != null) {
        String editTextString2 = savedInstanceState.getString("textCheck2");
        textCheck2.setText(editTextString2);
    }
    textCheck3 = (EditText) this.findViewById(R.id.sampleTextData3);
    if(savedInstanceState != null) {
        String editTextString3 = savedInstanceState.getString("textCheck3");
        textCheck3.setText(editTextString3);
    }
    textCheck4 = (EditText) this.findViewById(R.id.sampleTextData4);
    if(savedInstanceState != null) {
        String editTextString4 = savedInstanceState.getString("textCheck4");
        textCheck4.setText(editTextString4);
    }
    textCheck5 = (EditText) this.findViewById(R.id.sampleTextData5);
    if(savedInstanceState != null) {
        String editTextString5 = savedInstanceState.getString("textCheck5");
        textCheck5.setText(editTextString5);
    }

    //Display selected activity
    if ((textCheck.getText().toString().equals("")) && i == 1) {
        textCheck.setText(s);
        i++;
    } else if ((textCheck2.getText().toString().equals("")) && i == 2) {
        textCheck2.setText(s);
        i++;
    } else if ((textCheck3.getText().toString().equals("")) && i == 3) {
        textCheck3.setText(s);
        i++;
    } else if ((textCheck4.getText().toString().equals("")) && i == 4) {
        textCheck4.setText(s);
        i++;
    } else if ((textCheck5.getText().toString().equals("")) && i == 5) {
        textCheck5.setText(s);
        i++;
    } else if (i == 6){
        return;
    }
}

//Save text within activity
@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putString("textCheck", textCheck.getText().toString());
    outState.putString("textCheck2", textCheck2.getText().toString());
    outState.putString("textCheck3", textCheck3.getText().toString());
    outState.putString("textCheck4", textCheck4.getText().toString());
    outState.putString("textCheck5", textCheck5.getText().toString());
}

//Retrieve previous activity text
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    if (savedInstanceState!=null) {
        textCheck.setText(savedInstanceState.getString("textCheck"));
        textCheck2.setText(savedInstanceState.getString("textCheck2"));
        textCheck3.setText(savedInstanceState.getString("textCheck3"));
        textCheck4.setText(savedInstanceState.getString("textCheck4"));
        textCheck5.setText(savedInstanceState.getString("textCheck5"));
    }
}

4 个答案:

答案 0 :(得分:1)

覆盖活动中的OnSaveInstanceState(),并使用唯一键将捆绑所需的所有值放入。喜欢:

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putString("EDITTEXT", mEditText.getText().toString());
}

使用相同的密钥检索onCreate(Bundle savedInstanceState)中的值。像:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.view_activity_second);
    textCheck = (EditText) this.findViewById(R.id.sampleTextData);
    //this will be false for first time but will be true when activity recreates.
    if(savedInstanceState != null) {
        String editTextString = savedInstanceState.getString("EDITTEXT");
        textCheck.setText(editTextString);
    }
}

答案 1 :(得分:0)

将EditText的内容保存在共享首选项中

SharedPreferences.Editor editor = getSharedPreferences(preferences1,
MODE_PRIVATE).edit();
editor.putString("text", "editTextsString");
editor.commit();

然后在活动创建或恢复时,使用以下命令调用共享首选项:

 SharedPreferences prefs = getSharedPreferences(preferences1, MODE_PRIVATE); 
 String Text = prefs.getString("text", null);

并将编辑文本设置为字符串变量:Text

答案 2 :(得分:0)

 public String s;

TextView myLocationText;
TextView myLocationText2;
TextView myLocationText3;
TextView myLocationText4;
TextView myLocationText5;

EditText textCheck;
EditText textCheck2;
EditText textCheck3;
EditText textCheck4;
EditText textCheck5;

static int i = 1;

TextView testView;

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

    s = getIntent().getStringExtra("SELECTED_ACTIVITY");

    //Display selected activity in log
    Log.d("SecondActivity::" + s, "Extra");

    textCheck = (EditText) this.findViewById(R.id.sampleTextData);
    textCheck2 = (EditText) this.findViewById(R.id.sampleTextData2);
    textCheck3 = (EditText) this.findViewById(R.id.sampleTextData3);
    textCheck4 = (EditText) this.findViewById(R.id.sampleTextData4);
    textCheck5 = (EditText) this.findViewById(R.id.sampleTextData5);

    //Display selected activity
    if ((textCheck.getText().toString().equals("")) && i == 1) {
        myLocationText = (TextView) findViewById(R.id.sampleTextData);
        myLocationText.setText(s);
        i++;
    } else if ((textCheck2.getText().toString().equals("")) && i == 2) {
        myLocationText2 = (TextView) findViewById(R.id.sampleTextData2);
        myLocationText2.setText(s);
        i++;
    } else if ((textCheck3.getText().toString().equals("")) && i == 3) {
        myLocationText3 = (TextView) findViewById(R.id.sampleTextData3);
        myLocationText3.setText(s);
        i++;
    } else if ((textCheck4.getText().toString().equals("")) && i == 4) {
        myLocationText4 = (TextView) findViewById(R.id.sampleTextData4);
        myLocationText4.setText(s);
        i++;
    } else if ((textCheck5.getText().toString().equals("")) && i == 5) {
        myLocationText5 = (TextView) findViewById(R.id.sampleTextData5);
        myLocationText5.setText(s);
        i++;
    }


}

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putString("textCheck", textCheck.getText().toString());
    outState.putString("textCheck2", textCheck2.getText().toString());
    outState.putString("textCheck3", textCheck3.getText().toString());
    outState.putString("textCheck4", textCheck4.getText().toString());
    outState.putString("textCheck5", textCheck5.getText().toString());
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    if (savedInstanceState!=null) {
        textCheck.setText(savedInstanceState.getString("textCheck"));
        textCheck2.setText(savedInstanceState.getString("textCheck2"));
        textCheck3.setText(savedInstanceState.getString("textCheck3"));
        textCheck4.setText(savedInstanceState.getString("textCheck4"));
        textCheck5.setText(savedInstanceState.getString("textCheck5"));
    }
}

只需复制并粘贴此代码即可。希望它会奏效。

答案 3 :(得分:0)

Use OnSaveInstanceState() to save the value during restart of an activity .

use below code : 

@Override
protected void onSaveInstanceState(Bundle bundle) {
    super.onSaveInstanceState(bundle);
    bundle.putString("Key","Edit Text value");
}

and get the data using  below code :


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity);
    if(savedInstanceState != null) {
        String value = savedInstanceState.getString("Key");
    }
}