android的SharedPreferences使用

需求:模拟android系统应用设置中飞行模式的开启与关闭,并记住设置的状态

布局文件:fragment_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/rl"
    android:layout_width="match_parent"
    android:layout_height="70dip"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.lenovo.relay.MainActivity$PlaceholderFragment" >
    <TextView
        android:id="@+id/tv_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/airplane" />
    <TextView
        android:id="@+id/tv_content"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/tv_title"
        android:text="@string/content" />
      <CheckBox
        android:id="@+id/cb"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:focusable="false"
        android:clickable="false"/>
</RelativeLayout>
资源文件strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">layout</string>
    <string name="action_settings">Settings</string>
    <string name="airplane">飞行模式</string>
    <string name="content">关闭网络连接</string>
    <string name="ari_open">飞行模式已开启</string>
    <string name="ari_close">飞行模式已关闭</string>
</resources>
MainActivity.java

public class MainActivity extends Activity {
	private TextView tv_content;
	private CheckBox cb;
	private RelativeLayout rl;
	private SharedPreferences sp;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.fragment_main);

		sp = getSharedPreferences("config", MODE_PRIVATE);//将需要记录的数据保存在config.xml文件中
		// sp =  getPreferences(MODE_PRIVATE); 自动以当前activity.java文件的类名 作为.xml文件的名称
		boolean flymode = sp.getBoolean("flymode", false);

		tv_content = (TextView) findViewById(R.id.tv_content);
		cb=(CheckBox) findViewById(R.id.cb);
		rl = (RelativeLayout) findViewById(R.id.rl);

		if (flymode) {
			tv_content.setText(R.string.ari_open);
			cb.setChecked(true);
		} else {
			tv_content.setText(R.string.ari_close);
			cb.setChecked(false);
		}
		rl.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if (cb.isChecked()) {
					cb.setChecked(false);
					tv_content.setText(R.string.ari_close);
					Editor editor = sp.edit();
					editor.putBoolean("flymode", false);
					editor.commit();
				} else {
					cb.setChecked(true);
					tv_content.setText(R.string.ari_open);
					Editor editor = sp.edit();
					editor.putBoolean("flymode", true);
					editor.commit();
				}
			}
		});
		
/*		cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if(isChecked){
					tv_content.setText(R.string.ari_open);
					Editor editor = sp.edit();
					editor.putBoolean("flymode", false);
					editor.commit();
				}else{
					tv_content.setText(R.string.ari_close);
					Editor editor = sp.edit();
					editor.putBoolean("flymode", true);
					editor.commit();
				}
				
			}
		});*/
	}
  }
界面效果如图:



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值