facebook twitter hatena line email

「Android/PreferenceFragmentCompat」の版間の差分

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
行56: 行56:
 
     public void onCreatePreferences(Bundle bundle, String s) {
 
     public void onCreatePreferences(Bundle bundle, String s) {
 
         setPreferencesFromResource(R.xml.preferences, s);
 
         setPreferencesFromResource(R.xml.preferences, s);
        EditTextPreference editTextPreference = (EditTextPreference) findPreference("zoneId");
 
        editTextPreference.setSummary(getResources().getString(R.string.zone_id_hint));
 
        editTextPreference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
 
            @Override
 
            public boolean onPreferenceChange(Preference preference, Object newValue) {
 
                EditTextPreference editPref = (EditTextPreference)preference;
 
                editPref.setSummary(newValue.toString());
 
                return true;
 
            }
 
        });
 
  
         editTextPreference = (EditTextPreference)findPreference("zoneId");
+
         editTextPreference = (EditTextPreference)findPreference("editTextId");
 
         if (editTextPreference.getText() == null || editTextPreference.getText().length() < 1) {
 
         if (editTextPreference.getText() == null || editTextPreference.getText().length() < 1) {
             editTextPreference.setSummary(getResources().getString(R.string.zone_id_hint));
+
             editTextPreference.setSummary(getResources().getString(R.string.edit_text_hint));
 
         } else {
 
         } else {
 
             editTextPreference.setSummary(editTextPreference.getText());
 
             editTextPreference.setSummary(editTextPreference.getText());

2018年12月27日 (木) 15:16時点における版

Fragmentについて

Fragmentについて理解してない場合はこちら

android/Fragment [ショートカット]

PreferenceFragmentCompatを追加

build.gradleに以下を追加

dependencies {
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support:preference-v7:28.0.0'
}

MainActivity.java

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.kaji.fragmentmyapplication.ui.main.PrefsFragment;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_activity);
        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .replace(R.id.container, PrefsFragment.newInstance())
                    .commitNow();
        }
    }
}

ui/main/PrefsFragment.java


import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.support.v7.preference.PreferenceFragmentCompat;

import com.kaji.fragmentmyapplication.R;

public class PrefsFragment extends PreferenceFragmentCompat {

    public static PrefsFragment newInstance() {
        return new PrefsFragment();
    }
    @Override
    public void onCreatePreferences(Bundle bundle, String s) {
        setPreferencesFromResource(R.xml.preferences, s);

        editTextPreference = (EditTextPreference)findPreference("editTextId");
        if (editTextPreference.getText() == null || editTextPreference.getText().length() < 1) {
            editTextPreference.setSummary(getResources().getString(R.string.edit_text_hint));
        } else {
            editTextPreference.setSummary(editTextPreference.getText());
        }
        editTextPreference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
            @Override
            public boolean onPreferenceClick(Preference preference) {
                EditTextPreference editPref = (EditTextPreference)preference;
                if (editPref.getText() != null) {
                    editPref.setText(editPref.getText());
                }
                return true;
            }
        });
        editTextPreference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
            @Override
            public boolean onPreferenceChange(Preference preference,
                                              Object newValue) {
                EditTextPreference editPref = (EditTextPreference)preference;
                editPref.setSummary(newValue.toString());
                return true;
            }
        });
    }
}

res/layout/preferences.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    <SwitchPreferenceCompat
        android:defaultValue="false"
        android:key="red"
        android:summary="This is red."
        android:title="red" />
    <EditTextPreference
        android:key="editTextId"
        android:title="editTextId"
        android:summary="@string/edit_text_hint"
        android:hint="@string/edit_text_hint"
        />
</PreferenceScreen>

res/values/string.xml

<resources>
    <string name="app_name">FragmentMyApplication</string>
    <string name="hello_world">Hello world!</string>
    <string name="action_settings">Settings</string>
    <string name="edit_text_hint">hint</string>
</resources>

inconvertible types; cannnot cast Preferenceエラーが出る場合

古いPreferenceがあれば削除する

import android.preference.Preference;
import android.preference.EditTextPreference;

新しいPreferenceを追加

import android.support.v7.preference.Preference;
import android.support.v7.preference.EditTextPreference;
Preference screen = (Preference) getPreferenceManager().findPreference("key1");

or

Preference editTextPreference = (Preference) findPreference("key1");

参考

【Android】続・PreferenceFragmentCompatを使った設定画面(kotrin?) http://phicdy.hatenablog.com/entry/setting_ui_by_preference_fragment_compat

PreferenceActivity/PreferenceFragmentを使った設定画面 http://phicdy.hatenablog.com/entry/2015/09/08/%E3%80%90Android%E3%80%91PreferenceActivity/PreferenceFragment%E3%82%92%E4%BD%BF%E3%81%A3%E3%81%9F%E8%A8%AD%E5%AE%9A%E7%94%BB%E9%9D%A2