对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。
- 对contentView的点击事件的理解
- UIScrollView的contentView 助于理解
- 对事件的理解
- 对事件的理解
- 理解点击事件的事件分发过程
- UITableViewCell的contentView
- Android的点击事件的深入理解
- 我对事件的理解
- 对C#事件的理解
- 对事件委托的理解
- 对RecyclerView的item添加点击事件
- Activity的重新设置contentview
- iOS UICollectionViewCell 的 contentView 注意事项
- 我对事件驱动的理解
- 对AS3.0事件机制的理解
- 我对委托与事件的理解
- 我对委托与事件的理解
- 对touch事件传递的简单理解
- win10下vs2015配置Opencv3.3.1过程详解
- 对于数组指针—指针数组;函数指针—函数指针数组,指向函数指针数组的指针,的理解
- 读懂Hinton的提出的Capsulet架构并用TensorFlow实现
- Android 各种加密
- 热部署原理解析
- 对contentView的点击事件的理解
- 总裁、董事长、CEO有什么区别?
- 深度摄像头测距原理简介
- 15.4 处理事件(监视器)
- jquery 判断array中是否包含指定字符串
- MFC(vs2013)+opencv+海康SDK_显示视频教程
- Nexus Repository Manager OSS 3的好处
- 我的自传(一)
- iptables案例:使用iptables搭建路由器