android的SharedPreferences使用

来源:互联网 发布:淘宝嘉年华海报尺寸 编辑:程序博客网 时间:2024/04/28 09:46

需求:模拟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;@Overrideprotected 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() {@Overridepublic 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 0
原创粉丝点击