通过点击自定义对话框中的按钮生成一个新的自定义对话框
下面是我的第一层自定义对话框的类,其中,加载进来的布局中有三个按钮,第三个按钮也就是button3,是我当作生成下一层对话框的事件按钮。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | package com.studio.dialogdemo; import android.app.Dialog; import android.content.Context; import android.support.annotation.NonNull; import android.view.View; import android.widget.Button; /** * Created by dell on 2018/4/23. */ public class MyDialog1 extends Dialog { private Button button1,button2,button3; public MyDialog1( @NonNull Context context, int themeResId) { super (context, themeResId); setContentView(R.layout.layout_mydialog1); button1 = findViewById(R.id.button_yes_dialog1); button2 = findViewById(R.id.button_no_dialog1); button3 = findViewById(R.id.button_showthis); button1.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View view) { System.exit( 0 ); } }); button2.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View view) { dismiss(); } }); button3.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View view) { dismiss(); ShowMyInformationDailog showMyInformationDailog = new ShowMyInformationDailog( this ,R.style.mydialog); showMyInformationDailog.show(); } }); } } |
下面是我的第二层自定义对话框。这个自定义类中加载的布局中只有一个imageview。按理说,当我在第一个自定义对话框弹出来后点击button3的时候,会加载出第二层对话框,可是问题却是程序终止,应用闪退。请老师看看我哪里没注意!谢谢!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | package com.studio.dialogdemo; import android.app.Dialog; import android.content.Context; import android.support.annotation.NonNull; import android.view.View; /** * Created by dell on 2018/4/23. */ public class ShowMyInformationDailog extends Dialog{ public ShowMyInformationDailog( @NonNull View.OnClickListener context, int themeResId) { super ((Context) context, themeResId); setContentView(R.layout.layout_dialog1_dialog); } } |
10
收起
正在回答
1回答
1、MyDialog1中,下图中的this你替换成当前调用这个MyDialog1的Activity的context,例如改成LoginActivity.this
2、ShowMyInformationDailog中,把红框部分的代码换成android.content.Context包下面的Context类
Android零基础入门2018版
- 参与学习 人
- 提交作业 5461 份
- 解答问题 7235 个
此次推出的专题为Android攻城狮培养计划的第一部分语法与界面基础篇,将带大家从0开始学习Android开发。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧