当前位置:   article > 正文

PreferenceGroup中RingtonePreference的用法

preferencegroup
1.使用PerferenceActivity或者PerferenceFragment实现。
2.在创建布局时,选择PreferenceScreen;
PreferenceGroup:显示多个选项的容器,是PreferenceScreen和PreferenceCategory的父类;
PreferenceScreen:必须是一个PreferenceActivity/PreferenceFragment的根标签 ,也可用于其他Preference中。用于根标签时是不可见的,当用于其他偏好层级中时,是可见的,但是它所包含的子层级是不可见,点击它会跳转到
另一个Screen中,并显示它的子层级。
PreferenceCategory:用于给一组选项设置不可点击的标题;
ListPreference:以对话框的形式显示一系列词目的Preference;
CheckBoxPreference:提供了复选框功能的Preference;
DialogPreference: 提供了对话框功能的Preference;
EditTextPreference:DialogPreference的子类,加入了EditText的功能;
3。xml主要属性:
android:orderingFromXml:是否按照xml文件中的顺序,若设置此属性,必须是false或者true。
android:key:存储设置选项的键;
android:summary:选项的主题;
android:summary:选项标题;
4.实例:
xml文件:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
  3. <PreferenceCategory
  4. android:title="铃声设置">
  5. <RingtonePreference
  6. android:title="提示音"
  7. android:summary="默认铃声"
  8. android:key="key_ringtone_preference"
  9. android:ringtoneType="ringtone"/>
  10. </PreferenceCategory>
  11. </PreferenceScreen>
java代码:
MainActivity.java中:
  1. public class MainActivity extends Activity implements OnClickListener {
  2. private Button button;
  3. @Override
  4. protected void onCreate(Bundle savedInstanceState) {
  5. super.onCreate(savedInstanceState);
  6. setContentView(R.layout.activity_main);
  7. button = (Button) findViewById(R.id.button_settings);
  8. button.setOnClickListener(this);
  9. }
  10. @Override
  11. public void onClick(View v) {
  12. // TODO Auto-generated method stub
  13. getFragmentManager().beginTransaction().replace(R.id.containner, new SettingPreference())
  14. .commit();
  15. }
  16. }
PreferenceFragment.java中:
  1. public class SettingPreference extends PreferenceFragment implements OnPreferenceChangeListener{
  2. private static Activity activity;
  3. private RingtonePreference mRingtone;
  4. private PreferenceScreen mPreferenceScreen;
  5. private static final String KEY_RINGTONE = "key_ringtone_preference";
  6. @Override
  7. public void onCreate(Bundle savedInstanceState) {
  8. // TODO Auto-generated method stub
  9. super.onCreate(savedInstanceState);
  10. addPreferencesFromResource(R.xml.generalpreference);
  11. activity = getActivity();
  12. mPreferenceScreen = getPreferenceScreen();
  13. mRingtone = (RingtonePreference) mPreferenceScreen.findPreference(KEY_RINGTONE);
  14. String ringtoneUri = getRingtonePreference();
  15. String ringtone = getRingtoneTitleFromUri(ringtoneUri);
  16. mRingtone.setSummary(ringtone == null? "":ringtone);
  17. //设置监听事件
  18. mRingtone.setOnPreferenceChangeListener(this);
  19. }
  20. /**
  21. * 首选项值改变时回调
  22. */
  23. @Override
  24. public boolean onPreferenceChange(Preference preference, Object newValue) {
  25. // TODO Auto-generated method stub
  26. if(preference == mRingtone){
  27. Toast.makeText(getActivity(), "newValue="+newValue, Toast.LENGTH_SHORT).show();
  28. setRingtonePreference((String)newValue);
  29. String ringtongTitle = getRingtoneTitleFromUri((String)newValue);
  30. mRingtone.setSummary(ringtongTitle);
  31. }
  32. return true;
  33. }
  34. /**
  35. * 得到铃声的Uri
  36. * @return
  37. */
  38. public static String getRingtonePreference(){
  39. SharedPreferences prefs = activity.getSharedPreferences("SP", Context.MODE_PRIVATE);
  40. String ringtone = prefs.getString(KEY_RINGTONE, null);
  41. return ringtone;
  42. }
  43. /**
  44. * 将铃声的Uri进行存储
  45. * @param str
  46. */
  47. public static void setRingtonePreference(String str){
  48. SharedPreferences prefs = activity.getSharedPreferences("SP", Context.MODE_PRIVATE);
  49. prefs.edit().putString(KEY_RINGTONE, str).commit();
  50. }
  51. /**
  52. * 获取铃声名
  53. * @param uri
  54. * @return
  55. */
  56. public static String getRingtoneTitleFromUri(String uri){
  57. if(TextUtils.isEmpty(uri)){
  58. return null;
  59. }
  60. Ringtone ringtone = RingtoneManager.getRingtone(activity, Uri.parse(uri));
  61. String ringtoneTitle = ringtone.getTitle(activity);
  62. return ringtoneTitle;
  63. }
  64. }
运行效果:




声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/152964
推荐阅读
相关标签
  

闽ICP备14008679号