简体   繁体   English

自定义警报对话框未定义方法

[英]Custom Alert Dialogs undefined method

i am new to android and i am trying to get this custom dialog to work, i get an error on showDialog & removeDialog which says The method removeDialog(int) is undefined for the type new DialogInterface.OnClickListener . 我是Android新手,我试图让这个自定义对话框工作,我在showDialogremoveDialog上得到一个错误,说明方法removeDialog(int)未定义为new DialogInterface.OnClickListener类型。

onReceive method: onReceive方法:

showDialog(DIALOG_TEXT_ENTRY);

Code: 码:

    private static final int MY_PASSWORD_DIALOG_ID = 0;

    MediaPlayer mp;
    Context context;   

    protected Dialog onCreateDialog(int id, Context context) {

        Dialog dialog = null;
        switch(id) {
        case MY_PASSWORD_DIALOG_ID:
            LayoutInflater factory = LayoutInflater.from(context);

            final View textEntryView = factory.inflate(R.layout.password_dialog, null);

            final EditText password1 = (EditText) textEntryView.findViewById(R.id.inputPassword);

            AlertDialog.Builder builder = new AlertDialog.Builder(context);
            builder.setTitle("Enter Password");
            builder.setView(textEntryView);

            builder.setPositiveButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int whichButton) {
                      removeDialog(MY_PASSWORD_DIALOG_ID);
                   }
                });

                AlertDialog passwordDialog = builder.create();
                return passwordDialog; 
        }
        return null;
    }

use 采用

dialog.dismiss();

instead of 代替

removeDialog(MY_PASSWORD_DIALOG_ID);

you cant show a dialog in a BroadcastReceiver. 你不能在BroadcastReceiver中显示一个对话框。

but still, if you want to show a popup screen, then create an activity, with Dialog theme. 但是,如果你想显示一个弹出屏幕,那么用Dialog主题创建一个活动。

You can also use 你也可以使用

builder.setPositiveButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int whichButton) {
                       dialog.cancel(); 
                   }
                });

on button click it should work fine 点击按钮它应该工作正常

can refer this link also 也可以参考这个链接

http://developer.android.com/guide/topics/ui/dialogs.html#AddingButtons http://developer.android.com/guide/topics/ui/dialogs.html#AddingButtons

you should use dialog.dismiss(); 你应该使用dialog.dismiss(); instead of your removeDialog() method. 而不是你的removeDialog()方法。

See below. 见下文。 Use dismiss(); 使用dismiss();

builder.setPositiveButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
   public void onClick(DialogInterface dialog, int whichButton) {
      dismiss();
   }
};

Call this way showDialog(MY_PASSWORD_DIALOG_ID); 这样调用showDialog(MY_PASSWORD_DIALOG_ID);

Refer this Create Custom Dialog tutorial 请参阅此创建自定义对话框教程

@Override
protected Dialog onCreateDialog(int id) {
 // TODO Auto-generated method stub

 screenDialog = null;
 switch(id){
 case(ID_SCREENDIALOG):
  screenDialog = new Dialog(this);
  screenDialog.setContentView(R.layout.screendialog);
  bmImage = (ImageView)screenDialog.findViewById(R.id.image);
  TextOut = (TextView)screenDialog.findViewById(R.id.textout);
  btnScreenDialog_OK = (Button)screenDialog.findViewById(R.id.okdialogbutton);
  btnScreenDialog_OK.setOnClickListener(btnScreenDialog_OKOnClickListener);
 }
 return screenDialog;
}

use dialog.dismiss(); 使用dialog.dismiss();

plz refer to following link http://developer.android.com/guide/topics/ui/dialogs.html 请参阅以下链接http://developer.android.com/guide/topics/ui/dialogs.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM