android - Dialog 的键盘消失后,VideoView 未正确呈现

标签 android android-videoview android-alertdialog android-dialog

我将我的问题简化为可以重现的最小示例。

所以:

  • 1 个 VideoView 和 ImageView Activity 。
  • 单击 ImageView 后会显示 AlertDialog。
  • AlertDialog 有 1 个 EditText 字段。
  • 我触摸此 EditText 并显示标准 Android 键盘。
  • 关闭键盘。
  • 关闭对话框。

问题:VideoView 的边框(黑色矩形)被扩展,因此 ImageView 不再显示。

感谢任何帮助!谢谢。

BEFORE AFTER

代码:

主 Activity .java

import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.VideoView;

public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final Activity act = this;
    final VideoView videoView = (VideoView) findViewById(R.id.videoView1);
    videoView.setVideoPath("/mnt/sdcard/s1ep01.mp4");
    videoView.requestFocus();
    findViewById(R.id.imageView1).setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View arg0) {
            AlertDialog.Builder builder = new AlertDialog.Builder(act);
            View view = LayoutInflater.from(act).inflate(R.layout.dialog, null);
            builder.setView(view);
            builder.setPositiveButton("Save translation", null);
            builder.setNegativeButton("Cancel", null);
            AlertDialog dialog = builder.create();
            dialog.show();
        }
    });
}
}

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<VideoView
    android:id="@+id/videoView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true" />

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/videoView1"
    android:src="@android:drawable/btn_dialog" />

</RelativeLayout>

对话框.xml

<?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="wrap_content">

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

</RelativeLayout>

最佳答案

作为临时解决方案,我创建了一个 Runnable 使 VideoView 不可见,然后在 200 毫秒后使其可见:

// Hide the VideoView
videoLayout.setVisibility(View.INVISIBLE);

// create a handler to handle the delayed runnable request
new Handler().postDelayed(new Runnable() {

    @Override
    public void run() {
        // After the 200 millis (which are passes as a second parameter to this postDelayed() method in the last line of the code, this handler will invoke the following method :

        // run on UI so you can set the Visibitity of the UI elements
        runOnUiThread(new Runnable() {
     @Override
            public void run() {videoLayout.setVisibility(View.VISIBLE);}    // make it visible again
 });
    }
}, 200);    // this is the second parameter which decides when this handler will run it's run() method

希望这个临时解决方案对现在有帮助

当你的键盘隐藏时调用这个方法,就像在 ENTER 键上,或者当你触摸键盘外的某个 View 来隐藏它时......但不要把它放在 Activity 的 onTouchEvent() 或 onUserInteraction 中所以不要一直闪烁

关于android - Dialog 的键盘消失后,VideoView 未正确呈现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17832904/

相关文章:

android - 如何在android中单屏播放多个视频?

android - videoview的onpreparedlistener()出错

android - 什么是 `COLUMN_NAME_NULLABLE`

java - 如何正确扩展 TextInputLayout.java

android - Android 内部存储的视频播放最终挂起/崩溃

java - Android - 显示空 ListView 的警报对话框

android - 以编程方式创建要显示在 AlertDialog 上的布局

android - 从android中的服务弹出警告对话框

android - 在 android 中运行启动器 Activity 时出现空指针异常

java - 在包含下划线的 Android 包名称中调用 JNI 函数