質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

Kotlin

Kotlinは、ジェットブレインズ社のアンドリー・ブレスラフ、ドミトリー・ジェメロフが開発した、 静的型付けのオブジェクト指向プログラミング言語です。

Q&A

解決済

1回答

1041閲覧

linearLayoutの中に入っているTextViewだけを取得

sena14

総合スコア109

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

Kotlin

Kotlinは、ジェットブレインズ社のアンドリー・ブレスラフ、ドミトリー・ジェメロフが開発した、 静的型付けのオブジェクト指向プログラミング言語です。

0グッド

0クリップ

投稿2022/05/14 20:24

LinearLayoutの中に入っているTextViewだけを取得したいです。
getChildAt()を使って無理やり取得することは出来そうですが階層が変わった時にコードを変更する必要があったりと複雑になってしまうかと思っております。

レイアウトは下記の様な形でidがparentに設定してあるLinearLayout内のTextViewを全て(直下だけではなく孫?の階層までの全て)を取得したいです。

<LinearLayout android:id="@+id/parent" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"> <TextView android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1"/> <Button android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1"/> <TextView android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"> <Button android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1"/> <TextView android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1"/> <Button android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"> <TextView android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1"/> <Button android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1"/> <TextView android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"> <Button android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1"/> <TextView android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1"/> <Button android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1"/> </LinearLayout> </LinearLayout>

何か良い方法はありますでしょうか?よろしくお願い致します。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

とはいえ、結局は、getChildAt() を使って無理やり取得することになるような気もしますが・・・

※ 動かしてみたわけではありませんので、あくまでも「イメージ」として捉えてください

Java

1 private static void sample(View root) { 2 3 List<TextView> list = new ArrayList<>(); 4 5 LinearLayout parent = root.requireViewById(R.id.parent); 6 collectTextView(parent, list); 7 } 8 9 public static void collectTextView(ViewGroup layout, List<TextView> list) { 10 11 int count = layout.getChildCount(); 12 13 for (int i = 0; i < count; i ++) { 14 15 View child = layout.getChildAt(i); 16 17 if (child instanceof TextView) { 18 19 list.add((TextView)child); 20 continue; 21 } 22 23 if (child instanceof ViewGroup) { 24 25 collectTextView((ViewGroup)child, list); 26 continue; 27 } 28 } 29 }

投稿2022/05/15 01:55

momodx

総合スコア185

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問