【发布时间】: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 表示如果该行中的复选框未选中,单击该行中的任意位置将选中该框,取消选中也是如此。