Android 以编程方式获取按钮数组,而无需硬编码id';s并通过findViewById()获取它
我有一个Android 以编程方式获取按钮数组,而无需硬编码id';s并通过findViewById()获取它,android,layout-inflater,findviewbyid,r.id,Android,Layout Inflater,Findviewbyid,R.id,我有一个XML布局,上面有一个EditText,然后在键盘后面有很多按钮和一堆其他视图。根元素是ConstraintLayout <ConstraintLayout ...> <EditText .../> <!-- Here is the list of buttons --> <!-- For placement constraints purposes, I may have id's in the buttons --&
XML
布局,上面有一个EditText
,然后在键盘后面有很多按钮和一堆其他视图。根元素是ConstraintLayout
<ConstraintLayout ...>
<EditText .../>
<!-- Here is the list of buttons -->
<!-- For placement constraints purposes, I may have id's in the buttons -->
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="A"/>
... <!-- About 26 of them -->
<Button .../>
</ConstraintLayout>
你们可以看一看Android的黄油刀库。您只需使用@BindView和视图ID对字段进行注释,Butter knife将自动在布局中转换相应的视图。在我看来,这是一种很好的方法,因为在布局XML文件中仍然可以有一个清晰的结构 下面是一个关于如何在应用程序中使用它的示例
@BindView(R.id.title) TextView title;
@BindView(R.id.subtitle) TextView subtitle;
@BindView(R.id.footer) TextView footer;
要在项目中包括黄油刀,您只需将依赖项添加到应用程序gradle文件中
compile 'com.jakewharton:butterknife:8.8.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
有关更多信息以及详细的安装和使用指南,您可以查看他们的网站:
创建一个仅包含一个按钮的简单button.xml文件。 然后根据需要的次数构建按钮运行时。这就是为什么我们的班级更平
Button button = (Button) LayoutInflater.from(this).inflate(R.layout.button, null);
可以使用以下命令将数字与视图关联: android:tag 为此视图提供一个标记,该标记包含一个字符串,以便稍后使用检索或搜索。通常最好使用id(通过android:id属性)而不是标记,因为它们更快,并且允许编译时类型检查 可以是字符串值,使用“\;”转义字符,如unicode字符的“\n”或“\uxxx” 这样,您就可以记住布局中的所有按钮:
<Button
android:layout_width="wrap_content"
android:tag="0"
android:layout_height="wrap_content"
android:text="A"/>
<Button
android:layout_width="wrap_content"
android:tag="1"
android:layout_height="wrap_content"
android:text="B"/>
...
对于click listener,您可以通过使用getTag()
检查标记来执行以下操作:
您可以通过编程而不是通过xml创建所有按钮。在获得大量关于以编程方式添加按钮的响应后,编辑了描述。以编程方式添加26个按钮并生成
约束链
,将更加复杂。非常感谢您的快速响应。我曾经使用过ButterKnife
,发现它非常有用。但这仍然不能让我的代码感觉健壮(我仍然需要编写26行代码才能生成一个按钮数组)。我期待类似循环的东西,它将循环26次,并为我创建数组。您仍然可以通过编程方式创建按钮并将其添加到视图中。这是您的选择吗?@RahatZaman那么您需要使用我前面提到的Android数据绑定库。@AhmedArnaut是的,这是一个选择。但是,以编程方式在布局中添加按钮并调整设计、位置和其他内容似乎有点困难(因为以编程方式进行操作时,我无法将它们可视化)。我认为这超出了主题的上下文范围,但我需要使这些按钮成为一个ConstraintLayout链
。如果我使用LayoutInflater
以编程方式添加这些按钮,该怎么做?我需要引用在第一个按钮之后以编程方式添加的其他按钮,并且我还需要使用这些按钮的id来添加这些layoutParams。请考虑查看按钮XML代码来了解我真正想要做的事情。@ RaHAT可能我不理解正确,但是您可以从父视图(约束布局)获取上下文。LayoutInflater.from(rootView.getContext())context一词并不是指应用程序的上下文,我只是说这可能超出了我的问题主题。我的意思是,如何使用我在代码中创建的按钮创建约束链
。在数组中传递视图对象,或在带有键和值的条目中传递视图对象,但这是一种不好的做法,因为这可能会导致内存泄漏。我建议使用weakreference来保留您的观点。这就是我想要的。但是我已经找到了一种方法,可以用ids和getIdentifier做同样的事情。
<Button
android:layout_width="wrap_content"
android:tag="0"
android:layout_height="wrap_content"
android:text="A"/>
<Button
android:layout_width="wrap_content"
android:tag="1"
android:layout_height="wrap_content"
android:text="B"/>
...
Button[] buttons = new Button[26];
for(int i = 0; i < 26; i++) {
buttons[i] = findViewByTag(String.valueOf(i));
}
buttonClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
int tag = Integer.parseint(v.getTag());
// do something based on the tag.
}
};