1
------------------------------------------------ ---------- Xmlコード------------------------------------- -----------------------どのようにPreferenceFragment ..のアクションバーで戻るボタンを設定しましたか?
<?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コード----------- -------------------------------------------------
あなたはonCreatにコードActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
に従うことによって、戻るボタンを使用可能に設定している
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);
}
}
}
メソッドgetSupportActionBar()を解決できません:..? –
あなたのアクティビティがPreferenceFragmentを継承しているので、この関数はAppCompatActivityに属します – Harmantj
ええ、私はあなたにカスタムやその他のものを作成するような解決策があることを承知しています –