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

时间:2018-08-16 06:42:30

标签: android dialog android-dialogfragment customdialog

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

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();
}

4 个答案:

答案 0 :(得分:0)

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

首先在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

答案 1 :(得分:0)

请参见以下代码段:

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,“标题”); 您可以将标题和消息发送到构造函数中,并将在Dialog onCreate方法时在UI上进行设置。

答案 2 :(得分:0)

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

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

答案 3 :(得分:0)

好像您正在尝试创建一个独立于生命周期的实用程序类,您只需要将onCreate方法的代码移至如下所示的MyDialog Constructor中

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);
    }
}

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