質問

私はAndroid Worldで初めてです。疑いがあります。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)が必要です。

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

役に立ちましたか?

解決

親レイアウトに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
    }

他のヒント

ファイルを膨らませます

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");
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top