使用sharedPreferenced进行参数的保存与现实的java代码:
public class SharedpreferencesActivity extends Activity {
/** Called when the activity is first created. */
private EditText nameValue,ageValue;
private Button setButton,showButton;
private TextView result;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findViewById();
ButtonListener listener=new ButtonListener();
setButton.setOnClickListener(listener);
showButton.setOnClickListener(listener);
SharedPreferences pre=getSharedPreferences("show", Context.MODE_PRIVATE);
String name=pre.getString("name", "");
int age=pre.getInt("age", 1);
nameValue.setText(name);
//将 int 转换成 String
ageValue.setText(String.valueOf(age));
}
private void findViewById() {
// TODO Auto-generated method stub
nameValue=(EditText)findViewById(R.id.edtiname);
ageValue=(EditText)findViewById(R.id.edtiage);
setButton=(Button)findViewById(R.id.setbutton);
showButton=(Button)findViewById(R.id.showbutton);
result=(TextView)findViewById(R.id.result);
}
class ButtonListener implements OnClickListener{
SharedPreferences pre=getSharedPreferences("show",Context.MODE_PRIVATE);
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Button view=(Button)v;
switch(view.getId()){
case R.id.setbutton:
Editor editor=pre.edit();
editor.putString("name", nameValue.getText().toString());
editor.putInt("age", Integer.parseInt(ageValue.getText().toString()));
editor.commit();
Toast.makeText(SharedpreferencesActivity.this, "参数设置成功", 1).show();
break;
case R.id.showbutton:
String name=pre.getString("name", "");
int age=pre.getInt("age", 1);
result.setText("name="+name+",age="+age);
break;
}
}
}
}
布局文件:
<?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" >
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/name"
android:textSize="20dp" />
<EditText
android:paddingLeft="20dp"
android:id="@+id/edtiname"
android:layout_width="180dp"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/textname"
android:layout_alignTop="@id/textname"
/>
</RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/age"
android:textSize="20dp" />
<EditText
android:paddingLeft="20dp"
android:id="@+id/edtiage"
android:layout_width="180dp"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/textage"
android:layout_alignTop="@id/textage"
/>
</RelativeLayout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/setbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/set"
/>
<Button
android:id="@+id/showbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/show"
/>
</LinearLayout>
<TextView
android:id="@+id/result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFFFFF"
android:textSize="20dp"
/>
</LinearLayout>
在其他包中访问sharedPreferenced 保存的值
Context context=this.createPackageContext(baoming,Context_IGNORE_SECURITY);
SharedPreferences pre=context.getSharedPreferences(name,mode);
pre.getString("name","");
pre.getInt("age",2);