質問
ラジオボタンをテーブル形式で表示したかったために、How to group a 3x3 grid of radio buttons?をそのまま持ってきてレイアウトはできたのですが、この作成したviewのsetOnClickListenerが取得できません。
該当のソースコード
java
1public class ToggleButtonGroupTableLayout extends TableLayout implements OnClickListener { 2 3 private static final String TAG = "ToggleButtonGroupTableLayout"; 4 private RadioButton activeRadioButton; 5 6 /** 7 * @param context 8 */ 9 public ToggleButtonGroupTableLayout(Context context) { 10 super(context); 11 // TODO Auto-generated constructor stub 12 } 13 14 /** 15 * @param context 16 * @param attrs 17 */ 18 public ToggleButtonGroupTableLayout(Context context, AttributeSet attrs) { 19 super(context, attrs); 20 // TODO Auto-generated constructor stub 21 } 22 23 @Override 24 public void onClick(View v) { 25 final RadioButton rb = (RadioButton) v; 26 if ( activeRadioButton != null ) { 27 activeRadioButton.setChecked(false); 28 } 29 rb.setChecked(true); 30 activeRadioButton = rb; 31 } 32 33 /* (non-Javadoc) 34 * @see android.widget.TableLayout#addView(android.view.View, int, android.view.ViewGroup.LayoutParams) 35 */ 36 @Override 37 public void addView(View child, int index, 38 android.view.ViewGroup.LayoutParams params) { 39 super.addView(child, index, params); 40 setChildrenOnClickListener((TableRow)child); 41 } 42 43 44 /* (non-Javadoc) 45 * @see android.widget.TableLayout#addView(android.view.View, android.view.ViewGroup.LayoutParams) 46 */ 47 @Override 48 public void addView(View child, android.view.ViewGroup.LayoutParams params) { 49 super.addView(child, params); 50 setChildrenOnClickListener((TableRow)child); 51 } 52 53 54 private void setChildrenOnClickListener(TableRow tr) { 55 final int c = tr.getChildCount(); 56 for (int i=0; i < c; i++) { 57 final View v = tr.getChildAt(i); 58 if ( v instanceof RadioButton ) { 59 v.setOnClickListener(this); 60 } 61 } 62 } 63 64 public int getCheckedRadioButtonId() { 65 if ( activeRadioButton != null ) { 66 return activeRadioButton.getId(); 67 } 68 69 return -1; 70 } 71}
kotlin
1class Add : Fragment() { 2 3 (略) 4 5 override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, 6 savedInstanceState: Bundle?): View? { 7 // Inflate the layout for this fragment 8 var view= inflater.inflate(R.layout.fragment_sample, container, false) 9 val rGrp= view.findViewById<ToggleButtonGroupTableLayout>(R.id.rGrp)\ 10 11 rGrp.setOnClickListener { 12 Log.d("log", "" + rGrp.checkedRadioButtonId) 13 } 14 15 return view 16 } 17 18 (略) 19} 20
回答2件
あなたの回答
tips
プレビュー