Android保存之SharedPreferences
2015-08-01 11:03
381 查看
Android中一共有四种存储方式:
SharedPreferences 为其中的一种,具体还是看代码:
效果图如下:
SharedPreferences 为其中的一种,具体还是看代码:
package com.wyl.preferencetest; import android.support.v7.app.ActionBarActivity; import android.support.v7.app.ActionBar; import android.support.v4.app.Fragment; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.os.Bundle; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.Toast; import android.os.Build; import android.preference.PreferenceManager; public class MainActivity extends ActionBarActivity { SharedPreferences pref ; String username; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //1 获取到preference的两种方式: // pref = PreferenceManager.getDefaultSharedPreferences(MainActivity.this); pref = getSharedPreferences("myOwn", MODE_PRIVATE); //2.使pref可编辑,获得一个可编辑对象 Editor editor = pref.edit(); editor.putString("yourname", "wyl"); editor.putInt("age", 26); //3提交编辑,否则不成效的 editor.commit(); editor.putBoolean("flag", true); editor.putBoolean("flag2", true); editor.remove("flag"); editor.commit(); String myname = pref.getString("yourname","yourname"); System.out.println("myname:"+myname); MainActivity.this.username = myname; } public void btnOclick(View v){ if(R.id.btn01==v.getId()){ System.out.println("======"+this.username); // Toast.makeText(MainActivity.this, username+",你登陆成功了", 1000).show();//一定要加.show()方法 Toast.makeText(MainActivity.this, "this is the toast content", Toast.LENGTH_LONG).show();// } } }
效果图如下:
相关文章推荐
- ubuntu14.04 安装HAXM(KVM)提升android虚拟机Android x8运行速度
- Android studio 模拟器没有键盘的解决方法
- Android官方命令深入分析之Hierarchy Viewer
- Android官方命令深入分析之Hierarchy Viewer
- android注意事项,小技巧
- Android开发中 ic_launcher 尺寸
- 46、android studio第一次使用时卡在gradle下载怎么解决?
- android用getIdentifier()获取资源Id
- Android官方命令深入分析之etc1tool
- Android官方命令深入分析之etc1tool
- Android:相机调用与Activity生命周期
- Android图片转换
- android学习之android logat 调试及信息过滤
- 在ubuntu kylin 编译cubieboard2 的android镜像碰到的问题
- Android动画总结
- Android官方命令深入分析之虚拟机
- Android官方命令深入分析之虚拟机
- Android studio 重置 DDMS面板布局
- EventBus 《二》 Android EventBus的简单使用
- Android 清除应用缓存