问题描述
我正在尝试构建一个应用程序,以便有人可以填写其个人数据,例如姓名,电话号码,电子邮件...
I'm trying to build an app where someone can fill in their personal data like their name, telephone number, Email...
对于上述每个字段,我创建了一个EditText
.现在,我的目标是使用SharedPreferences
保存用户的输入,以便他/她不必在每次重新打开应用程序时都填写该输入.
For each field mentioned above, I created an EditText
. Now my goal is to save the user's input using SharedPreferences
so that he/she don't have to fill it up every time they reopen the app.
我找到的代码仅用于保存一个EditText
的数据().如何在多个EditText
字段上实现该目标?
The codes I've found take care of saving the data for one EditText
only (Save entered text in editText via button). How can I achieve that on multiple EditText
fields?
这是我下面的XML文件:
This is my XML file below:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Vul dit compleet in"
android:textAppearance="?android:attr/textAppearanceMedium"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:textColor="#000066"
android:textStyle="bold"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Naam: "
android:textAppearance="?android:attr/textAppearanceMedium"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:textColor="#000066"
android:textStyle="bold"/>
<EditText
android:id="@+id/edit_Naam"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:textColor="#000066"
android:hint="Vul uw naam in"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Functie: "
android:textAppearance="?android:attr/textAppearanceMedium"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:textColor="#000066"
android:textStyle="bold"/>
<EditText
android:id="@+id/edit_Functie"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:textColor="#000066"
android:hint="Vul uw functie in"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Personeelsnummer "
android:textAppearance="?android:attr/textAppearanceMedium"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:textColor="#000066"
android:textStyle="bold"/>
<EditText
android:id="@+id/edit_Plnr"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:textColor="#000066"
android:hint="Vul uw plnr in"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Email: "
android:textAppearance="?android:attr/textAppearanceMedium"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:textColor="#000066"
android:textStyle="bold"/>
<EditText
android:id="@+id/edit_Email"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:textColor="#000066"
android:hint="NS mail"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Telefoon: "
android:textAppearance="?android:attr/textAppearanceMedium"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:textColor="#000066"
android:textStyle="bold"/>
<EditText
android:id="@+id/edit_Telefoon"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:textColor="#000066"
android:hint="zonder +31"/>
<Button
android:id="@+id/button_Opslaan"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_below="@+id/edit_Bericht"
android:layout_centerHorizontal="true"
android:layout_marginBottom="48dp"
android:layout_weight="0.03"
android:background="#009CDE"
android:drawableLeft="@drawable/ic_launcher"
android:text="Opslaan"
android:textColor="#FFFFFF"/>
</LinearLayout>
</ScrollView>
推荐答案
您只需保存每个EditText
值,并在下次重新加载Activity
时检索它们.以下代码改编自您在问题中提到的链接:
You just have to save each EditText
value and retrieve them next time your Activity
reloads. The code below is adapted from the link you mentioned in your question:
public class PersonalInformation extends Activity{
private SharedPreferences savedFields;
private Button saveButton;
private EditText editText1;
private EditText editText2;
// Add all your EditTexts...
// Upon creating your Activity, reload all the saved values.
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout);
saveButton = (Button) findViewById(R.id.your_save_button_id);
editText1 = (EditText) findViewById(R.id.your_edit_text_1_id);
editText2 = (EditText) findViewById(R.id.your_edit_text_2_id);
// Keep adding all your EditTexts the same way...
// "info" is just a tag name, use anything you like
savedFields = getSharedPreferences("info", MODE_PRIVATE);
// In case no value is already saved, use a Default Value
editText1.setText(savednotes.getString("editText1", "Default Value 1"));
editText2.setText(savednotes.getString("editText2", "Default Value 2"));
// Save the changes upon button click
saveButton.setOnClickListener(saveButtonListener);
}
public OnClickListener saveButtonListener = new OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences.Editor preferencesEditor = savedFields.edit();
if(editText1.getText().length() > 0) // Not empty
preferencesEditor.putString("editText1", editText1.getText());
if(editText2.getText().length() > 0) // Not empty
preferencesEditor.putString("editText2", editText2.getText());
// You can make a function so you woudn't have to repeat the same code for each EditText
// At the end, save (commit) all the changes
preferencesEditor.commit();
}
}
};
}
这篇关于使用SharedPreferences保存多个EditText值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!