对contentView的点击事件的理解

来源:互联网 发布:unix环境高级编程mobi 编辑:程序博客网 时间:2024/05/17 22:01

一、关于对contentView的点击事件的理解
1、获取任意Activity的contentView
ViewGroup contentView = ((ViewGroup)activity.findViewById(android.R.id.content));
2、获取contentView 的最外层(即下图的test_linearlayout)
View outView = contentView .getChildAt(0);
3、对最外层outView点击事件的理解
outView.setOnClickListener();
结合实例:

1)布局文件

<?xml version="1.0" encoding="utf-8"?><LinearLayout    xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:cyou="http://schemas.android.com/apk/res-auto"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:id="@+id/test_linearlayout"    android:background="@color/default_light_grey_bg"    android:orientation="vertical">    <RelativeLayout        android:background="@color/red"        android:layout_width="match_parent"        android:layout_height="match_parent">        <TextView            android:id="@+id/test_textview"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="我是子的TextView"            />    </RelativeLayout></LinearLayout>

这里写图片描述

总结:
1)若未对子TextView未设置点击事件,则点击文字(TextView)和其他空白区域,contentView 的点击事件都会响应
2)若对TextView设置点击事件,则点击文字(TextView),会响应TextView自己的点击事件,点击其他空白区域,contentView 的点击事件都会响应。
*结论:在子View未设置点击事件的情况下,点击任何区域contentView 的点击事件都会响应。若子View设置了点击事件,则除子View外的区域都会响应contentView 的点击事件。
3)但是,如果test_linearlayout的子View包裹的是ScrollView、ListView、RecycleView、WebView
不管test_textview有没有设置点击事件,点击任何区域,contentView 的点击事件都不会响应

<?xml version="1.0" encoding="utf-8"?><LinearLayout    xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:cyou="http://schemas.android.com/apk/res-auto"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:id="@+id/test_linearlayout"    android:background="@color/default_light_grey_bg"    android:orientation="vertical">    <ScrollView        android:background="@color/red"        android:layout_width="match_parent"        android:layout_height="match_parent">        <TextView            android:id="@+id/test_textview"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="我是子的TextView"            />    </RelativeLayout></LinearLayout>

这篇文章的主要用意是:
使用场景:若点击屏幕空白区域,键盘会被隐藏。此时,可以考虑在ContentView点击事件中处理。但是,需要注意在ScrollView、ListView、RecycleView、WebView中使用的场景。

  outView.setOnClickListener(new OnClickListener() {            public void onClick(View view) {                //隐藏键盘(注意ScrollView、ListView、RecycleView、WebView的使用场景)            }        });

二、知识点补充:获取根View的方法

getParent(获取上一级View)getRootViewgetWindow().getDecorView()findViewById(android.R.id.content)((ViewGroup)findViewById(android.R.id.content)).getChildAt(0)

1、getRootView特例

布局(R.layout.list_empty):<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="#FFFFFF">    <TextView        android:id="@+id/name"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerVertical="true"        android:layout_marginLeft="15dp"        android:text="暂无数据" /></RelativeLayout>
代码:    final View view = View.inflate(this, R.layout.list_empty, null);    final TextView name = (TextView) view.findViewById(R.id.name);    System.out.println("before View.inflate name getParent " + name.getParent());    System.out.println("before View.inflate name getRootView " + name.getRootView());    addContentView(view, new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT));    System.out.println("after View.inflate name getParent " + name.getParent());    System.out.println("after View.inflate name getRootView " + name.getRootView());
结果:before View.inflate name getParent android.widget.RelativeLayout{3d679e31 }before View.inflate name getRootView android.widget.RelativeLayout{3d679e31}after View.inflate name getParent android.widget.RelativeLayout{3d679e31}after View.inflate name getRootView com.android.internal.policy.impl.MultiPhoneWindow$MultiPhoneDecorView{c535bb}

结论:如果View在代码中通过View.inflate实例化,在没有添加到显示界面前,getRootView获得的是Xml的根布局。添加后getRootView获得的是MultiPhoneDecorView。

原创粉丝点击