android - 循环通过id访问资源

标签 android

我在布局中有一长串 EditText 元素,实际上这些元素用于获取矩阵输入,矩阵大小可以变化 3x3 5x5 .. .

元素的 id 是像 11,12,13,21,22,23 这样的系列模式 ... 现在有没有一种方法可以在循环中使用它们,比如

for(int i=0;i<10;i++)
a = findViewById("R.id.kernel1"+i);

当然,目前上面的简单连接是行不通的,但是有什么办法吗?

<LinearLayout
android:id="@+id/convolutionMatrix"
android:layout_width="@dimen/matrixLayoutWidthHeight"
android:layout_height="fill_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_margin="@dimen/marginOne"
android:background="@color/white1"
android:padding="@dimen/marginOne" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:padding="@dimen/marginOne"
    android:text="@string/matrixTitle"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<EditText
    android:id="@+id/kernel11"
    android:layout_width="@dimen/matrixBoxWidthHeight"
    android:layout_height="@dimen/matrixBoxWidthHeight"
    android:inputType="numberSigned" >

    <requestFocus />
</EditText>

<EditText
    android:id="@+id/kernel12"
    android:layout_width="@dimen/matrixBoxWidthHeight"
    android:layout_height="@dimen/matrixBoxWidthHeight"
    android:layout_alignParentLeft="true"
    android:inputType="numberSigned" >
</EditText>

<EditText
    android:id="@+id/kernel13"
    android:layout_width="@dimen/matrixBoxWidthHeight"
    android:layout_height="@dimen/matrixBoxWidthHeight"
    android:inputType="numberSigned" />

<EditText
    android:id="@+id/kernel14"
    android:layout_width="@dimen/matrixBoxWidthHeight"
    android:layout_height="@dimen/matrixBoxWidthHeight"
    android:inputType="numberSigned" />

<EditText
    android:id="@+id/kernel15"
    android:layout_width="@dimen/matrixBoxWidthHeight"
    android:layout_height="@dimen/matrixBoxWidthHeight"
    android:inputType="numberSigned" />

<EditText
    android:id="@+id/kernel21"
    android:layout_width="@dimen/matrixBoxWidthHeight"
    android:layout_height="@dimen/matrixBoxWidthHeight"
    android:inputType="numberSigned" />

<EditText
    android:id="@+id/kernel22"
    android:layout_width="@dimen/matrixBoxWidthHeight"
    android:layout_height="@dimen/matrixBoxWidthHeight"
    android:inputType="numberSigned" />

<EditText
    android:id="@+id/kernel23"
    android:layout_width="@dimen/matrixBoxWidthHeight"
    android:layout_height="@dimen/matrixBoxWidthHeight"
    android:inputType="numberSigned" />

<EditText
    android:id="@+id/kernel24"
    android:layout_width="@dimen/matrixBoxWidthHeight"
    android:layout_height="@dimen/matrixBoxWidthHeight"
    android:inputType="numberSigned" />

<EditText
    android:id="@+id/kernel25"
    android:layout_width="@dimen/matrixBoxWidthHeight"
    android:layout_height="@dimen/matrixBoxWidthHeight"
    android:inputType="numberSigned" />

<EditText
    android:id="@+id/kernel31"
    android:layout_width="@dimen/matrixBoxWidthHeight"
    android:layout_height="@dimen/matrixBoxWidthHeight"
    android:inputType="numberSigned" />

<EditText
    android:id="@+id/kernel32"
    android:layout_width="@dimen/matrixBoxWidthHeight"
    android:layout_height="@dimen/matrixBoxWidthHeight"
    android:inputType="numberSigned" />

<EditText
    android:id="@+id/kernel33"
    android:layout_width="@dimen/matrixBoxWidthHeight"
    android:layout_height="@dimen/matrixBoxWidthHeight"
    android:inputType="numberSigned" />

<EditText
    android:id="@+id/kernel34"
    android:layout_width="@dimen/matrixBoxWidthHeight"
    android:layout_height="@dimen/matrixBoxWidthHeight"
    android:inputType="numberSigned" />

<EditText
    android:id="@+id/kernel35"
    android:layout_width="@dimen/matrixBoxWidthHeight"
    android:layout_height="@dimen/matrixBoxWidthHeight"
    android:inputType="numberSigned" />

<EditText
    android:id="@+id/kernel41"
    android:layout_width="@dimen/matrixBoxWidthHeight"
    android:layout_height="@dimen/matrixBoxWidthHeight"
    android:inputType="numberSigned" />

<EditText
    android:id="@+id/kernel42"
    android:layout_width="@dimen/matrixBoxWidthHeight"
    android:layout_height="@dimen/matrixBoxWidthHeight"
    android:inputType="numberSigned" />

<EditText
    android:id="@+id/kernel43"
    android:layout_width="@dimen/matrixBoxWidthHeight"
    android:layout_height="@dimen/matrixBoxWidthHeight"
    android:inputType="numberSigned" />

<EditText
    android:id="@+id/kernel44"
    android:layout_width="@dimen/matrixBoxWidthHeight"
    android:layout_height="@dimen/matrixBoxWidthHeight"
    android:inputType="numberSigned" />

<EditText
    android:id="@+id/kernel45"
    android:layout_width="@dimen/matrixBoxWidthHeight"
    android:layout_height="@dimen/matrixBoxWidthHeight"
    android:inputType="numberSigned" />

<EditText
    android:id="@+id/kernel51"
    android:layout_width="@dimen/matrixBoxWidthHeight"
    android:layout_height="@dimen/matrixBoxWidthHeight"
    android:inputType="numberSigned" />

<EditText
    android:id="@+id/kernel52"
    android:layout_width="@dimen/matrixBoxWidthHeight"
    android:layout_height="@dimen/matrixBoxWidthHeight"
    android:inputType="numberSigned" />

<EditText
    android:id="@+id/kernel53"
    android:layout_width="@dimen/matrixBoxWidthHeight"
    android:layout_height="@dimen/matrixBoxWidthHeight"
    android:inputType="numberSigned" />

<EditText
    android:id="@+id/kernel54"
    android:layout_width="@dimen/matrixBoxWidthHeight"
    android:layout_height="@dimen/matrixBoxWidthHeight"
    android:inputType="numberSigned" />

<EditText
    android:id="@+id/kernel55"
    android:layout_width="@dimen/matrixBoxWidthHeight"
    android:layout_height="@dimen/matrixBoxWidthHeight"
    android:inputType="numberSigned" />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Apply Kernel Mask" />

最佳答案

我就是这样做的。我需要阅读的地方有 6 种不同的资源。

  for (int i = 0; i < 6; i++ ){
          String fname = "p" + i;
          int id = context.getResources().getIdentifier(fname, "drawable", "com.example.yourproject");
          if (id == 0) {
              Log.e(TAG, "Lookup id for resource '"+fname+"' failed");
              // graceful error handling code here
          }
         scoresBm[i] =  (Bitmap) BitmapFactory.decodeResource(context.getResources(), id);

  }

关于android - 循环通过id访问资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19799224/

相关文章:

Android ActionBar/Toolbar 在我的应用程序中消失了

android - 什么可能导致 TextView 上的 setText 出现 ANR?

android - 在android中播放相同的声音而无需等待上一个结束

java - 读取两个相同元素后停止读取剩余元素(Android、Java)

java - android变量空指针

android - 设置 View 背景导致 OutOfMemoryError

javascript - 如何更改 Phonegaps 条码扫描仪插件的外观

android - 如何在通知组单击时打开非启动器 Activity

android - 如何保存在 webview 中显示的图像?

java - 将泛型转换为其子类