【问题标题】:Why does my alert dialog not appear?为什么我的警报对话框没有出现?
【发布时间】:2013-10-11 17:08:59
【问题描述】:

有人可以帮帮我吗? 当我单击按钮时,没有任何反应。我对android编程很陌生,所以请按我的理解回答。

(不要怀疑我的变量)

谢谢

@Override
public void onClick(View v) {

    Button preis = (Button) findViewById(R.id.essenpreis);
    preis.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            // Creating alert Dialog with one Button

            AlertDialog.Builder alertDialog = new AlertDialog.Builder(options.this);

            // Setting Dialog Title
            alertDialog.setTitle("Essenspreis");

            // Setting Dialog Message
            alertDialog.setMessage("Neuen Preis eintragen:");

            // Setting Icon to Dialog
            // alertDialog.setIcon(R.drawable.tick);

            // Setting OK Button
            alertDialog
                .setPositiveButton("YES", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog,int which) {

                            // Write your code here to execute after dialog closed
                        Toast.makeText(getApplicationContext(),"Preis geändert!", Toast.LENGTH_SHORT).show();
                        }
                    });

            // Showing Alert Message
            alertDialog.show();

        }
    });
  }                 
}

【问题讨论】:

  • 你确定onclicked被调用了吗?
  • onClick 你设置这个按钮点击什么?

标签: android dialog alert


【解决方案1】:

你的按钮点击监听器是在 onClick 接口的方法体中定义的,这就是你的对话框没有显示的原因,

这是显示警报对话框的方法

           Button preis = (Button) findViewById(R.id.essenpreis);

        // add button listener
        preis.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {

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

            // set title
            alertDialogBuilder.setTitle("Your Title");

            // set dialog message
            alertDialogBuilder
                .setMessage("Click yes to exit!")
                .setCancelable(false)
                .setPositiveButton("Yes",new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,int id) {
                        // if this button is clicked, close
                        // current activity
                        MainActivity.this.finish();
                    }
                  })
                .setNegativeButton("No",new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,int id) {
                        // if this button is clicked, just close
                        // the dialog box and do nothing
                        dialog.cancel();
                    }
                });

                // create alert dialog
                AlertDialog alertDialog = alertDialogBuilder.create();

                // show it
                alertDialog.show();
            }
        });
    }

【讨论】:

    【解决方案2】:

    请注意,在执行第一个 onClick 方法后,您将侦听器设置为 ID 为 R.id.essenpreis 的按钮,检查您是否将 click listener 分配给任何人,如果您从第一个onClick

            Button preis = (Button) findViewById( android.R.id.button1 );
            preis.setOnClickListener(new View.OnClickListener() {
                public void onClick(View view) {
                    // Creating alert Dialog with one Button
    
                    AlertDialog.Builder alertDialog = new AlertDialog.Builder( MainActivity.this );
    
                    // Setting Dialog Title
                    alertDialog.setTitle("Essenspreis");
    
                    // Setting Dialog Message
                    alertDialog.setMessage("Neuen Preis eintragen:");
    
                    // Setting Icon to Dialog
                    // alertDialog.setIcon(R.drawable.tick);
    
                    // Setting OK Button
                    alertDialog
                        .setPositiveButton("YES", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog,int which) {
    
                                    // Write your code here to execute after dialog closed
                                Toast.makeText(getApplicationContext(),"Preis geändert!", Toast.LENGTH_SHORT).show();
                                }
                            });
    
                    // Showing Alert Message
                    alertDialog.show();
    
                }
            }); 
    

    【讨论】:

      【解决方案3】:

      我假设 options.java 是您的主要源文件。

      如果你已经在 XML 中创建了布局,那么请给按钮 id 为:

      android:id = "@+id/button1"
      

      如果您还没有创建任何布局,请在 res 文件夹中创建一个文件 ma​​in.xml,如下所示

      <?xml version="1.0" encoding="utf-8"?>
      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:orientation="vertical" >
      
      <Button
          android:id="@+id/buttonAlert"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="Show Alert Box" />
      
      </LinearLayout>
      

      现在在活动中,我想这里是代码中的 Options.java。

      onCreate方法中编写如下代码:

      Button button = (Button)findViewById(R.id.button1);
      button.setOnClickListener(new OnClickListener() {
      
          @Override
          public void onClick(View arg0) {
      
              AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                  options.this);
      
              // set title
              alertDialogBuilder.setTitle("Essenspreis");
      
              // set dialog message
              alertDialogBuilder
                  .setMessage("Neuen Preis eintragen:")
                  .setCancelable(false)
                  .setPositiveButton("Yes",new DialogInterface.OnClickListener() {
                      public void onClick(DialogInterface dialog,int id) {
                          // if this button is clicked, close
      
                      }
                    })
                  .setNegativeButton("No",new DialogInterface.OnClickListener() {
                      public void onClick(DialogInterface dialog,int id) {
                          // if this button is clicked, just close
      
                      }
                  });
      
                  // create alert dialog
                  AlertDialog alertDialog = alertDialogBuilder.create();
      
                  // show it
                  alertDialog.show();
              }
          });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-12-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-03-29
        相关资源
        最近更新 更多