----------------------------------------------- ----------- 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代码-------------------------------------- ----------------------
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);
}
}
}
答案 0 :(得分:1)
您已通过以下代码
设置启用后退按钮 ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
onCreate()方法中的以及在代码
之后单击该按钮的内容 @Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
finish();
}
return super.onOptionsItemSelected(item);
}