如何在Android上将onClickListener设置为WHOLE屏幕? [英] How to set onClickListener to the WHOLE screen in Android?

查看:78
本文介绍了如何在Android上将onClickListener设置为WHOLE屏幕?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

xml布局文件如下:

The xml layout file looks like:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout  android:id="@+id/my_rootViewID"
    <LinearLayout>

        <ImageView/>

        <TextView/>

        <ImageView/>

    </LinearLayout>
</FrameLayout>

我已经尝试过了:

my_view = findViewById(R.id.my_rootViewID);
my_view.setOnClickListener( new My_OnClickListener() );

我的问题:这仅适用于ImageViews,但TextView是不可单击的. 我也可以将OnClickListener专门设置为TextView,但是我有很多TextView(在其他情况下),因此添加这些监听器将非常缓慢且复杂.

My problem: This works only with the ImageViews, but the TextView is not clickable. I could set OnClickListener specifically to the TextView as well, but I have many TextViews (in other cases), so adding theese listeners would be very slow, and elaborate.

推荐答案

您可以执行以下操作:

ViewTreeObserver viewTreeObserver = myView.getViewTreeObserver();
viewTreeObserver.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {...}

来自 ViewTreeObserver :

视图树观察器用于注册可以 通知视图树中的全局更改.这样的全球性事件 包括但不限于整棵树的布局,开始 图纸传递的次数,触摸模式更改

A view tree observer is used to register listeners that can be notified of global changes in the view tree. Such global events include, but are not limited to, layout of the whole tree, beginning of the drawing pass, touch mode change

这篇关于如何在Android上将onClickListener设置为WHOLE屏幕?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆