布局文件:
main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!-- 姓名标签 -->
<TextView
android:id="@+id/nameText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="10px"
android:textStyle="bold"
android:text="@string/name"
/>
<!-- 姓名输入框 -->
<EditText
android:id="@+id/nameEdit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/nameText"
android:layout_alignTop="@id/nameText"
/>
<!-- 电话 -->
<TextView
android:id="@+id/telText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/nameEdit"
android:layout_alignParentLeft="true"
android:textStyle="bold"
android:text="@string/tel"
/>
<!-- 电话输入框 -->
<EditText
android:id="@+id/telEdit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/telText"
android:layout_alignTop="@id/telText"
/>
<!-- 反馈内容 -->
<TextView
android:id="@+id/contentText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/telEdit"
android:layout_alignParentLeft="true"
android:textStyle="bold"
android:text="@string/content"
/>
<!-- 反馈输入框 -->
<EditText
android:id="@+id/contentEdit"
android:layout_width="fill_parent"
android:layout_height="120px"
android:layout_toRightOf="@id/contentText"
android:layout_alignTop="@id/contentText"
/>
</RelativeLayout>
MainActivity
import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
private EditText nameEdit;
private EditText telEdit;
private EditText contentEdit;
private final String PREFERENCES_NAME = "feedback";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
nameEdit = (EditText) findViewById(R.id.nameEdit);
telEdit = (EditText) findViewById(R.id.telEdit);
contentEdit = (EditText) findViewById(R.id.contentEdit);
SharedPreferences mySharedPreferences = getSharedPreferences(
PREFERENCES_NAME, MODE_PRIVATE);
nameEdit.setText(mySharedPreferences.getString("name", ""));
telEdit.setText(mySharedPreferences.getString("tel", ""));
}
@Override
public void onStop() {
SharedPreferences mySharedPreferences = getSharedPreferences(
PREFERENCES_NAME, MODE_PRIVATE);
SharedPreferences.Editor editor = mySharedPreferences.edit();
editor.putString("name", nameEdit.getText().toString());
editor.putString("tel", telEdit.getText().toString());
editor.commit();
super.onStop();
}
}