【发布时间】:2012-11-29 03:17:46
【问题描述】:
我正在实现我自己的自定义 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 中。
我的问题是: 我当前的代码不允许在 XML 中将 dialogMessage XML 属性指定为字符串资源 ID。
换句话说,这是可行的:
android:dialogMessage="Hello world!"
但这不是:
android:dialogMessage="@string/hello_world"
如果我在 XML 中将其指定为资源 ID,则资源 id 将保存到 mDialogMsg,而不是字符串资源本身。现在,我知道我可以做到:
context.getString(attrs.getAttributeValue(androidns, "dialogMessage"))
但随后用户将无法在 XML 中输入普通字符串(即非资源 ID)。我想给用户两种选择。我该怎么做?
【问题讨论】:
标签: android android-layout attributes android-xml android-custom-view