2

我已经用 Xamarin 在 C# 中实现了这个自定义 DialogPreference,但是我在将它添加到我的 xml 文件时遇到了问题。

namespace Client.Android
{
public class TimePreference : DialogPreference
{
    private int lastHour = 0;
    private int lastMinute = 0;
    private TimePicker picker = null;

    public static int GetHour(string time)
    {
        string[] pieces = time.Split (':');
        return Convert.ToInt32 (pieces [0]);
    }

    public static int GetMinute(string time)
    {
        string[] pieces = time.Split (':');
        return Convert.ToInt32 (pieces [1]);
    }

    public TimePreference(Context context, IAttributeSet attSet) : base(context, attSet)
    {
        SetPositiveButtonText (Resource.String.time_preference_set);
        SetNegativeButtonText (Resource.String.time_preference_cancel);
    }

    protected override View OnCreateDialogView ()
    {
        picker = new TimePicker (Context);
        return picker;
    }

    protected override void OnBindDialogView (View view)
    {
        base.OnBindDialogView (view);
        picker.CurrentHour = (Java.Lang.Integer) lastHour;
        picker.CurrentMinute = (Java.Lang.Integer) lastMinute;
    }

    protected override void OnDialogClosed (bool positiveResult)
    {
        base.OnDialogClosed (positiveResult);

        if (positiveResult) {
            lastHour = (int) picker.CurrentHour;
            lastMinute = (int) picker.CurrentMinute;

            string time = lastHour + ":" + lastMinute;

            if (CallChangeListener (time)) {
                PersistString (time);
            }
        }
    }

    protected override Java.Lang.Object OnGetDefaultValue (TypedArray a, int index)
    {
        return a.GetString (index);
    }

    protected override void OnSetInitialValue (bool restorePersistedValue, Java.Lang.Object defaultValue)
    {
        string time = string.Empty;

        if (restorePersistedValue) {
            if (defaultValue == null) {
                time = GetPersistedString ("00:00");
            } else {
                time = GetPersistedString (defaultValue.ToString ());
            }
        } else {
            time = defaultValue.ToString ();
        }

        lastHour = GetHour (time);
        lastMinute = GetMinute (time);
    }
}
}

我的项目名称/默认命名空间是“Client.Android”,我尝试将它添加到我的 xml 文件中,如下所示:

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
    <PreferenceCategory android:title="@string/pref_foo" >
    ...

    <Client.Android.TimePreference
         android:title="@string/pref_bar"
         android:key="@string/prefkey_bar"
         android:defaultValue="12:00" />
    ...
    </PreferenceCategory>
</PreferenceScreen>

AddPreferencesFromResource(Resource.Xml.settings);但是我的方法出现以下异常OnCreate

    Android.Views.InflateException: Binary XML file line #32: Error inflating class Client.Android.TimePreference
  at Android.Runtime.JNIEnv.CallNonvirtualVoidMethod (intptr,intptr,intptr,Android.Runtime.JValue[]) [0x00084] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.12-series/7f0e3d3c/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.g.cs:895
  at Android.Preferences.PreferenceActivity.AddPreferencesFromResource (int) [0x00070] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.12-series/7f0e3d3c/source/monodroid/src/Mono.Android/platforms/android-19/src/generated/Android.Preferences.PreferenceActivity.cs:729
  at Client.Android.SettingsActivity.OnCreate (Android.OS.Bundle) [0x00015] in c:\pushtotalk\Client.Android\SettingsActivity.cs:45
  at Android.App.Activity.n_OnCreate_Landroid_os_Bundle_ (intptr,intptr,intptr) [0x00011] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.12-series/7f0e3d3c/source/monodroid/src/Mono.Android/platforms/android-19/src/generated/Android.App.Activity.cs:2178
  at at (wrapper dynamic-method) object.7c9f4d4d-9496-4803-b019-cd453beeee8b (intptr,intptr,intptr) <IL 0x00017, 0x00043>
  at --- End of managed exception stack trace ---
  at android.view.InflateException: Binary XML file line #32: Error inflating class Client.Android.TimePreference
  at at android.preference.GenericInflater.createItemFromTag(GenericInflater.java:441)
  at at android.preference.GenericInflater.rInflate(GenericInflater.java:481)
  at at android.preference.GenericInflater.rInflate(GenericInflater.java:493)
  at at android.preference.GenericInflater.inflate(GenericInflater.java:326)
  at at android.preference.GenericInflater.inflate(GenericInflater.java:263)
  at at android.preference.PreferenceManager.inflateFromResource(PreferenceManager.java:272)
  at at android.preference.PreferenceActivity.addPreferencesFromResource(PreferenceActivity.java:1472)
  at at client.android.SettingsActivity.n_onCreate(Native Method)
  at at client.android.SettingsActivity.onCreate(SettingsActivity.java:32)
  at at android.app.Activity.performCreate(Activity.java:5231)
  at at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
  at at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
  at at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
  at at android.app.ActivityThread.access$800(ActivityThread.java:135)
  at at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
  at at android.os.Handler.dispatchMessage(Handler.java:102)
  at at android.os.Looper.loop(Looper.java:136)
  at at android.app.ActivityThread.main(ActivityThread.java:5017)
  at at java.lang.reflect.Method.invokeNative(Native Method)
  at at java.lang.reflect.Method.invoke(Method.java:515)
  at at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
  at at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
  at at dalvik.system.NativeStart.main(Native Method)
  at Caused by: java.lang.ClassNotFoundException: Didn't find class "Client.Android.TimePreference" on path: DexPathList[[zip file "/data/app/Client.Android-1.apk"],nativeLibraryDirectories=[/data/app-lib/Client.Android-1, /vendor/lib, /system/lib]]
  at at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
  at at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
  at at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
  at at android.preference.GenericInflater.createItem(GenericInflater.java:375)
  at at android.preference.GenericInflater.createItemFromTag(GenericInflater.java:430)
  at ... 22 more
4

1 回答 1

4

这些是可用的默认属性(您还可以通过展开android 文档中Inherited XML Attributes的块来查看继承的属性):DialogPreference

android:dialogIcon - The icon for the dialog. 
android:dialogLayout - A layout to be used as the content View for the dialog. 
android:dialogMessage - The message in the dialog. 
android:dialogTitle - The title in the dialog. 
android:negativeButtonText - The negative button text for the dialog. 
android:positiveButtonText - The positive button text for the dialog. 

因此,为了使用您自己的属性,您需要创建自己的Resource文件,在其中为您的like定义Stylables :DialogPreference

资源\值\MyTimePreferenceAttrs.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="MyTimePreference">
        <attr name="defaultValue" format="string"/>
    </declare-styleable>
</resources>

然后在您创建 TimePreference 视图的 AXML 布局中,您需要添加一个命名空间,以便可以解析它们:

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:my="http://schemas.android.com/apk/res-auto" >
    <PreferenceCategory android:title="@string/pref_foo" >
    ...

    <Client.Android.TimePreference
         my:defaultValue="12:00" />
    ...
    </PreferenceCategory>
</PreferenceScreen>

然后在您的构造函数中,TimePreference您可以通过以下方式获取值:

var ta = context.ObtainStyledAttributes(attrs, Resource.Styleable.MyTimePreference);
var defaultValue = ta.GetString(Resource.Styleable.MyTimePreference_defaultValue);
ta.Recycle();

在 XML 布局中使用大写名称空间时,您也可能会遇到问题。但是类名保持大写,所以:

<client.android.TimePreference
    ...
于 2014-04-25T15:03:25.337 回答