没有显示onClicklistener的警报对话框

时间:2016-08-21 13:10:26

标签: android android-alertdialog

我编写了以下代码来显示addinf客户列表的alertdialog框。但是它没有显示。帮我弄清楚错误。我已经通过更改上下文运行getApplicationContext(),Acitvity.this

            AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);


            LayoutInflater inflater = (LayoutInflater)getApplicationContext()
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View view = inflater.inflate(R.layout.add_customer_dialog, null);
            alertDialogBuilder.setView(view);

            TextView cust_txt=(TextView)view.findViewById(R.id.title);
            cust_txt.setText(getString(R.string.confirm_save_title));


            savebox_image=(RoundedImageView)view.findViewById(R.id.sav_imageView);
            diin=(TextView)view.findViewById(R.id.diain);
            dinam=(TextView)view.findViewById(R.id.dianame);

            diin.setText(mInDate.getText().toString());
            dinam.setText(name.getName());

            final AlertDialog alertDialog = alertDialogBuilder.create();
            alertDialog.getWindow().setType(WindowManager.LayoutParams.
                    TYPE_SYSTEM_ALERT);
            alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
            alertDialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);

            alertDialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN);
            alertDialog.show();

          alertDialog.findViewById(R.id.positive_button).setOnClickListener(new View.OnClickListener() {

                @Override

                public void onClick(View v) {

                    db.addRecord(new RecordClass(id, mInDate.getText().toString(),
                            name.getName());



                    alertDialog.dismiss();

                    db.close();

                }

            });

            alertDialog.findViewById(R.id.negative_button).setOnClickListener(new View.OnClickListener() {

                @Override

                public void onClick(View v) {

                    alertDialog.dismiss();

                }

            });

            alertDialog.findViewById(R.id.close_button).setOnClickListener(new View.OnClickListener() {

                @Override

                public void onClick(View v) {

                    alertDialog.dismiss();

                }

            });

2 个答案:

答案 0 :(得分:0)

您应该使用view代替alertDialog来获取自定义布局的按钮。

Button positiveCustomButton = (Button)view.findViewById(R.id.positive_button);
positiveCustomButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        db.addRecord(new RecordClass(id, mInDate.getText().toString(),
                                name.getName());
                        alertDialog.dismiss();
                        db.close();
                    }
                });

答案 1 :(得分:0)

AlertDialog类,无论您的自定义视图按钮面板(直到您有意将其从视图树中删除)及其自己的界面/侦听器,所以使用它:

  

DialogInterface.OnClickListener

而不是

  

View.OnClickListener

你有对话框参考,所以:

单键按钮

        alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, 
                                "Text of Button", 
                                new DialogInterface.OnClickListener() {
                                  @Override
                                  public void onClick(DialogInterface dialog, 
                                                    int which) {

                                  }
                                }
                             );
多个按钮的

 class SomeClas implements DialogInterface.OnClickListener {

     alertDialog.setButton(DialogInterface.BUTTON_POSITIVE,"Confirm Button", this);
     alertDialog.setButton(DialogInterface.BUTTON_NEGATIVE,"Abort Button", this);


    @Override
    public void onClick(DialogInterface dialog, int which) {

        switch(which) {
            case DialogInterface.BUTTON_POSITIVE:
              // positive clicked so do work 
            break;
            case DialogInterface.BUTTON_NEGATIVE:
              // example abort 
              dialog.dismiss();
            break;
        }
   }


}