Android开发:对话框

发布时间:2020-07-03 08:29:10 作者:斯莱特林
来源:网络 阅读:463
package com.xy.alterdialog;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.DialogInterface.OnMultiChoiceClickListener;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
public class Main extends Activity {
    private Button btn;
    private Button btn2;
    private Button btn3;
    private Button btn4;
      
    private final CharSequence[] items = {"北京","上海","广州"};
      
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.alterdialog);
          
        btn = (Button)findViewById(R.id.button1);
        btn2 = (Button)findViewById(R.id.button2);
        btn3 = (Button)findViewById(R.id.button3);
        btn4 = (Button)findViewById(R.id.button4);
          
        //AlterDialog
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                AlertDialog.Builder builder = new AlertDialog.Builder(Main.this);  
                builder.setMessage("Sure to Cancel?");
                builder.create();
                builder.setPositiveButton("Yes", new OnClickListener() {
                      
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                        dialog.dismiss();
                    }
                });
                builder.setNegativeButton("No", new OnClickListener() {
                      
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                        dialog.dismiss();
                    }
                });
                AlertDialog dialog = builder.create();
                dialog.show();
            }
        });
      
        //列表对话框
        btn2.setOnClickListener(new View.OnClickListener() {
              
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                AlertDialog.Builder builder = new AlertDialog.Builder(Main.this);
                builder.setTitle("提示");
                builder.setItems(items, new OnClickListener() {
                      
                    @Override
                    public void onClick(DialogInterface arg0, int arg1) {
                        // TODO Auto-generated method stub
                          
                    }
                });
                AlertDialog dialog = builder.create();
                dialog.show();
            }
        });
          
        //单选对话框
        btn3.setOnClickListener(new View.OnClickListener() {
              
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                AlertDialog.Builder builder = new AlertDialog.Builder(Main.this);
                builder.setTitle("请选择");
                builder.setSingleChoiceItems(items, -1, new OnClickListener() {
                      
                    @Override
                    public void onClick(DialogInterface arg0, int arg1) {
                        // TODO Auto-generated method stub
                          
                    }
                });
                AlertDialog dialog = builder.create();
                builder.setPositiveButton("确定", new OnClickListener() {
                      
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                          
                    }
                });
                builder.show();
            }
        });
          
        //复选对话框
        btn4.setOnClickListener(new View.OnClickListener() {
              
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                AlertDialog.Builder builder = new AlertDialog.Builder(Main.this);
                builder.setTitle("请选择");
                //第二个参数null:默认开始时没有被选中的选项
                builder.setMultiChoiceItems(items, null, new OnMultiChoiceClickListener() {
                      
                    @Override
                    public void onClick(DialogInterface dialog, int which,boolean isChecked) {
                        // TODO Auto-generated method stub
                          
                    }
                });
                builder.setPositiveButton("确定", new OnClickListener() {
                      
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                          
                    }
                });
                  
                AlertDialog dialog = builder.create();
                dialog.show(); 
            }
        });
          
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}


推荐阅读:
  1. 日期对话框DatePickerDialog
  2. Qt模态对话框 非模态对话框

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

对话框 android roi

上一篇:HBASE 0.98版本安装,三步曲:安装Zookeeper 3.4.5

下一篇:解决Ubuntu 18.04里的docker不能正常DNS解析问题

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》