如何把警告框,两个按钮 [英] How to put two buttons on alert box

查看:154
本文介绍了如何把警告框,两个按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了简单的警告框的程序android.now我不得不把两个按钮确定和取消,但是当我运行该程序只显示,取消按钮......我的code是如下:

I have made a program of simple alert box in android.now i have to put two buttons "OK" and "cancel" but when i run the program it only shows ,the "cancel" button...my code is as below:

Main.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  
    }
});
    }
}

感谢您提前。

推荐答案

我有同样的问题。这是什么是做到了。

I had the same problem. This is what is did.

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

这工作正常。你必须数警报箱的按钮。

This works fine. You have to number the buttons for alert-box.

这篇关于如何把警告框,两个按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆