我有一个设置首选项屏幕,它有一个ListPreference和一个CheckBoxPreference。选择ListPreference的项目时,我想更改应用程序的日期格式。另外,我想通过CheckBoxPreference在状态栏上显示/隐藏通知。任何人都可以告诉我要实现这一目标需要做什么。

另外,如何将工具栏添加到首选项屏幕?我被困在这里。请帮忙。提前致谢。
我被困在这里。请帮忙。
提前致谢。

MainActivity.java

public void setCurrentDateOnView() {
    String dateFormat = "dd - MM - yyyy";
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dateFormat, Locale.US);
    tv_Current_Date.setText(simpleDateFormat.format(calendar_now.getTime()));
    String short_weekday = new DateFormatSymbols().getShortWeekdays()[day_of_current_week];
    tv_Current_weekday.setText(short_weekday);
    til_Current_Date.setError(null);
}

public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {

        case R.id.action_settings:
            Intent intent_settings = new Intent(this, SettingsActivity.class);
            startActivity(intent_settings);
            Toast.makeText(this, "You have clicked on settings action menu.", Toast.LENGTH_SHORT).show();
            break;

    }
    return super.onOptionsItemSelected(item);
}


SettingsActivity.java

public class SettingsActivity extends PreferenceActivity
    implements SharedPreferences.OnSharedPreferenceChangeListener {

NotificationManager mNotifyManager;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //Display the fragment as the main content.
    getFragmentManager().beginTransaction()
            .replace(android.R.id.content, new SettingsFragment())
            .commit();
    PreferenceManager.setDefaultValues(this, R.xml.preferences, false);

    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);

    boolean notifyEnabled = sharedPreferences.getBoolean("pref_cb_notification", true);
    mNotifyManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    if (notifyEnabled) {
        //Show notification
        showNotification();
    }
    else {
        //Hide notification
        hideNotification();
    }
}

@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {

        boolean isChecked = sharedPreferences.getBoolean("pref_cb_notification", false);
        if (isChecked) {
            //Show notification
            showNotification();
        }
        else {
            //Hide notification
            hideNotification();
        }

}

public static class SettingsFragment extends PreferenceFragment implements SharedPreferences.OnSharedPreferenceChangeListener{

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //Load the preferences from an XML resource
        addPreferencesFromResource(R.xml.preferences);
    }

    @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {

    }
}

//Method to show notification
public void showNotification() {
    NotificationCompat.Builder mBuilder = (NotificationCompat.Builder)
            new NotificationCompat.Builder(SettingsActivity.this)
                    .setSmallIcon(R.drawable.ic_notifications_white_24dp)
                    .setContentTitle("My Application")
                    .setSubText("Tap to start");
    Intent resultIntent = new Intent(SettingsActivity.this, MainActivity.class);
    PendingIntent resultPendingIntent = PendingIntent
            .getActivity(SettingsActivity.this, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    //System.currentTimeMillis();
    mBuilder.setContentIntent(resultPendingIntent);
    Notification notification = mBuilder.build();
    notification.flags |= Notification.FLAG_NO_CLEAR | Notification.FLAG_ONGOING_EVENT;
    //notification.flags |= Notification.FLAG_AUTO_CANCEL;
    mNotifyManager.notify(1, notification);
}

//Method to hide notification
public void hideNotification() {
    mNotifyManager.cancel(1);
}


}

Settings image

最佳答案

要添加工具栏,您只需要在活动的布局文件中使用协调器布局即可。偏好活动具有与其他活动一样的简单布局,您只需在容器内填充偏好片段。

07-24 09:47
查看更多