简体   繁体   English

Android OnTouchListener

[英]Android OnTouchListener

I want to implement OnTouchListener in my app.我想在我的应用程序中实现 OnTouchListener。 What I want:我想要的是:

  • when I touch the screen , I need to show or hide some action: here when I touch ImageView , show the LinearLayout which handle actions当我触摸屏幕时,我需要显示或隐藏一些动作:在这里,当我触摸 ImageView 时,显示处理动作的 LinearLayout

My Layout我的布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rl_af"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000">

<ImageView
    android:id="@+id/ivaf"
    android:layout_width="match_parent"
    android:layout_height="400dp"
    android:layout_centerInParent="true" />

<LinearLayout
    android:id="@+id/llaffbtn"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_alignParentBottom="true"
    android:orientation="horizontal">

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="#000000"
        android:src="@drawable/ic_file_download_white_24dp" />

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="#000000"
        android:src="@drawable/ic_share_white_24dp" />

</LinearLayout>

and what i tried in my java class:以及我在 Java 类中尝试的内容:

    img = (ImageView) findViewById(R.id.ivaf);
    rl = (RelativeLayout) findViewById(R.id.rl_af);
    affBtn = (LinearLayout) findViewById(R.id.llaffbtn);
    rl.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {

            if (event.getAction() == MotionEvent.ACTION_DOWN) {

                affBtn.setVisibility(View.VISIBLE);
            }
            if (event.getAction() == MotionEvent.ACTION_UP) {
                affBtn.setVisibility(View.INVISIBLE);
            }

            return false;
        }
    });

instead of View.INVISIBLE ==> View.Gone而不是View.INVISIBLE ==> View.Gone

Update更新

Try it without MotionEvent.ACTION_DOWN) {... like :在没有MotionEvent.ACTION_DOWN) {...情况下尝试MotionEvent.ACTION_DOWN) {...像:

    img = (ImageView) findViewById(R.id.ivaf);
    rl= (RelativeLayout) findViewById(R.id.rl_af);
    affBtn = (LinearLayout) findViewById(R.id.llaffbtn);
    rl.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            affBtn.setVisibility(affBtn.getVisibility()== View.VISIBLE ? View.GONE : View.VISIBLE);
            return false;
        }
    });

this code working on my devise.这段代码适用于我的设计。 hope it helps希望能帮助到你

Using invisible will still have the object take up space in layout.使用 invisible 仍然会使对象在布局中占用空间。 Using Gone instead will hide the object and remove the space.改用 Gone 将隐藏对象并删除空间。

affBtn.setVisibility(View.GONE);

Are you sure you don't really want an OnClickListener ?你确定你真的不想要一个OnClickListener吗?

rl.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        affBtn.setVisibility(affBtn.getVisibility == View.VISIBLE ? View.INVISIBLE : View.VISIBLE);
        }
    }
});

This will make your view toggle between visible and invisible on every click.这将使您的视图在每次单击时在可见和不可见之间切换。

If you want the view to disappear along with the space that it takes up, change View.INVISIBLE in the code snippet above to View.GONE如果您希望视图与其占用的空间一起消失, View.INVISIBLE上面代码片段中的View.GONE更改为View.GONE

Try returning "True" after ACTION_DOWN.尝试在 ACTION_DOWN 后返回“True”。 Android will then listen to ACTION_UP events and call it when it happens.然后 Android 会监听 ACTION_UP 事件并在它发生时调用它。 I had a similar issue in an earlier project of mine.我在我早期的项目中遇到了类似的问题。

rl.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {

        if (event.getAction() == MotionEvent.ACTION_DOWN) {

            affBtn.setVisibility(View.VISIBLE);
            return true;
        }
        if (event.getAction() == MotionEvent.ACTION_UP) {
            affBtn.setVisibility(View.INVISIBLE);
        }

        return false;
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM