0

我在android中制作了一个简单的警报框程序。现在我必须放置两个按钮“确定”和“取消”但是当我运行程序时它只显示“取消”按钮......我的代码如下:

主.java

public class MainActivity extends Activity {
Button btn;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
    btn = (Button)findViewById(R.id.button);
    final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
    btn.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        alertDialog.setTitle("Title");
        alertDialog.setMessage("Message");
        alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int which) {
              // TODO Add your code for the button here.
               Toast.makeText(getApplicationContext(), "well come", 1).show();
           }
        });
        alertDialog.setButton("cancel",new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                Toast.makeText(getApplicationContext(), "yoy have pressed cancel", 1).show();
            }
        });
        // Set the Icon for the Dialog
        alertDialog.setIcon(R.drawable.ic_launcher);
        alertDialog.show();
        // see http://androidsnippets.com/simple-alert-dialog-popup-with-title-message-icon-and-button  
    }
});
    }
}

先感谢您。

4

7 回答 7

4

我有同样的问题。这就是所做的。

public class MainActivity extends Activity {
    Button btn;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        btn = (Button)findViewById(R.id.button);
        final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
        btn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
            alertDialog.setTitle("Title");
            alertDialog.setMessage("Message");
            alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int which) {
                   Toast.makeText(getApplicationContext(), "well come", 1).show();
               }
            });
        alertDialog.setButton2("cancel",new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(getApplicationContext(), "yoy have pressed cancel", 1).show();
            }
        });
        // Set the Icon for the Dialog
        alertDialog.setIcon(R.drawable.ic_launcher);
        alertDialog.show();
        // see http://androidsnippets.com/simple-alert-dialog-popup-with-title-message-icon-and-button  
    }
});
    }
}

这工作正常。您必须为警报框的按钮编号。

于 2013-01-23T06:44:49.830 回答
2

简单的警报

 private AlertDialog AskOption()
 {
    AlertDialog myQuittingDialogBox =new AlertDialog.Builder(this) 
        //set message, title, and icon
        .setTitle("Title") 
        .setMessage("Message") 
        .setIcon(R.drawable.icon)

        .setPositiveButton("yes", new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int whichButton) { 

                //your code
            }   
        })

        .setNeutralButton("No", new DialogInterface.OnClickListener() { 
            public void onClick(DialogInterface dialog, int whichButton) { 

                dialog.dismiss();
         } 
        })

        .create();
        return myQuittingDialogBox;

    }

用法

AlertDialog al = AskOption();
al.show();
于 2013-01-23T05:28:31.470 回答
1

像这样改变我们安排setPositiveButton的是oksetNegativeButtoncancel按钮。

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

 alertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
       public void onClick(DialogInterface dialog, int which) {
          // TODO Add your code for the button here.
           Toast.makeText(getApplicationContext(), "well come", 1).show();
       }
    });
    alertDialog.setNegativeButton("cancel",new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub
            Toast.makeText(getApplicationContext(), "yoy have pressed cancel", 1).show();
        }
    });
于 2013-01-23T05:24:21.693 回答
1

添加按钮的正确方法:

对于积极

alertDialog.setPositiveButton("OK",new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog,int id) {
        // perform your action
    }
});

对于负

alertDialog.setNegativeButton("Cancel",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();
    }
});

Android 警报对话框示例

于 2013-01-23T05:25:03.317 回答
0
public void showDialog(Activity activity, String title, CharSequence message) {
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
if (title != null)
    builder.setTitle(title);
builder.setMessage(message);
builder.setPositiveButton("OK", null);
        builder.setNegativeButton("Cancel", null);
builder.show();

}

于 2013-01-23T05:25:05.273 回答
0

您可以使用AlertDialog.Builder类。有一些方法可以使用这个构建器类在对话框上放置按钮。

  1. 设置正按钮
  2. 设置负按钮
  3. 设置中性按钮
于 2013-01-23T05:27:38.410 回答
0

您需要使用 setPositiveButton() 和 setnegativeButton() 之类的,

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

public class MainActivity extends Activity {
Button btn;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
    btn = (Button)findViewById(R.id.button);
    final AlertDialog alertDialog = new AlertDialog.Builder(this).create();
btn.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        alertDialog.setTitle("Title");
        alertDialog.setMessage("Message");
        alertDialog.setPositiveButton("OK",new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog,int id) {
                    // if this button is clicked, close
                    // current activity
                     Toast.makeText(getApplicationContext(), "well come", 1).show();
                }
              });
        alertDialog.setNegativeButton("Cancel",new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog,int id) {
                    // if this button is clicked, just close
                    // the dialog box and do nothing
                     Toast.makeText(getApplicationContext(), "yoy have pressed cancel", 1).show();
                }
            });
        // Set the Icon for the Dialog
        alertDialog.setIcon(R.drawable.ic_launcher);
        alertDialog.show();
        // see http://androidsnippets.com/simple-alert-dialog-popup-with-title-message-icon-and-button  
    }
});
    }
}
于 2013-01-23T05:33:25.513 回答