Alertdialog不会出现

时间:2014-01-24 21:39:12

标签: java android class alert

当我按下按钮时,此处不显示警告对话框的代码: 请你告诉我它有什么问题,以及我能做些什么来解决它。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.row_item);
    Button bDR = (Button) findViewById(R.id.bDR);
    bDR.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View v) {
            AlertDialog.Builder builder = new AlertDialog.Builder(DeleteRenameList.this);
            builder.setCancelable(true);
            builder.setMessage("Would you like to delete or rename the list?");
            builder.setNegativeButton("Delete", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {


                }
            });
            builder.setNeutralButton("Rename", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {


                }
            });

        AlertDialog alert = builder.create();
        alert.show();

}

    });

请帮忙

2 个答案:

答案 0 :(得分:0)

请尝试以下格式:

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {

    Context context = this;
    Button button;

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        button = (Button) findViewById(R.id.buttonAlert);

        // add button listener
        button.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("Would you like to delete or rename the list?")
                .setCancelable(false)
                .setPositiveButton("Delete",new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,int id) {

                    }
                  })
                .setNegativeButton("Rename",new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,int id) {

                    }
                });

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

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

Source

答案 1 :(得分:0)

试试这个

@Override
 protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.row_item);
Button bDR = (Button) findViewById(R.id.bDR);
bDR.setOnClickListener(new OnClickListener(){

    @Override
    public void onClick(View v) {
        AlertDialog.Builder builder = new 
         AlertDialog.Builder(DeleteRenameList.this).create();
        builder.setCancelable(true);
        builder.setMessage("Would you like to delete or rename the list?");
        builder.setNegativeButton("Delete", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {


            }
        });
        builder.setNeutralButton("Rename", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {


            }
        });

    AlertDialog.show();

    }

    });