简体   繁体   English

我如何在 PreferenceFragment 的操作栏中设置后退按钮..?

[英]How i set Back Button In Action Bar in PreferenceFragment ..?

----------------------------------------------------------Xml-Code------------------------------------------------------------ -------------------------------------------------- --------Xml-代码------------------------------ ---------------------

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory android:title="Notification Settings">
    <SwitchPreference
        android:defaultValue="true"
        android:key="notification_state"
        android:summary="Notification is Enabled"
        android:title="Show Notification" />
    <Preference
        android:key="reset_data"
        android:summary="All data will be removed"
        android:title="Reset Data" />
</PreferenceCategory>
</PreferenceScreen>

---------------------------------------------------------Java Code------------------------------------------------------------ -------------------------------------------------- -------Java代码----------------------------------------- -------------------

import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceManager;
import android.support.v4.app.NavUtils;
import android.support.v7.app.AlertDialog;
import android.view.MenuItem;
import android.widget.Toast;

import com.internet.services.DataService;
import com.github.machinarius.preferencefragment.PreferenceFragment;


public class SettingsActivity extends PreferenceFragment {
    SharedPreferences dataPref;

    public void onStart() {
        super.onStart();
        dataPref = PreferenceManager.getDefaultSharedPreferences(getActivity());
    }


    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preferences);

        findPreference("notification_state").setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
            @Override
            public boolean onPreferenceChange(Preference preference, Object newValue) {
                boolean isNotificationOn = (Boolean) newValue;
                Editor edit = dataPref.edit();
                edit.putBoolean("notification_state", isNotificationOn);
                edit.apply();
                if (isNotificationOn) {
                    findPreference("notification_state").setSummary("Notification is Enabled");
                } else {
                    findPreference("notification_state").setSummary("Notification is Disabled");
                }
                return true;
            }
        });
        findPreference("reset_data").setOnPreferenceClickListener(new OnPreferenceClickListener() {
            @Override
            public boolean onPreferenceClick(Preference preference) {
                AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                builder.setMessage("All Data Will Be Clear!").setCancelable(false).setPositiveButton("Yes", new OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        SharedPreferences sp_today = getActivity().getSharedPreferences(DataService.TODAY_DATA, 0);
                        SharedPreferences sp_month = getActivity().getSharedPreferences(DataService.MONTH_DATA, 0);
                        Editor editor = sp_today.edit();
                        Editor edito2 = sp_month.edit();
                        editor.clear();
                        edito2.clear();
                        editor.apply();
                        edito2.apply();
                        Toast.makeText(getActivity(), "Data Removed", Toast.LENGTH_LONG).show();
                    }
                }).setNegativeButton("No", new OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    }
                });
                AlertDialog alert = builder.create();
                alert.setTitle("Do You Want To Reset Data?");
                alert.show();
                return true;
            }
        });
    }

    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case 16908332:
                NavUtils.navigateUpFromSameTask(getActivity());
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }


}

You have set enable back button by following code您已通过以下代码设置启用后退按钮

 ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);

in onCreate() method and what to on clicking that button following code在 onCreate() 方法中以及单击该按钮后的代码如下

 @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == android.R.id.home) {
            finish();
        } 
        return super.onOptionsItemSelected(item);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android:如何在后退按钮操作栏和设备的后退按钮上保留对象的状态 - Android: How can I retain the state of an object on back button Action bar and back button of device 如何覆盖android中的操作栏后退按钮? - how to override action bar back button in android? 如何通过Java代码设置操作栏按钮图标 - How Can I set Action bar button icon by Java code 动作栏上的“返回”按钮-Android。 如何“回头”? - “Back” button on Action bar - Android . How to go “back”? 如何在不启用操作栏上的后退按钮图标的情况下启用手机的后退按钮到 go 回到上一个活动? - How to enable back button of phone to go back to previous activity without enabling back button icon on action bar? 如何在已打开活动的操作栏中向片段添加后退按钮? - How to add a back button to the fragment in the action bar of an opened activity? 如何使Android操作栏后退按钮返回片段 - How to have android action bar back button return to fragment 从操作栏中删除后退箭头按钮 - To remove the back arrow button from Action Bar 操作栏中的“后退”按钮不起作用 - Back button in action bar doesn't work 如何通过后退操作栏按钮 go 回到活动而不重新创建父活动 - How to go back to a activity through the back action bar button without recreating the Parent activity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM