Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/182.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android OnTouchListener_Android_Xml_Ontouchlistener_Ontouch - Fatal编程技术网

Android OnTouchListener

Android OnTouchListener,android,xml,ontouchlistener,ontouch,Android,Xml,Ontouchlistener,Ontouch,我想在我的应用程序中实现OnTouchListener。我想要的是: 当我触摸屏幕时,我需要显示或隐藏一些动作:在这里,当我触摸ImageView时,显示处理动作的线性布局 我的布局 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/rl_af" android:l

我想在我的应用程序中实现OnTouchListener。我想要的是:

  • 当我触摸屏幕时,我需要显示或隐藏一些动作:在这里,当我触摸ImageView时,显示处理动作的线性布局
我的布局

<?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>

而不是
查看。不可见
==>
查看。消失

更新

在没有
MotionEvent.ACTION\u 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;
        }
    });
这是我设计的代码。
希望对您有所帮助

使用“不可见”仍会使对象在布局中占用空间。使用“已消失”将隐藏对象并删除空间

affBtn.setVisibility(View.GONE);

是否确实不需要一个
OnClickListener

rl.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        affBtn.setVisibility(affBtn.getVisibility == View.VISIBLE ? View.INVISIBLE : View.VISIBLE);
        }
    }
});
这将使您的视图在每次单击时在可见和不可见之间切换

如果你想让视图随着它占用的空间一起消失,请将上面代码段中的
视图。不可见的
更改为
视图。消失的

尝试在动作停止后返回“True”。安卓随后将侦听动作停止事件,并在发生时调用它。我在以前的一个项目中遇到过类似的问题

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;
    }
});

您遇到了什么问题?此代码不起作用。当我触摸图像时,查看线性布局(我在其中放置了另外两个按钮)不会消失如果您只想隐藏
linearlayout
为什么不使用
onClickListener
?我想每次触摸都显示或隐藏我想每次触摸都显示或隐藏。如果我单击并创建visibility消失了,当我再次单击时,如何使其可见?设置为在您希望再次看到它时可见
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;
    }
});