代码之家  ›  专栏  ›  技术社区  ›  Nima Khalili

AlertDialog按钮错误

  •  -3
  • Nima Khalili  · 技术社区  · 7 年前

         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 回复  |  直到 7 年前
        1
  •  1
  •   AskNilesh    7 年前

    你忘了添加 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();
    
                }
            });
    
        2
  •  0
  •   M.Waqas Pervez    7 年前

    只需将第一个参数添加到 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();
    
    }
                        });
    
        3
  •  0
  •   Arjun Solanki    7 年前
     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();
    

    您也可以使用上面描述的代码中的肯定按钮。

        4
  •  0
  •   Sonal Bharwani    7 年前

    试试这个

       
    
        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();
            }