zoukankan      html  css  js  c++  java
  • android学习十三 首选项

    1,首选项可用用来持久保存用户设置,游戏最高分等

    2,首选项有,列表首选项,复选框首选项,对话框首选项..
    3,通过xml文件和代码创建首选项
         addPreferencesFromResource(R.xml.main)
    4.从首选项中获取值
       Resources resources = this.getResources();
                  SharedPreferences spf= PreferenceManager.getDefaultSharedPreferences( this);
                  String option = spf.getString(resources.getString(R.string.FLIGHT_SORT_KEY),
                                resources.getString(R.string.FLIGHT_SORT_DEFAULT_VALUE ));
                  
                  String[] optionArray = resources.getStringArray(R.array.filght_sort_option );
    5.判断是否存在某个首选项的值
     
    boolean hasPreferences = myPrefs.getBoolean(INITIALIZED, false);
    if(hasPreferences) {
    Log.v("Preferences", "We've been called before");
    // Read other values as desired from preferences file...
    someString = myPrefs.getString("someString", "");
    }
    else {
    Log.v("Preferences", "First time ever being called");
    // Set up initial values for what will end up
    // in the preferences file
    someString = "some default value";
    }
    // Later when ready to write out values
     
    6.在代码中设置首选项的值
    Editor editor = myPrefs.edit();
    CHAPTER 13: Working with Preferences and Saving State  359
    editor.putBoolean(INITIALIZED, true);
    editor.putString("someString", someString);
    // Write other values as desired
    editor.commit();
     
     
    7.首选项可用嵌套,可以存在父子关系。
     
     
     
     
    8一般步骤
     
      a.在xml文件中布局首选项
      b.创建活动继承自PreferenceActivity,然后通过
       addPreferencesFromResource() 创建视图
      c.通过相应的方法获取和设置值
       
  • 相关阅读:
    C语言点滴
    随便记点什么
    STL的使用
    Linux下OTG支持USB摄像头
    socket编程实战-调试
    socket编程实战-bind端口占用问题
    socket编程实战-tcp_tw_recycle问题
    socket编程实战-connect超时问题
    BT[3]-BLE广播详解
    BT[2]-BLE初体验:心率计
  • 原文地址:https://www.cnblogs.com/manziluo/p/5800466.html
Copyright © 2011-2022 走看看