【问题标题】:Use of numeric ids in Activity xml description and by code在活动 xml 描述和代码中使用数字 ID
【发布时间】:2018-09-10 05:15:01
【问题描述】:

我在xml中使用id:

Button
        android:text="Button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" tools:layout_editor_absoluteY="438dp"
        tools:layout_editor_absoluteX="110dp" android:id="@+id/a11"/>

然后,我想通过迭代所有按钮来检索 Java 代码中的对象。以前,我使用数字 id 并这样做:

  for (int i = 20; i < 24; i++) {
        RadioButton radioButton = findViewById(i);

但这是不正确的。它在 R 生成的类中给出错误。

public static final int 11=0x49279429;

所以我在数字 id 前加了一个字母:“a11”

但是如何在循环中检索 Button:

findViewById(R.id.(loop increment variable ????)

【问题讨论】:

    标签: java android android-activity view


    【解决方案1】:

    你可以用这个

    int Id = getResources().getIdentifier("radioButtonId" ,"id",getPackageName());
    

    获取id并使用

     RadioButton radioButton = findViewById(Id);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-03-14
      • 1970-01-01
      • 2015-01-22
      • 2020-10-21
      • 1970-01-01
      • 2016-02-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多