我正在实现我自己的自定义 DialogPreference 子类,如下所示:
public class MyCustomPreference extends DialogPreference
{
private static final String androidns = "http://schemas.android.com/apk/res/android";
private String mDialogMsg;
public MyCustomPreference(Context context, AttributeSet attrs)
{
super(context, attrs);
mDialogMsg = attrs.getAttributeValue(androidns, "dialogMessage");
...
}
...
}
如您所见,我获取了dialogMessage
XML 属性并将其保存在成员变量mDialogMsg
中。
我的问题是:我当前的代码不允许将dialogMessage
XML 属性指定为 XML 中的字符串资源 ID。
换句话说,这有效:
android:dialogMessage="Hello world!"
但这不会:
android:dialogMessage="@string/hello_world"
如果我在 XML 中将其指定为资源 id,则资源id将保存到mDialogMsg
,而不是字符串资源本身。现在,我知道我可以这样做:
context.getString(attrs.getAttributeValue(androidns, "dialogMessage"))
但随后用户将无法在 XML 中输入普通字符串(即非资源 ID)。我想给用户两种选择。我该怎么做呢?