【问题标题】:How to create Settings with toolbar in android如何在android中使用工具栏创建设置
【发布时间】:2018-03-10 22:55:52
【问题描述】:

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

【问题讨论】:

  • 你怎么控制不住?

标签: java android android-studio settings preferencefragment


【解决方案1】:

将 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 可以在其上添加设置菜单。

【讨论】:

  • 感谢您的回复@ysl。我的 MainActivity 和其他活动中已经有一个工具栏。问题是当我通过单击选项菜单中的“设置”进入设置屏幕时,只有没有工具栏。现在如上所述,如果我在我的activity_main.xml 中包含toolbar.xml,我以前的包含应用程序名称的工具栏就消失了,设置工具栏占据了它的位置
  • 如果您使用 Activity 来显示设置,那么如果您想为所有屏幕显示工具栏,那么它的正常行为则使用片段
  • 我正在使用片段进行设置。我已经发布了我的 settingsactivity.java 类。请查看并告诉我必须做什么
  • 在这种情况下,您必须在 SettingsActivity 中包含工具栏,或者您可以将用户重定向到 settingsFragment 而不是 SettingActivity
  • 我不知道该怎么做。你能帮忙吗?
【解决方案2】:

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

【讨论】:

  • 你能告诉我怎么做吗?我是这个领域的新手。@Umar
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-06-21
  • 2021-04-27
  • 2016-06-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多