0

有人可以帮帮我吗?当我单击按钮时,没有任何反应。我对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();

        }
    });
  }                 
}
4

3 回答 3

1

您的按钮 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();
            }
        });
    }
于 2013-10-11T17:16:54.783 回答
0

我假设 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();
        }
    });
于 2013-10-11T17:38:49.817 回答
0

请注意,在执行R.id.essenpreis第一个方法后,您将侦听器设置为带有 id 的按钮onClick,检查您是否将其分配给任何人,如果您从第一个方法中click listener提取设置,您的代码将起作用buttononClick

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

            }
        }); 
于 2013-10-11T17:16:34.443 回答