button not shown in alertDialog

浪子不回头ぞ 提交于 2020-01-06 10:41:50

问题


I m creating a class that extends AlertDialog. and setting layout. But when pop up is called,

**1) layout is transparent

2) buttons not visible even after calling setButton ** I don't know what to do with , and no Idea why its happening.

My classes are this DateTimeDialog.java

public class DateTimeDialog extends AlertDialog{

    Date date;
    String title;
    DialogInterface.OnClickListener listner;
    protected DateTimeDialog(Context context, String title, Date date ) {
        super(context);
        // TODO Auto-generated constructor stub
        this.title = title;
        this.date = date;
    }

    public void initListener(DialogInterface.OnClickListener listner){
        this.listner = listner;
    }

    public Date getDate(){


        return date;
    }


    @Override
    public void onCreate(Bundle savedInstanceState){
        //super.onCreate(savedInstanceState);
        setContentView(R.layout.date_time_picker);



        setTitle(title);

        setButton( "OK", listner);

        setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel",
                new OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // Do something
                        dialog.dismiss();
                    }
                });

    }

method that is calling this class

final DateTimeDialog dateTimeDialog = new DateTimeDialog(context, "title", time);
           dateTimeDialog.show();
           dateTimeDialog.initListener(new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub
                    //doSomething
                }
            });

Please help.....


回答1:


When you call setContentView, you override any and all content the AlertDialog class provides. If you want to set the content view yourself, you need to add the buttons in the XML file and set listeners.



来源:https://stackoverflow.com/questions/10991805/button-not-shown-in-alertdialog

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!