2014-07-26 13 views
1

我已经在我的应用程序中设置了首选项屏幕作为设置选项,但是我对如何在用户选择或更改选项时完成更改完全感到困惑。如何使事情上的首做被改变?

这里是我的PreferenceScreen的XML代码:

<?xml version="1.0" encoding="utf-8"?> 
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" > 

    <PreferenceCategory android:title="Notifications"> 
     <RingtonePreference 
      android:summary="Choose a Ringtone" 
      android:title="Notification Ringtone" 
      android:key="ringtonePref" /> 
     <ListPreference 
      android:title="Notification Timer" 
      android:summary="Select when to Notify" 
      android:dialogTitle="Show Notification after every:" 
      android:positiveButtonText="OK" 
      android:negativeButtonText="Cancel" 
      android:entries="@array/entries" 
      android:entryValues="@array/entries" 
      android:key="listPrefs" /> 
     <ListPreference 
      android:title="LED Color" 
      android:summary="Choose LED Color" 
      android:positiveButtonText="OK" 
      android:negativeButtonText="Cancel" 
      android:entries="@array/colors" 
      android:entryValues="@array/colors" 
      android:key="listPrefs2" /> 
     <CheckBoxPreference 
      android:title="LED" 
      android:defaultValue="true" 
      android:summary="Flash LED or Not" 
      android:key="checkBoxPrefs2"/> 
     <CheckBoxPreference 
      android:title="Vibrate" 
      android:defaultValue="true" 
      android:summary="Vibrate on receiving Notification" 
      android:key="checkBoxPrefs3" /> 
    </PreferenceCategory> 

    <PreferenceCategory android:title="Invite"> 
     <Preference android:summary="Send invitation to more peoples so that\nthey can also Learn more and more Duas."/> 
    </PreferenceCategory> 

    <PreferenceCategory android:title="Version"> 
     <Preference 
      android:key="versionPrefs" 
      android:summary="Version 1.0\nThank you for Downloading the App." /> 
    </PreferenceCategory> 

</PreferenceScreen> 

这里是我Settings.class:

public class Settings extends PreferenceActivity implements OnPreferenceChangeListener{ 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     addPreferencesFromResource(R.xml.settings); 

     //setting the properties of Action Bar 
     ActionBar actionBar = getActionBar(); 
     actionBar.setTitle(Html.fromHtml("<font color='#FFFFFF'><strong>Settings</strong></font>")); 
     actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#33B5E5"))); 
     actionBar.setDisplayHomeAsUpEnabled(true); 

     PreferenceManager.setDefaultValues(this, R.xml.settings, true); 
    } 

    @Override 
    public boolean onPreferenceChange(Preference preference, Object newValue) { 
     // TODO Auto-generated method stub 
     return false; 
    } 
} 

我不知道我要补充什么码onPreferenceChange让我的偏好实际上做的改变。

请告诉我。

任何帮助将是非常赞赏

感谢您的宝贵的时间

+0

http://developer.android.com/reference/android/preference/package-summary.html –

+0

@Hammad纳西尔如果妳想要保存UR设置使用SharedPreferences请查看以下链接的http:// developer.android.com/reference/android/content/SharedPreferences.html –

+0

@shylendra我想要的东西就像是假设有一个振动选项,如果用户检查它会比振动发生,如果用户将取消它的振动不会发生。请帮我解决一下这个。 –

回答

1

振动

CheckBoxPreference checkBoxPrefs3 = (CheckBoxPreference) findPreference("checkBoxPrefs3"); 
    checkBoxPrefs3.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { 
     @Override 
     public boolean onPreferenceChange(Preference preference, Object o) { 
      if ((Boolean) o) { 
       checkBoxPrefs3.setChecked(true); 
       // vibration for 800 milliseconds 
       ((Vibrator)getSystemService(VIBRATOR_SERVICE)).vibrate(800); 
       // TODO vibrate 
      } else { 
       checkBoxPrefs3.setChecked(false); 
      } 
      return false; 
     } 
    }); 

须获得许可以AndroidManifest.xml文件中添加:

<uses-permission android:name="android.permission.VIBRATE" /> 

如何申请您的应用程序?

SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context); 
boolean vibrate = sharedPrefs.getBoolean("checkBoxPrefs3", true); 

Notification notification = new Notification(icon, 
        "message", when); 
      notification.defaults |= Notification.DEFAULT_SOUND; 
      if (vibrate) { 
        notification.defaults |= Notification.DEFAULT_VIBRATE; 
      } 

      notification.setLatestEventInfo(mContext, 
        "ticker", 
        "message", contentIntent); 
      notification.flags = Notification.FLAG_AUTO_CANCEL; 
      notificationManager.notify(count, notification); 
+0

你没有让我兄弟,我想设置这是我的应用程序的设置,当用户**检查**振动复选框时,设备将在收到通知时震动,如果用户**取消选中**收到通知时设备不会振动._请帮助**。 –

+0

完成.. :)很好的答案!...非常感谢! –

+0

**感谢您的回答**兄弟,它**工作!**现在请告诉我有关_ListPreferences_也。我已经在我的问题中发布了,在那里我已经给出了一个选项,用户在多少小时后才想知道信息。前 - 每2小时或4小时后等。 _因此,如果用户选择2小时**应用程序应该如何每2小时后通知用户?再次感谢。请帮忙,这也**。 –

0

Android将其自动保存到您的SharedPreferences。 你总是可以通过调用得到所需的值:

SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(getContext()); 
boolean led = sharedPrefs.getBoolean("checkBoxPrefs2", true); 

checkBoxPrefs2关键是在你的布局定义,true为默认值。

+0

感谢您的回答。但我想要的东西就像是假设有一个振动选项,如果用户检查它,而不是震动发生,如果用户不检查振动,振动不会发生。请在这件事上给予我帮助。 –

1

在您PreferenceActivity

  • 初始化您的CheckBoxPreference
  • 添加OnPreferenceChangeListener
  • 做任何你想做的变化..

下面是一个例子:

CheckBoxPreference checkBoxPrefs3 = (CheckBoxPreference) findPreference("checkBoxPrefs3"); 
    checkBoxPrefs3.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { 
     @Override 
     public boolean onPreferenceChange(Preference preference, Object o) { 
      if ((Boolean) o) { 
       checkBoxPrefs3.setChecked(true); 
       // TODO vibrate 
      } else { 
       checkBoxPrefs3.setChecked(false); 
      } 
      return false; 
     } 
    }); 
+0

感谢您的回答,但请简单说明一下。在这里做什么:'// TODO振动'。 _这里写的代码是为了在**检查时振动**而不在**选中时振动** ._ –

+0

请帮忙。 –

+0

看看http://stackoverflow.com/a/13950364/3289311 – mroczis

相关问题