如何从ATTR参考格式类型得到一个字符串? [英] How to get a string from attr with reference format type?

查看:230
本文介绍了如何从ATTR参考格式类型得到一个字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的自定义 attr.xml 中,我指定的文档申报,设置样式

I have my custom attr.xml document in which I specified declare-styleable:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <declare-styleable name="EditTextValidateablePreference">
        <attr name="errorMessage" format="reference" />
    </declare-styleable>

</resources>

然后在布局我设置:

Then in layout I set:

<com.xx.yy.EditTextValidateablePreference
    ...
    ns:errorMessage="@string/validation_email_mail_server_invalid"
    />

而在 EditTextValidateable preference.class 我得到它:

    String validatorErrorMessage = attrs.getAttributeValue(PREFERENCE_NS, "errorMessage");

validatorErrorMessage 具有类似值: @ 2131099700

怎样才能得到它的整数值与使用它:

How can I get its integer value to use it with:

context.getResources().getString(messageId)

谢谢!

推荐答案

有我建议你参考一个优秀的笼统的回答:<一href=\"http://stackoverflow.com/questions/2695646/declaring-a-custom-android-ui-element-using-xml\">Declaring一个定制的Andr​​oid使用的UI元素XML

There's an excellent general answer which I recommend you to refer to: Declaring a custom android UI element using XML

在特别的,你应该使用<一个href=\"http://developer.android.com/reference/android/content/Context.html#obtainStyledAttributes%28android.util.AttributeSet,%20int%5b%5d%29\">Context.obtainStyledAttributes(AttributeSet集,INT [] ATTRS)和<一个href=\"http://developer.android.com/reference/android/content/res/TypedArray.html#getString%28int%29\">TypedArray.getString(int指数),而不是的AttributeSet.getAttributeValue(...)的:

In particular, you should use Context.obtainStyledAttributes(AttributeSet set, int[] attrs) and TypedArray.getString(int index) instead of AttributeSet.getAttributeValue(...):

TypedArray ta = activityContext.obtainStyledAttributes(attrs, R.styleable.EditTextValidateablePreference);
String theString = ta.getString(R.styleable.EditTextValidateablePreference_errorMessage);
ta.recycle();

这篇关于如何从ATTR参考格式类型得到一个字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆