数据存储preferenceActivity
1、preferenceActivity.java
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.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class preferenceActivity extends Activity implements OnClickListener{
/** Called when the activity is first created. */
EditText text1;
EditText text2;
Button button1;
Button button2;
Button button3;
SharedPreferences sharedPreferences;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
init();
buttonListener();
}
public void init(){
text1 = (EditText)findViewById(R.id.editText1);
text2 = (EditText)findViewById(R.id.editText2);
button1 = (Button)findViewById(R.id.button1);
button2 = (Button)findViewById(R.id.button2);
button3 = (Button)findViewById(R.id.button3);
}
public void buttonListener(){
button1.setOnClickListener(this);
button2.setOnClickListener(this);
button3.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()){
case R.id.button1:
setSharedPreference();
text1.setText("已经输入数据到preference");
text2.setText("已经输入数据到preference");
break;
case R.id.button2:
text1.setText("");
text2.setText("");
break;
case R.id.button3:
getSahrePreference();
break;
}
}
//存储sharedpreferences
public void setSharedPreference(){
sharedPreferences = getSharedPreferences("itcast", Context.MODE_PRIVATE);
Editor editor = sharedPreferences.edit();
editor.putString("username", "我的帐号");
editor.putInt("password", 123456);
editor.commit();//提交修改
}
//获得sharedpreferences的数据
public void getSahrePreference(){
String username = sharedPreferences.getString("username", "");
int password = sharedPreferences.getInt("password", 0);
String str = String.valueOf(password);
text1.setText(username);
text2.setText(str);
}
2、main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="" >
</EditText>
<EditText
android:id="@+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="" >
</EditText>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button1" >
</Button>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button2" >
</Button>
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button3" >
</Button>
</LinearLayout>
3、运行效果