您好我正在开发一个应用程序,其中我正在使用活动对话框,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" />
答案 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()
;在所有初始化和界面注册之后,或者将对话框与您的活动分开以使代码模块化,您可以执行以下操作:
class
扩展Dialog
。method
中创建class
以接受OnClickListener
并将其保存在对话class
内的class
级别。 activity
,而不是使用activity's
对话框的方法。Dialog class
的对话框按钮上设置新的OnClickListener
。activity's
OnClickListener
。希望它有所帮助!