1

私は Android の世界に慣れていないので、疑問があります。main.xml で作成した ID の名前を教えてくれる方法はありますか? たとえば、私はこれを持っています:

main.xml

<TextView android:id="@+id/text1"
       android:layout_width="70px"
       android:layout_height="70px"
       android:text="Google"
       />

<TextView android:id="@+id/text2"
       android:layout_width="70px"
       android:layout_height="70px"
       android:text="As"
       />

そして、私が欲しいのは2つのTextViewからのID名です.この例でIDを与えるクラス.javaで使用できるメソッドはありますか? この場合、(text1 と text2) が必要です。

ありがとう、私の英語を許してください。

4

2 に答える 2

5

親レイアウトに id を設定し、次のようなことを試してください。

    LinearLayout ll = findViewById(R.id.yourLayout);
    for(int i=0; i<ll.getChildCount(); i++){
        View v = ll.getChildAt(i);
        int idView = v.getId();
            //Do something with idView
    }
于 2010-04-06T09:50:41.820 に答える
0

ファイルを膨らませる

linflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View main = linflater.inflate(R.layout.main, null);

次に findViewById を実行して TextView をオブジェクトに変換します

TextView text1 = (TextView)main.findViewById(R.id.text1);

次に、このように後でオブジェクト text1 を操作できます

text1.setText("foo");
于 2010-04-06T09:48:40.773 に答える