33

これはアラートの私のコードで、ボタンが押されたときにメッセージを表示します。OKボタンを閉じさせるにはどうすればよいですか?

    Button bm1 = (Button) findViewById(R.id.button1);
    bm1.setOnClickListener(new View.OnClickListener() {
        
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            AlertDialog alert = new AlertDialog.Builder(screen4.this).create();
            alert.setTitle("Doctor");
            alert.setMessage("message");
            alert.setCancelMessage(null);
            alert.show();
        }
    });
}}      

入れましalert.setCancelMessage(null)たが、閉じるボタンが表示されません。

解決:

      Button bm1 = (Button) findViewById(R.id.button1);
       bm1.setOnClickListener(new View.OnClickListener() {
        
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Builder alert = new AlertDialog.Builder(screen4.this);
            alert.setTitle("Doctor");
            alert.setMessage("message");
            alert.setPositiveButton("OK", null);
            alert.show();
        }
    });
4

9 に答える 9

48

alertdialogを次のように変更します。

Builder alert = new AlertDialog.Builder(screen4.this);
alert.setTitle("Doctor");
alert.setMessage("message");
alert.setPositiveButton("OK",null);
alert.show();    
于 2013-01-18T11:56:24.787 に答える
12

これを使用して、ダイアログの[OK]ボタンと[キャンセル]ボタンの両方を作成できます。

AlertDialog.Builder builder = new AlertDialog.Builder(
                        getApplicationContext());
                builder.setCancelable(true);
                builder.setTitle("Title");
                builder.setInverseBackgroundForced(true);
                builder.setPositiveButton("Yes",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog,
                                    int which) {
                                dialog.dismiss();
                            }
                        });
                builder.setNegativeButton("No",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog,
                                    int which) {
                                dialog.dismiss();
                            }
                        });
                AlertDialog alert = builder.create();
                alert.show();
于 2013-01-18T11:56:33.367 に答える
4

それは、ポジティブボタンを使用するだけで実行できませんでしたか?

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Look at this dialog!")
       .setCancelable(false)
       .setPositiveButton("OK", new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int id) {
                //do things
           }
       });
AlertDialog alert = builder.create();
alert.show();
于 2015-02-21T10:25:00.280 に答える
1

あなたはそれを使用してそれを行うことができsetPositiveButtonますsetNegativeButton

    alert.setPositiveButton("OK", new OnClickListener() {

        @Override
        public void onClick(DialogInterface alert, int which) {
            // TODO Auto-generated method stub
            //Do something
            alert.dismiss();
        }
    });
    alert.setNegativeButton("Cancel", new OnClickListener() {

        @Override
        public void onClick(DialogInterface alert, int which) {
            // TODO Auto-generated method stub
            alert.dismiss();
        }
    });

編集:

alert.setButton("OK", new DialogInterface.OnClickListener() {

    @Override
    public void onClick(DialogInterface dialog, int which) {
        // TODO Auto-generated method stub
        // Do something
        alert.dismiss();
        }
    });
alert.setButton("Cancel", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        // TODO Auto-generated method stub

        alert.dismiss();
        }
    });
于 2013-01-18T11:55:16.470 に答える
1
    bt = (Button) findViewById(R.id.button1);
    bt.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            ab = new AlertDialog.Builder(MainActivity.this);

            ab.setTitle("confirm");
            ab.setIcon(R.drawable.ic_launcher);
            ab.setMessage("Do u want to close this application?");

            ab.setPositiveButton("ok", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int i) {
                    Toast.makeText(MainActivity.this, "ok", Toast.LENGTH_SHORT).show();
                }
            });

            ab.setNegativeButton("cancle", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int i) {
                    Toast.makeText(MainActivity.this, "cancle", Toast.LENGTH_SHORT).show();
                }
            });

            ab.show();
        }
    });
于 2015-03-03T09:43:28.390 に答える
0

Check the AlertDialog.Builder page in the API: http://developer.android.com/reference/android/app/AlertDialog.Builder.html

You can set positive, neutral, and negative buttons using the setPositiveButton, setNeutralButton and setNegativeButton methods, respectively

于 2013-01-18T11:58:45.887 に答える
0
Button bm1 = (Button) findViewById(R.id.button1);
    bm1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            AlertDialog alert = new AlertDialog.Builder(screen4.this).create();
            alert.setTitle("Doctor");
            alert.setMessage("message");
            alert.setCancelMessage(null);

            alert.setButton("OK", new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int which) {
                    //do somthing or dismiss dialog by                dialog.dismiss();

                }
            });

            alert.show();
        }
    });
}}      
于 2013-01-18T11:57:00.043 に答える
0

alert.setPositiveButtonまたはalert.setNegativeButton

于 2013-01-18T11:55:14.643 に答える
0
    Button bm1 = (Button) findViewById(R.id.button1);
bm1.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
    final AlertDialog dialog = new AlertDialog.Builder(screen4.this).create();
    dialog.setTitle("Doctor");
    dialog.setMessage("message");
    dialog.setButton("Ok",new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int id) {
            // here you can add functions

           }
    });
    dialog.setButton2("Cancel",new DialogInterface.OnClickListener() {
       public void onClick(DialogInterface dialog, int id) {
        // here you can add functions
               dialog.dismiss();
    }
    });
    dialog.show();
    }};      
于 2013-01-18T12:02:05.853 に答える