【问题标题】:Custom DialogBox Alert in Android is not PromptingAndroid中的自定义对话框警报没有提示
【发布时间】:2012-08-23 09:07:29
【问题描述】:

以下是我为 2 个对话框创建的代码:即日期选择器和提交 2 个按钮时的自定义对话框,即 btnselDate 和 btnAlertDialog。

日期选择器对话框工作正常,但自定义对话框存在一些问题。 我的自定义对话框显示用户的登录表单。

请查看并提出适当的建议。

代码如下:

 public void onClick(View view)
    {
        if(view.getId() == R.id.btnselDate)
        {
            // Date Picket DialogBox

            showDialog(1);
        }
        else if(view.getId()==R.id.btnAlertDialog)
        {
            // Alert Dialog Box

            Context mContext = getApplicationContext();
            Dialog dialog = new Dialog(mContext);

            dialog.setContentView(R.layout.custom_activity);
            dialog.setTitle("Custom Dialog");

            TextView text = (TextView) dialog.findViewById(R.id.tvPwd);
            text.setText("Enter the Password");

            final EditText pwd=(EditText) dialog.findViewById(R.id.etPwd);

            Button btnlogin=(Button) dialog.findViewById(R.id.btnOK);

            btnlogin.setOnClickListener(new View.OnClickListener() {

                public void onClick(View v) {
                    // TODO Auto-generated method stub

                    //Login Button

                    if(pwd.getText().toString().equals("abc"))
                    {
                        Intent intent=new Intent(MainActivity.this,WelcomeUser.class);
                        startActivity(intent);
                    }
                    else
                    {
                        Toast.makeText(MainActivity.this, "Wrong Password, Try Again", Toast.LENGTH_SHORT).show();
                    }

                }
            });
        }
        else
        {
            Toast.makeText(MainActivity.this, "No Dialog Selected yet", Toast.LENGTH_SHORT).show();
        }
    }

【问题讨论】:

  • @RaghavSood 我正在逼近错误。我不知道我多次检查 logcat 的问题,但我无法修复错误。如果您有任何错误,请识别并告诉我代码中的错误??
  • 而 logcat 显示....
  • @DmytroZarezenko:我已经明确提到我的警报对话框不是提示..
  • 如果您要强制关闭,则必须有异常,因此对于相同的 Stackoverflow 人员总是期望 Logcat 输出

标签: android android-intent android-widget dialog customdialog


【解决方案1】:

而不是这个

  dialog.setContentView(R.layout.custom_activity);

对于对话框的自定义布局,您必须在此处膨胀布局,检查代码

    LayoutInflater factory = LayoutInflater.from(this);
    final View textEntryView = factory.inflate(R.layout.alert_dialog_text_entry, null);
    return new AlertDialog.Builder(AlertDialogSamples.this)
        .setIcon(R.drawable.alert_dialog_icon)
        .setTitle(R.string.alert_dialog_text_entry)
        .setView(textEntryView)
        .setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {

                /* User clicked OK so do some stuff */
            }
        })
        .setNegativeButton(R.string.alert_dialog_cancel, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {

                /* User clicked cancel so do some stuff */
            }
        })
        .create();

【讨论】:

  • @Rushabh 请检查此处的代码以扩展警报对话框的布局
猜你喜欢
  • 2015-04-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-17
  • 1970-01-01
相关资源
最近更新 更多