0

1 つのアクティビティですべてのトグルボタンをループして、2 つのいずれかを実行しようとしています。値を設定したい場合もあれば、値を読み取りたい場合もあります。これを行う最も簡単な方法は、子をループし、トグルボタンのタイプを確認してから、前述のタスクを実行することですか?

このようなものを使用しますか?

     for (int i = 0; i < rootView.getChildCount(); i++)
       if (Widget_Tag != null){   
       View Current_Widget = (View) rootView.getChildAt(i);          
       String Widget_Tag = (String) Current_Widget.getTag();
       if (Widget_Tag.equals("MyToggleButton")) {
         //do something
       }
     }

** * ***更新* ** * **

次のような作業コード (回答ありがとうございます)、別の for ループをネストして、ToggleButtons の 1 つの子レイヤーを掘り下げました。

    ViewGroup rootView = (ViewGroup) findViewById(R.id.LayoutHere);
    for (int i = 0; i < rootView.getChildCount(); i++) {
      ViewGroup nextLayout = (ViewGroup) rootView.getChildAt(i);
      for (int i2 = 0; i2 < nextLayout.getChildCount(); i2++) {
          View Current_Widget = nextLayout.getChildAt(i2);          
            if(Current_Widget instanceof ToggleButton) 
            {                     
              //Do Something
            }                         
      }         
    }
4

1 に答える 1

1

次のように「MyToggleButton」ToggleButtonでタグを付けた場合、あなたがしていることはうまくいくでしょう:String

ToggleButton button = getSomeToggleButton();
button.setTag("MyToggleButton");

上記のようなことを以前に行ったことがない場合、コードスニペットは機能しません。instanceofそれぞれをチェックすることができますView

if(Current_Widget instanceof ToggleButton) {// or instanceof MyToggleButton if that is a class of yours...
    //do something
}
于 2011-05-29T00:32:55.293 に答える