【问题标题】:Toggle Checkbox by clicking row layout通过单击行布局来切换复​​选框
【发布时间】:2014-11-15 16:35:27
【问题描述】:

我有一个自定义的 LinearLayout 视图,其中只有一个 TextView 和一个 CheckBox。 I can't seem to allow the checkbox to be toggled when any part of the layout is selected.我四处搜索,我看到的大部分内容是将 CheckBox 上的可聚焦属性设置为我已经完成的 false。

我有一个带有两个文本视图的类似组件,并且 onClick 有效。

布局:

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:descendantFocusability="beforeDescendants"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/probe_item_label"
        style="@style/Probe.Item.Text.Label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="left"
        android:layout_marginBottom="3dp"
        android:layout_weight="0" />

    <View
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_weight="1" />

    <CheckBox
        android:id="@+id/probe_item_checkbox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:layout_weight="0"
        android:focusable="false"
        android:focusableInTouchMode="false" />

</LinearLayout>

类:

public class ProbeItemCheckBox extends LinearLayout
{
   LinearLayout mLayout = null;
   TextView mLabelTextView = null;
   CheckBox mCheckbox = null;

   Context mContext = null;

   public ProbeItemCheckBox(Context context)
   {
      super(context);
   }
   public ProbeItemCheckBox(Context context, AttributeSet attrs)
   {
      super(context, attrs);

      mContext = context;

      TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ProbeText);

      final String labelText = a.getString(R.styleable.ProbeText_labelText);

      String service = Context.LAYOUT_INFLATER_SERVICE;
      LayoutInflater li = (LayoutInflater) getContext().getSystemService(service);

      mLayout = (LinearLayout) li.inflate(R.layout.probe_item_checkbox, this, true);

    //  layout.setDescendantFocusability(FOCUS_BEFORE_DESCENDANTS);

      mLabelTextView = (TextView)mLayout.findViewById(R.id.probe_item_label);
      mCheckbox = (CheckBox)mLayout.findViewById(R.id.probe_item_checkbox);

      mCheckbox.setFocusable(false);
      mCheckbox.setFocusableInTouchMode(false);

      mLabelTextView.setText(labelText);
      mLabelTextView.setClickable(false);

      mCheckbox.setClickable(false);
      mLayout.setClickable(true);

      mLayout.setOnClickListener(new OnClickListener()
      {
         @Override
         public void onClick(View v)
         {
            mCheckbox.toggle();   

         }

      });
      a.recycle();
   }
...
}

如果您能提供任何帮助,我们将不胜感激。

【问题讨论】:

  • 切换是什么意思?
  • @Chitrang Toggle 表示如果该行中的复选框未选中,单击该行中的任意位置将选中该框,取消选中也是如此。

标签: android checkbox focus


【解决方案1】:

可能在 setOnClickListener 中将 new OnClickListener() 更改为 new View.OnClickListener()。

参考:how to set onClick method with linearLayout?

【讨论】:

  • 谢谢,但这没有用。它已经将 OnClickListener 用于 View 类,但只是为了好玩,我添加了它并没有帮助。
【解决方案2】:

我让它工作了。我必须通过 ID 在 xml 文件中找到顶级 LinearLayout 并在该对象上设置 onClickListener。

在布局文件中添加ID:

LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/probe_item_checkbox_item_layout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/btn_settings"
    android:descendantFocusability="beforeDescendants"
    android:orientation="horizontal"
    style="@style/Probe.Item.Layout" >

然后在java文件中通过ID查找:

  mLayout = (LinearLayout) li.inflate(R.layout.probe_item_checkbox, this, true);

  mLayout2 = (LinearLayout) findViewById(R.id.probe_item_checkbox_item_layout);

在 mLayout2 上设置点击监听器

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-14
    • 2015-02-20
    • 1970-01-01
    • 2011-05-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多