编程ID添加到R.id [英] programatically add id to R.id

查看:141
本文介绍了编程ID添加到R.id的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个的EditText 对象,然后我尝试在一个单元测试引用。什么是添加一个新的 ID的最佳方式 R.id 此动态创建的对象,这样我就可以通过 findViewById()在单元测试以后引用呢?

I am creating an EditText object which I then try to reference in a unit test. What is the best way to add a new id to R.id for this dynamically created object, so that I can later reference it via findViewById() in the unit test?

推荐答案

您可以设置的ID稍后将在R.id类使用XML资源文件中使用,并让在编译期间Android SDK中给他们独特的价值观。

You can set ID's you'll use later in R.id class using an xml resource file, and let Android SDK give them unique values during compile time.

RES /价值/ ids.xml

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

    <item name="my_edit_text_1" type="id"/>
    <item name="my_button_1" type="id"/>
    <item name="my_time_picker_1" type="id"/>

</resources>

要使用它在code:

myEditTextView.setId(R.id.my_edit_text_1);

这篇关于编程ID添加到R.id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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