1、
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cn.itcast.set"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="8" />
</manifest>
2、
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/name"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/name"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/age"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/age"
/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button"
android:id="@+id/button"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button2"
android:id="@+id/button2"
/>
</LinearLayout>
</LinearLayout>
3、
package cn.itcast.set;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
private EditText nameText;
private EditText ageText;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
nameText = (EditText)this.findViewById(R.id.name);
ageText = (EditText)this.findViewById(R.id.age);
Button button = (Button)this.findViewById(R.id.button);
Button button2 = (Button)this.findViewById(R.id.button2);
ButtonClickListener listener = new ButtonClickListener();
button2.setOnClickListener(listener);
button.setOnClickListener(listener);
}
private final class ButtonClickListener implements View.OnClickListener{
@Override
public void onClick(View v) {
SharedPreferences preferences = getSharedPreferences("itcast", Context.MODE_WORLD_READABLE);
switch (v.getId()) {
case R.id.button:
String name = nameText.getText().toString();
String age = ageText.getText().toString();
Editor editor = preferences.edit();
editor.putString("name", name);
editor.putInt("age", new Integer(age));
editor.commit();
Toast.makeText(MainActivity.this, R.string.success, 1).show();
break;
case R.id.button2:
nameText.setText(preferences.getString("name", ""));
ageText.setText(String.valueOf(preferences.getInt("age", 20)));
break;
}
}
}
}