繁体   English   中英

为什么我的警报对话框没有出现?

[英]Why does my alert dialog not appear?

有人可以帮我吗? 当我单击按钮时,什么也没有发生。 我对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();

        }
    });
  }                 
}

按钮的clicklistener是在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();
            }
        });
    }

请注意,在执行第一个onClick方法之后,您要将侦听器设置为ID为R.id.essenpreis按钮,请检查是否将该click listener器分配给任何人,如果您从第一个onClick提取button设置,则代码会工作

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

            }
        }); 

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

如果您已使用XML创建布局,则请为按钮提供id为:

android:id = "@+id/button1"

如果尚未创建任何布局,请在res文件夹中创建一个文件main.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();
        }
    });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM