如何在android中创建带工具栏的设置

时间:2017-09-29 04:48:39

标签: java android android-studio settings preferencefragment

我有一个设置首选项屏幕。它有一个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

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

将appcompact设计支持库添加到build.gradle

compile 'com.android.support:appcompat-v7:21.0.3'

将toolbar.xml添加到布局文件夹

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
 xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/ColorPrimary"
    android:elevation="4dp"
    >
</android.support.v7.widget.Toolbar>

然后在activity.xml中包含工具栏

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    tools:context=".MainActivity">

    <include
        android:id="@+id/tool_bar"
        layout="@layout/tool_bar"
        ></include>

    <TextView
        android:layout_below="@+id/tool_bar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/TextDimTop"
        android:text="@string/hello_world" />

</RelativeLayout>

然后你需要在你的活动中设置它

toolbar = (Toolbar) findViewById(R.id.tool_bar); 
setSupportActionBar(toolbar); 

然后使用onCreateOptionsMenu,您可以在其上添加设置菜单。