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
- android SharedPreferences 的使用
- Android SharedPreferences的使用
- Android SharedPreferences的使用
- Android SharedPreferences的使用
- android SharedPreferences的使用
- Android SharedPreferences的使用
- Android SharedPreferences的使用
- Android SharedPreferences的使用
- android SharedPreferences 的使用
- Android SharedPreferences的使用
- Android SharedPreferences的使用
- Android SharedPreferences的使用
- Android SharedPreferences的使用
- android SharedPreferences 的使用
- android的SharedPreferences使用
- Android SharedPreferences的使用
- Android SharedPreferences的使用
- Android SharedPreferences的使用
- jxl.read.biff.biffexception: the input file was not found
- 编程小回忆二
- 新手学习SEO是否有用?
- CentOS登录窗口 不能使用
- 基础总结篇:创建及调用自己的ContentProvider
- android的SharedPreferences使用
- iOS内存管理之:引用计数、ARC、自动释放池autoreleasepool和便捷方法之间的关系
- 端端Clouduolc的云成员用户图标颜色的含义
- CURL编译
- 父对话框和子对话框之间的传值
- 如何选择设置有效的关键词
- 编程小回忆三
- Win7系统中修改MAC地址的几种方法
- Android应用程序的debug属性