安卓中使用SharedPreferences实现软件参数的保存和从另外一个资源获取保存结果

使用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);


 



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值