Dialog Android中的OnclickListener

时间:2016-07-18 05:02:01

标签: android button dialog onclicklistener

您好我正在开发一个应用程序,其中我正在使用活动对话框,Dialog有一个按钮但是当我想使用OnClickListener(来自活动)时它不起作用。我试图在互联网上搜索这个问题的解决方案,但我找不到一个正确的方法来实现它。那么任何人都可以建议一种方法......

public void btnStartDialog(View view) {
    final Dialog dialog = new Dialog(this);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.setContentView(R.layout.custom_dialog);
    final VideoView videoview = (VideoView) dialog.findViewById(R.id.videoView);
    Uri uri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.video);
    videoview.setVideoURI(uri);
    videoview.start();
    videoview.requestFocus();

    final Button stop = (Button)dialog.findViewById(R.id.btnStop);

    stop.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    videoview.stopPlayback();
                    dialog.dismiss();
                }
            });

    dialog.show();
    Thread timerThread = new Thread() {
        public void run() {
            try {
                sleep(34800);
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                videoview.stopPlayback();
                dialog.dismiss();
                //close();
            }
        }
    };
    timerThread.start();

}

BTN XML:

 <android.support.design.widget.FloatingActionButton
    android:layout_width="56dp"
    android:layout_height="56dp"
    android:src="@drawable/fab_plus"
    app:backgroundTint="#d20b0d"
    app:borderWidth="0dp"
    app:elevation="10dp"
    app:fabSize="normal"
    android:onClick="btnStartDialog"
    app:pressedTranslationZ="12dp"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="10dp" />

3 个答案:

答案 0 :(得分:3)

stop.setOnClickListener之后显示对话框,

final Dialog dialog = new Dialog(this);

   //Your codes here

    stop.setOnClickListener(
            new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            videoview.stopPlayback();
            dialog.dismiss();
        }
    });

   dialog.show();

答案 1 :(得分:1)

试试这个..

final Dialog dialog = new Dialog(this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
View view = LayoutInflater.from(activity).inflate(R.layout.custom_dialog,
            null);
dialog.setContentView(view );

final VideoView videoview = (VideoView) view.findViewById(R.id.videoView);
Uri uri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.video);
videoview.setVideoURI(uri);
videoview.start();
videoview.requestFocus();

final Button stop = (Button) view.findViewById(R.id.btnStop);

dialog.show();

stop.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        videoview.stopPlayback();
        dialog.dismiss();
    }
 });

答案 2 :(得分:0)

致电dialog.show();在所有初始化和界面注册之后,或者将对话框与您的活动分开以使代码模块化,您可以执行以下操作:

  1. 创建class扩展Dialog
  2. 在对话框method中创建class以接受OnClickListener并将其保存在对话class内的class级别。
  3. 启动该对话框调用后activity,而不是使用activity's对话框的方法。
  4. Dialog class的对话框按钮上设置新的OnClickListener
  5. 只需手动拨打该按钮即可回复activity's OnClickListener
  6. 希望它有所帮助!