使用自定义对话框类时如何修改文本并单击侦听器?

拉维

我有一个带有自定义布局的对话框类。现在我想从不同的活动更改文本值和不同的按钮单击侦听器。我正在尝试这样做但出现错误。这是我的源代码。任何帮助或建议将非常感激。

public class MyDialog extends Dialog {

    TextView dialogTitle, dialogMessage;
    Button dialogCancel, dialogOk;

    public MyDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    public void dismiss() {
        super.dismiss();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_layout);

        //..................REFERENCE
        dialogTitle = (TextView) findViewById(R.id.txt_ttl);
        dialogMessage = (TextView) findViewById(R.id.txt_msg);

        dialogCancel = (Button) findViewById(R.id.btn_cancel_id);
        dialogOk = (Button) findViewById(R.id.btn_ok_id);

        //dialogTitle.setText("title has been changed");
    }

    private void dialogCancel(){

    }

    private void dialogOk(){

    }

    public void changeDialogTitle(String dTitle){
        dialogTitle.setText(dTitle);
    }
}

从我正在使用的其他活动中

    ...onCreate{...
    MyDialog myDialog = new MyDialog(this);
}

// on button click show dialog
public void showDialog(View view) {
    myDialog.changeDialogTitle("Title");
    myDialog.show();
}
财政部

是的,也许您可​​以将标题作为参数传递

首先在 MyDialog 类中添加参数

       public class MyDialog extends Dialog {

    TextView dialogTitle, dialogMessage;
    Button dialogCancel, dialogOk;
    String title;
    public MyDialog(@NonNull Context context, String title) {
        super(context);
        this.title = title;
    }

    @Override
    public void dismiss() {
        super.dismiss();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_layout);

        //..................REFERENCE
        dialogTitle = (TextView) findViewById(R.id.txt_ttl);
        dialogMessage = (TextView) findViewById(R.id.txt_msg);

        dialogCancel = (Button) findViewById(R.id.btn_cancel_id);
        dialogOk = (Button) findViewById(R.id.btn_ok_id);
        // set your title in here
        dialogTitle.setText(title);
    }

    private void dialogCancel(){

    }

    private void dialogOk(){

    }

    public void changeDialogTitle(String dTitle){
        dialogTitle.setText(dTitle);
    }
}

然后在你的 Activity 上调用这样的函数

MyDialog myDialog = new Mydialog(this,"My New Title");
myDialog.show

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

自定义对话框的侦听器null

如何在“自定义”对话框中添加广播组选中的侦听器

Kotlin如何通过lambda使用自定义单击侦听器,而不是声明对象:CustomClass

使用自定义对话框时如何关闭引导框

使用自定义投射按钮时如何启动“连接到设备”对话框?

单击对话框警报按钮时将值设置为 listview(多个 onclick 侦听器)

创建了一个自定义布局对话框,但在对话框窗口外单击时无法关闭对话框

在Android中单击“按钮”时关闭自定义对话框

在单击ListView项时打开自定义对话框Android吗?

在自定义对话框窗口中单击按钮时出现空指针异常

通过自定义对话框类中的方法访问 EditText 文本

Android 自定义视图:如何在单个小部件上设置单击侦听器

使用自定义适配器在AlertDialog中单击侦听器

在自定义对话框的TextView中设置文本

Android-如何在自定义警报对话框中检查按钮单击?

单击按钮后如何打开“自定义对话框”

活动停止时删除自定义侦听器

自定义对话框类不显示对话框

Android微调器显示自定义对话框

Chromecast自定义选择器对话框

调用Dispose时不会自定义对话框

启动时创建自定义对话框

如何从 xml 文件中的 TextView 获取文本到自定义对话框中的 TexView?

如何在Xamarin.forms for Android中的权限对话框中自定义文本?

如何在自定义对话框小部件中获取文本字段的输入?

如何设置onDismiss / CancelListener在类外的自定义对话框?

在自定义对话框类中使用Android注释

如何更改打开对话框的图像以及如何自定义对话框?

如何在自定义对话框中添加微调框并使用数组填充它