crear una DialogPreference desde XML


He estado tratando de usar un android.preference.DialogPreference inflado de XML, pero la documentación parece estar faltando algunos bits esenciales, y no puedo encontrar un ejemplo de trabajo en ninguna parte. Mi XML ahora se ve así (probé muchas permutaciones, pero esto parece un mínimo razonable):

<DialogPreference
android:key="funThing" android:title="Fun Thing"
android:dialogLayout="@layout/fun_layout"
android:positiveButtonText="OK"
android:negativeButtonText="Cancel"
/>

Mi suposición en este punto es que se requiere subclase DialogPreference y no usarlo directamente. Por un lado, no puedo encontrar una manera de asociar el valor de preferencia real con un elemento en el diálogo mismo, lo que al reflexionar es una especie de regalo. Y también, mirando la fuente para DialogPreference.java parece confirmarlo. Y también acabo de notar que la documentación oficial se refiere a ella como una "clase base". Pero al menos, sería bueno establecer una fuente lo suficientemente definitiva en la red que ayudaría a la siguiente gente N averiguar esto más rápido de lo que hice.

Para el registro, el archivo de registro se ve así:

I/ActivityManager(   61): Starting: Intent { cmp=org.jeremy.android/.PreferencesActivity } from pid 2755
W/Resources( 2755): Converting to string: TypedValue{t=0x10/d=0x4b0 a=-1}
W/Resources( 2755): Converting to string: TypedValue{t=0x10/d=0x20 a=-1}
D/AndroidRuntime( 2755): Shutting down VM
W/dalvikvm( 2755): threadid=1: thread exiting with uncaught exception (group=0x40015560)
E/AndroidRuntime( 2755): FATAL EXCEPTION: main
E/AndroidRuntime( 2755): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.jeremy.android/org.jeremy.android.PreferencesActivity}: android.view.InflateException: Binary XML file line #28: Error inflating class java.lang.reflect.Constructor
E/AndroidRuntime( 2755):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
E/AndroidRuntime( 2755):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
E/AndroidRuntime( 2755):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
E/AndroidRuntime( 2755):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
E/AndroidRuntime( 2755):    at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime( 2755):    at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime( 2755):    at android.app.ActivityThread.main(ActivityThread.java:3683)
E/AndroidRuntime( 2755):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime( 2755):    at java.lang.reflect.Method.invoke(Method.java:507)
E/AndroidRuntime( 2755):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
E/AndroidRuntime( 2755):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
E/AndroidRuntime( 2755):    at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime( 2755): Caused by: android.view.InflateException: Binary XML file line #28: Error inflating class java.lang.reflect.Constructor
E/AndroidRuntime( 2755):    at android.preference.GenericInflater.createItem(GenericInflater.java:397)
E/AndroidRuntime( 2755):    at android.preference.GenericInflater.onCreateItem(GenericInflater.java:417)
E/AndroidRuntime( 2755):    at android.preference.GenericInflater.createItemFromTag(GenericInflater.java:428)
E/AndroidRuntime( 2755):    at android.preference.GenericInflater.rInflate(GenericInflater.java:481)
E/AndroidRuntime( 2755):    at android.preference.GenericInflater.rInflate(GenericInflater.java:493)
E/AndroidRuntime( 2755):    at android.preference.GenericInflater.inflate(GenericInflater.java:326)
E/AndroidRuntime( 2755):    at android.preference.GenericInflater.inflate(GenericInflater.java:263)
E/AndroidRuntime( 2755):    at android.preference.PreferenceManager.inflateFromResource(PreferenceManager.java:251)
E/AndroidRuntime( 2755):    at android.preference.PreferenceActivity.addPreferencesFromResource(PreferenceActivity.java:262)
E/AndroidRuntime( 2755):    at org.jeremy.android.PreferencesActivity.onCreate(PreferencesActivity.java:40)
E/AndroidRuntime( 2755):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
E/AndroidRuntime( 2755):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
E/AndroidRuntime( 2755):    ... 11 more
E/AndroidRuntime( 2755): Caused by: java.lang.InstantiationException: android.preference.DialogPreference
E/AndroidRuntime( 2755):    at java.lang.reflect.Constructor.constructNative(Native Method)
E/AndroidRuntime( 2755):    at java.lang.reflect.Constructor.newInstance(Constructor.java:415)
E/AndroidRuntime( 2755):    at android.preference.GenericInflater.createItem(GenericInflater.java:383)
E/AndroidRuntime( 2755):    ... 22 more
W/ActivityManager(   61):   Force finishing activity org.jeremy.android/.PreferencesActivity
W/ActivityManager(   61):   Force finishing activity org.jeremy.android/.SplashActivity
Author: Jeremy, 2011-03-20

3 answers

Este es raro, necesitas subclase DialogPreference. La subclase no necesita hacer nada. So

public class MyDialogPreference extends DialogPreference {

    public MyDialogPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
    }

} 

Puede ser instanciado. Mientras que un avión viejo DialogPreference no puede. Muy raro, deberían ser exactamente lo mismo.

 19
Author: Marc Holder Kluver,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-06-20 06:12:33

Aquí hay un ejemplo de cómo usar la preferencia de diálogo (subclase como mencionaste).

package dk.myapp.views;

import android.content.Context;
import android.preference.DialogPreference;
import android.util.AttributeSet;

/**
 * The OptionDialogPreference will display a dialog, and will persist the
 * <code>true</code> when pressing the positive button and <code>false</code>
 * otherwise. It will persist to the android:key specified in xml-preference.
 */
public class OptionDialogPreference extends DialogPreference {

    public OptionDialogPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onDialogClosed(boolean positiveResult) {
        super.onDialogClosed(positiveResult);
        persistBoolean(positiveResult);
    }

}

Las preferencias.xml debe contener

<dk.myapp.views.OptionDialogPreference
            android:key="@string/prefKeyResetQuests"
            android:dialogIcon="@android:drawable/ic_dialog_alert"
            android:title="Reset Quests"
            android:summary="Reset all quest-progress."
            android:dialogMessage="Are you sure you wish to reset your quest progress? This action cannot be undone!"
            android:positiveButtonText="Clear Quests"
            android:negativeButtonText="Cancel"/>

Tengo un valor res/que contiene (aunque el nombre-clave también se puede declarar explícitamente arriba).

<string name="prefKeyResetQuests">resetQuests</string>  

My PreferenceActivity hace el restablecimiento real de onPause. Tenga en cuenta que onStop puede ser demasiado tarde ya que el onStop no siempre se llamará inmediatamente después de presionar back:

@Override
public void onPause() {
    SharedPreferences prefs = android.preference.PreferenceManager.
        getDefaultSharedPreferences(getBaseContext());
    if(prefs.getBoolean(
        getResources().getString(R.string.prefKeyResetQuests), false)) {
        // apply reset, and then set the pref-value back to false
    }
}

O equivalentemente, ya que todavía estamos en la actividad de preferencia:

@Override
protected void onPause() {
    Preference prefResetQuests =
        findPreference(getResources().getString(R.string.prefKeyResetQuests));
    if(prefResetQuests.getSharedPreferences().
        getBoolean(prefResetQuests.getKey(), false)){
        // apply reset, and then set the pref-value back to false 
    }
}
 41
Author: arberg,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-01-21 09:59:14

Primero:

Crea tu propia clase que se extiende DialogPreference como abajo:

package com.test.view;

import android.preference.DialogPreference;

public class DialogExPreference extends DialogPreference 
{
    public DialogExPreference(Context oContext, AttributeSet attrs)
    {
        super(oContext, attrs);     
    }
}

Segundo:

Modifique el archivo xml como abajo:

<PreferenceScreen
    xmlns:android="http://schemas.android.com/apk/res/android">

    <com.test.view.DialogExPreference
        android:title="@string/title"
        android:dialogMessage="@string/lite"              
                android:negativeButtonText="test"/> 

</PreferenceScreen>

Entonces está bien.

 15
Author: SKf,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-03-21 07:39:43