1.在普通布局中,父控件中含有button、ImageButton、CheckBox、EditText,RatingBar等时,点击事件失效。
例如:列表中的item布局中含有RatingBar子控件。
在item的根布局文件中加入:android:descendantFocusability=”blocksDescendants”
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="48dp"
android:descendantFocusability="blocksDescendants">
<TextView
android:id="@+id/tv_store_listitem_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="哈尔滨神灯汽修"/>
<RatingBar
android:id="@+id/rb_store_listitem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5"
android:max="5"
android:progress="2"
android:stepSize="1"/>
</LinearLayout>
descendantFocusability属性
android:descendantFocusability
该属性是当一个为view获取焦点时,定义viewGroup和其子控件两者之间的关系。
属性的值有三种:
beforeDescendants:viewgroup会优先其子类控件而获取到焦点
afterDescendants:viewgroup只有当其子类控件不需要获取焦点时才获取焦点
blocksDescendants:viewgroup会覆盖子类控件而直接获得焦点
2.自定义组合控件中含有button、ImageButton、CheckBox、EditText,RatingBar等时,使自定义的组合控件点击事件失效。而响应子view的事件
在自定义view中重写onInterceptTouchEvent()方法,它会在事件传给孩子之前被调用。从而实现事件分配不到给子View时就将事件拦截下来。
例如自定义FrameLayout布局,布局中含有一个TextView和一个EditText。通过判断自定义属性isContentFocus来判断是否响EditText的事件。如果传过来的值是true,那么就相应EditText事件不响应自定义布局事件,如果传过来的值是false,那么取消EditText事件响应实现自定义布局的事件响应。
自定义SettingItemView组合控件继承FrameLayout
public class SettingItemView extends FrameLayout{
private static final String TAG = "ZCN=SettingItemView";
private EditText etMsg;
private TextView tvTitle;
private static final int DEFAULT_TEXT_COLOR = Color.BLACK;
private static final int DEFAULT_EDIT_COLOR = Color.TRANSPARENT;
private String titleText;
private float titleSize;
private int titleColor;
private boolean isTitleHide;
private float contentSize;
private int contentColor;
private int contentHintColor;
private String contentText;
private boolean isContentFocus;
private int contentLength;
private int inputType;
private int DEFAULT_TITLE_SIZE = 16;
private int DEFAULT_TITLE_COLOR = Color.GRAY;
private boolean DEFAULT_TITLE_HIDE = false;
private int DEFAULT_CONTENT_SIZE = 16;
private int DEFAULT_CONTENT_COLOR = Color.BLACK;
private int DEFAULT_CONTENT_HINT_COLOR = Color.GRAY;
private boolean DEFAULT_CONTENT_FOCUS = true;
private int DEFAULT_CONTENT_LENGTH = 20;
public SettingItemView(Context context, AttributeSet attrs) {
super(context, attrs);
initAttrs(context, attrs);
initView(context);
}
private void initAttrs(Context context, AttributeSet attrs) {
Log.d(TAG,"=====initAttrs=====");
TypedArray typedArray = context.obtainStyledAttributes(attrs,R.styleable.SettingItemView);
titleText = typedArray.getString(R.styleable.SettingItemView_title_text);
titleSize = typedArray.getDimension(R.styleable.SettingItemView_title_size, DEFAULT_TITLE_SIZE);
titleColor = typedArray.getColor(R.styleable.SettingItemView_title_color, DEFAULT_TITLE_COLOR);
isTitleHide = typedArray.getBoolean(R.styleable.SettingItemView_title_hide, DEFAULT_TITLE_HIDE);
contentText = typedArray.getString(R.styleable.SettingItemView_content_text);
contentSize = typedArray.getDimension(R.styleable.SettingItemView_content_size, DEFAULT_CONTENT_SIZE);
contentColor = typedArray.getColor(R.styleable.SettingItemView_content_color, DEFAULT_CONTENT_COLOR);
contentHintColor = typedArray.getColor(R.styleable.SettingItemView_content_hint_color, DEFAULT_CONTENT_HINT_COLOR);
isContentFocus = typedArray.getBoolean(R.styleable.SettingItemView_content_focus, DEFAULT_CONTENT_FOCUS);
contentLength = typedArray.getInteger(R.styleable.SettingItemView_content_length,DEFAULT_CONTENT_LENGTH);
inputType = typedArray.getInteger(R.styleable.SettingItemView_content_input_type,1);
typedArray.recycle();
}
private void initView(Context context) {
Log.d(TAG,"=====initView=====");
LayoutInflater.from(context).inflate(R.layout.view_setting_item, this);// 加载布局
etMsg = (EditText) findViewById(R.id.et_setting_item);// 获取控件
tvTitle = (TextView) findViewById(R.id.tv_setting_item);
tvTitle.setText(titleText);
tvTitle.setTextSize(TypedValue.COMPLEX_UNIT_SP,titleSize);
//如果不设置这个文字会变得非常大
tvTitle.getPaint().setTextSize(titleSize);
tvTitle.setTextColor(titleColor);
if(isTitleHide){
tvTitle.setVisibility(GONE);
}
etMsg.setText(contentText);
etMsg.setTextSize(TypedValue.COMPLEX_UNIT_SP,contentSize);
//如果不设置这个文字会变得非常大
etMsg.getPaint().setTextSize(contentSize);
etMsg.setTextColor(contentColor);
etMsg.setHintTextColor(contentHintColor);
etMsg.setFilters(new InputFilter[]{new InputFilter.LengthFilter(contentLength)});
etMsg.setInputType(inputType);
if(!isContentFocus){
etMsg.setFocusable(false);
etMsg.setClickable(false);
}
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
Log.d(TAG,"=====onInterceptTouchEvent=====");
//如果editText传过来放弃焦点的值,那么layout截取事件并响应事件
return !isContentFocus;
}
//设置EditText值
public void setContentText(String str){
etMsg.setText(str);
}
//获取EditText值
public String getContentText(){
return etMsg.getText().toString().trim();
}
}
attrs.xml文件
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="SettingItemView">
<attr name="title_text" format="string"/>
<attr name="title_color" format="color"/>
<attr name="title_size" format="dimension"/>
<attr name="title_hide" format="boolean"/>
<attr name="content_text" format="string"/>
<attr name="content_size" format="dimension"/>
<attr name="content_color" format="color"/>
<attr name="content_length" format="integer"/>
<attr name="content_input_type" format="integer"/>
<attr name="content_hint_color" format="color"/>
<attr name="content_focus" format="boolean"/>
</declare-styleable>
</resources>
view_setting_item.xml布局
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/et_setting_item"
android:layout_width="match_parent"
android:layout_height="48dp"
android:background="@android:color/transparent"
android:singleLine="true"
android:gravity="center"/>
<TextView
android:id="@+id/tv_setting_item"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:gravity="center_vertical"
android:layout_marginLeft="16dp"/>
</FrameLayout>
在布局中使用自定义View
<!--//将content_focus属性值置为ture,就会响应EditText事件,置为false会响应SettingItemView的点击事件-->
<com.sdlj.vehiclerepair.view.SettingItemView
android:id="@+id/siv_setting_name"
style="@style/setting_setting_item_view"
android:layout_marginBottom="@dimen/setting_gap_view_height"
android:layout_marginTop="@dimen/setting_gap_view_height"
app:title_text="@string/setting_name"
app:content_focus="false"
/>