如何在使用自定义对话框时修改文本和点击监听器Class?

How to modify Text and click Listener while using custom Dialog Class?

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

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 中添加参数 Class

       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

请看下面的代码片段:

public class MyDialog extends Dialog {

    TextView dialogTitle, dialogMessage;
    Button dialogCancel, dialogOk;
    String title;

    public MyDialog(@NonNull Context context, final 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);

        dialogTitle.setText(title);
    }

    private void dialogCancel(){

    }

    private void dialogOk(){

    }

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


----------


// on button click show dialog
public void showDialog(View view) {
 MyDialog myDialog = new MyDialog(this, "Title");
  myDialog.show();
}

MyDialog myDialog = new MyDialog(this, "Title"); 您可以将标题和消息发送到构造函数中,它将在 Dialog onCreate 方法时在 UI 上设置。

您遇到了空指针异常,因为您在显示对话框之前更改了对话框标题。因此,首先显示对话框,然后更改标题。将其更改为:

myDialog.show();
myDialog.changeDialogTitle("Title");

看起来您正在尝试创建一个独立于生命周期的实用程序class,您只需将 onCreate 方法的代码移动到 MyDialog 构造函数,如下所示

public class MyDialog extends Dialog {

    TextView dialogTitle, dialogMessage;
    Button dialogCancel, dialogOk;

    public MyDialog(@NonNull Context context) {
        super(context);
        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");
    }

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

    private void dialogCancel(){

    }

    private void dialogOk(){

    }

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

现在您可以随时更改视图的内容。应该可以。