【问题标题】:How to hide and un-hide image button(cancel button) on video view when user touches the video screen?当用户触摸视频屏幕时,如何在视频视图中隐藏和取消隐藏图像按钮(取消按钮)?
【发布时间】:2017-04-28 00:25:42
【问题描述】:

我试图让我的图像按钮在用户触摸视频屏幕时可见和不可见。(我正在使用视频视图在 android studio 上播放视频)。最初图像按钮是不可见的,但我想让它在用户可见时可见触摸视频屏幕,如果他再次触摸它,我希望图像按钮变得不可见。

我使用了以下代码,但是当我触摸视频时,它说不幸的是您的应用停止工作并让我回到主要活动(关闭播放器活动)!专家可以告诉我如何解决这个问题谢谢

@Override
    public boolean onTouchEvent(MotionEvent event) {
        but1= (Button)findViewById(R.id.imageButtonBack);
        if (event.getAction() == MotionEvent.ACTION_DOWN) {

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

            but1.setVisibility(View.INVISIBLE);
        }
        return super.onTouchEvent(event);
    }

activity_player.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_player"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">




    <VideoView android:id="@+id/videoView"
        android:layout_width="fill_parent"
        android:layout_alignParentRight="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_alignParentBottom="true"
        android:layout_height="fill_parent">
    </VideoView>

    <ImageButton
        android:paddingLeft="5dp"
        android:paddingTop="8dp"
        android:layout_width="67dp"
        android:layout_height="40dp"
        android:id="@+id/imageButtonBack"
        android:src="@drawable/donebutton_active"
        android:visibility="gone"
        android:onClick="donebutton" />
</RelativeLayout>

【问题讨论】:

    标签: android android-studio ontouchlistener android-videoview


    【解决方案1】:

    findViewById 应该在 onCreate 中的 setContentView(R.layout.xxx) 之后调用,

    像这样:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.xx);
        but1= (Button)findViewById(R.id.imageButtonBack);
    
    }
    

    【讨论】:

    • 感谢您的回复。我没有任何布局如何制作?我是 android 编程的新手,所以如果需要解决问题,希望你帮我添加布局。
    • 也许你可以先尝试学习别人的项目。像上面一样。
    • 我会尝试将该链接中描述的方法用于其他项目,但是否可以使用视频视图使按钮可见?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-18
    • 2011-03-22
    相关资源
    最近更新 更多