android高手进阶教程七之android中preferences的使用!

android高手进阶教程七之android中preferences的使用!

ID:33535044

大小:60.50 KB

页数:4页

时间:2019-02-26

android高手进阶教程七之android中preferences的使用!_第1页
android高手进阶教程七之android中preferences的使用!_第2页
android高手进阶教程七之android中preferences的使用!_第3页
android高手进阶教程七之android中preferences的使用!_第4页
资源描述:

《android高手进阶教程七之android中preferences的使用!》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库

1、大家好,我们这一节讲的是AndroidPreferences的学习,Preferences在Android当中被用来记录应用,以及用户喜好等等,它可以用来保存简单的数据类型,如Int,Double,Boolean等。Preferences中保存的数据可以理解为Map型。我们通过PreferenceManager以及getDefaultSharedPreferences(Context)来获取它,比如当我们想获得整数我们可以用getInt(Stringkey,intdefVal).获取里面的某个键值,当我们想修改时候我们用putInt(Stri

2、ngkey,intnewVal),最后用edit(),方法提交!千万不要忘记了哦~为了让大家跟好的理解我做了一个简单的Demo,程序主要有个TextView控件,上面写着用户使用改应用的次数。效果如下图所示:下面是实现Demo的大体步骤:一、新建一个Android工程命名为:PreferencesDemo。二、在修改main.xml布局文件,这里只是在TextView控件里加了一个id.代码如下:1viewplaincopytoclipboardprint?23

3、arLayoutxmlns:android="http://schemas.android.com/apk/res/android"4android:orientation="vertical"5android:layout_width="fill_parent"6android:layout_height="fill_parent"7>814127

5、iew8android:id="@+id/text"9android:layout_width="fill_parent"10android:layout_height="wrap_content"11android:text="@string/hello"12/>13三、修改PreferenceDemo.java的代码,全部代码如下:14viewplaincopytoclipboardprint?15packagecom.android.tutor;16importandroid.app.Activity;17

6、importandroid.content.SharedPreferences;18importandroid.os.Bundle;19importandroid.preference.PreferenceManager;20importandroid.widget.TextView;21publicclassPreferencesDemoextendsActivity{22/**Calledwhentheactivityisfirstcreated.*/23@Override24publicvoidonCreate(BundlesavedI

7、nstanceState){25super.onCreate(savedInstanceState);26setContentView(R.layout.main);272829SharedPreferencesmPerferences=PreferenceManager30.getDefaultSharedPreferences(this);3132intcounter=mPerferences.getInt("counter",0);3334TextViewmTextView=(TextView)findViewById(R.id.tex

8、t);3536mTextView.setText("Thisapphasbeenstarted"+counter+"times.");3738SharedPrefe

当前文档最多预览五页,下载文档查看全文

此文档下载收益归作者所有

当前文档最多预览五页,下载文档查看全文
温馨提示:
1. 部分包含数学公式或PPT动画的文件,查看预览时可能会显示错乱或异常,文件下载后无此问题,请放心下载。
2. 本文档由用户上传,版权归属用户,天天文库负责整理代发布。如果您对本文档版权有争议请及时联系客服。
3. 下载前请仔细阅读文档内容,确认文档内容符合您的需求后进行下载,若出现内容与标题不符可向本站投诉处理。
4. 下载文档时可能由于网络波动等原因无法下载或下载错误,付费完成后未能成功下载的用户请联系客服处理。