一种继承自linearlayout的布局可以。在listview中包含checkbox,这时候闹钟的处理时,activity实现一个OnItemClickListener的监听,点击每一项的监听。然后在checkbox单独拿出去写一个类,继承LinearLayout,重写setPressed()这个方法,以实现“当点击checkbox的时候不触发parent的click事件”
/* * Copyright (C) 2009 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.cn.daming.deskclock; import android.content.Context; import android.util.AttributeSet; import android.view.View; import android.widget.LinearLayout; /** * Special class to to allow the parent to be pressed without being pressed * itself. This way the time in the alarm list can be pressed without changing * the background of the indicator. */ public class DontPressWithParentLayout extends LinearLayout { public DontPressWithParentLayout(Context context, AttributeSet attrs) { super(context, attrs); } @Override public void setPressed(boolean pressed) { // If the parent is pressed, do not set to pressed. if (pressed && ((View) getParent()).isPressed()) { return; } super.setPressed(pressed); } }
<?xml version="1.0" encoding="utf-8"?> <!-- Copyright (C) 2008 The Android Open Source Project Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:padding="8dp" android:background="@android:color/white"> <!-- I can't use a normal checkbox here for a couple reasons: 1. The checkbox button resources cannot contain layouts so I have to use 2 views for the clock and the bar. 2. The normal checkbox has a really messed up layout. Using the button attribute produces a left-aligned image that has some kind of minimum height. If the bar is a checkbox, it is too tall and is clipped on the right. --> <com.cn.daming.deskclock.DontPressWithParentLayout android:id="@+id/indicator" style="@style/alarm_list_left_column" android:background="@drawable/clock_selector" android:gravity="center" android:orientation="vertical"> <CheckBox android:id="@+id/clock_onoff" android:focusable="false" android:clickable="false" android:background="@drawable/indicator_clock_onoff" android:duplicateParentState="true" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_gravity="center" android:button="@null" /> <ImageView android:id="@+id/bar_onoff" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:paddingTop="4dip" android:src="@drawable/ic_indicator_off" /> </com.cn.daming.deskclock.DontPressWithParentLayout> <!-- note by wangxianming android:background="?android:attr/windowBackground" --> <ImageView android:src="@drawable/divider_vertical_dark" android:layout_width="wrap_content" android:layout_height="match_parent" android:paddingTop="4dip" android:paddingBottom="4dip" android:scaleType="fitXY" android:gravity="fill_vertical" /> <!-- A layout that displays the time. Shows time, am/pm (if 12-hour), and an optional line below, used for day/days of week --> <com.cn.daming.deskclock.DigitalClock android:id="@+id/digitalClock" android:layout_width="wrap_content" android:layout_height="match_parent" android:gravity="center_vertical" android:layout_weight="1" android:orientation="vertical" android:paddingLeft="16dip" android:paddingRight="16dip"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:baselineAligned="true"> <TextView android:id="@+id/timeDisplay" android:includeFontPadding="false" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingRight="6dip" android:textSize="28sp" android:textColor="@android:color/black"/> <TextView android:id="@+id/am_pm" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceSmall" android:textStyle="bold" android:textColor="@android:color/black"/> <TextView android:id="@+id/label" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:paddingLeft="8dip" android:textAppearance="?android:attr/textAppearanceSmall" android:textColor="@android:color/black" android:textStyle="bold" android:gravity="right" android:singleLine="true"/><!-- android:textColor="?android:attr/textColorTertiary" --> </LinearLayout> <TextView android:id="@+id/daysOfWeek" android:includeFontPadding="false" android:layout_width="match_parent" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceSmall" android:textColor="@android:color/black"/> </com.cn.daming.deskclock.DigitalClock> </LinearLayout>