AlertDialog按钮错误

时间:2017-09-07 08:01:32

标签: android android-alertdialog

我的问题是我无法在ExpandableListView中使用alertDialog设置Button, 你可以在图像中看到我的错误

     public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, final View convertView, ViewGroup parent) {

            final ImageView imageView = new ImageView(context);
            imageView.setImageResource(imgList[groupPosition][childPosition]);
            imageView.setLayoutParams(new ViewGroup.LayoutParams(350,350));
            imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    AlertDialog alt = new AlertDialog.Builder(MainActivity.this).create();
                    alt.setTitle("Esfandune");
                    alt.setIcon(R.drawable.ic_launcher);
                    alt.setMessage("Esfandune.ir is the best !");
                    alt.setButton("yes", new DialogInterface.OnClickListener() {
                      @Override
                       public void onClick( DialogInterface arg0,  int arg1) {
                           Toast.makeText(getApplicationContext(),

//
                                  "You clicked on yes", Toast.LENGTH_SHORT).show();

}
                    });

图片

enter image description here

4 个答案:

答案 0 :(得分:1)

您忘记在DialogInterface.BUTTON_POSITIVE中添加添加alt.setButton作为第一个参数,因此请将其添加为参数,如下面的代码

alt.setButton(DialogInterface.BUTTON_POSITIVE, "yes", new DialogInterface.OnClickListener() {
            @Override
            public void onClick( DialogInterface arg0,  int arg1) {
                Toast.makeText(getApplicationContext(),"You clicked on yes", Toast.LENGTH_SHORT).show();

            }
        });

答案 1 :(得分:0)

只需添加setButton中的第一个参数即可。

 AlertDialog alt = new AlertDialog.Builder(MainActivity.this).create();
                    alt.setTitle("Esfandune");
                    alt.setIcon(R.drawable.ic_launcher);
                    alt.setMessage("Esfandune.ir is the best !");
                    alt.setButton(DialogInterface.BUTTON_POSITIVE, "yes", new DialogInterface.OnClickListener() {
                      @Override
                       public void onClick( DialogInterface arg0,  int arg1) {
                           Toast.makeText(getApplicationContext(),

//
                                  "You clicked on yes", Toast.LENGTH_SHORT).show();

}
                    });

答案 2 :(得分:0)

 AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MainActivity.this);
        alertDialogBuilder.setMessage("Message");
        alertDialogBuilder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
              // add your work after click
            }
        });


        AlertDialog alertDialog = alertDialogBuilder.create();
        alertDialog.show();

您也可以使用正面按钮作为上述描述代码。

答案 3 :(得分:0)

试试这个

   

    public static void openAlertDialog(final Activity context, final String message) {
            if (isValueNull(message)) {
                return;
            }

            new AlertDialog.Builder(context).setTitle(R.string.app_name).setMessage(message).setCancelable(false)
                    .setPositiveButton("yes", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            Toast.makeText(getApplicationContext(),

    //
                                      "You clicked on yes", Toast.LENGTH_SHORT).show();

                        }
                    }).create().show();
        }