Android常使用的控件(下)

Dialog

  对话框在各种应用或者游戏开发都会用到,当然也会以各种不同的方式出现。

  第一种:Toast(他不会打断当前正在操作的应用,在屏幕上显示一段时间(2秒左右)后消失)。

  代码如下:

Toast.makeText(this, "", Toast.LENGTH_LONG).show(); //参数1:Context对象 参数2:对话框提示的内同 参数3:对象框展示的方式

   第二种:Builder

    private Builder builder;
    private TextView txtTextView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(new BitmapBodyView(this));
        setContentView(R.layout.activity_main);
        txtTextView=(TextView)this.findViewById(R.id.lab);
        builder=new Builder(this);
        builder.setIcon(R.drawable.ic_launcher); //设置图标
        builder.setTitle("Dialog");//设置标题
        builder.setMessage("I'm Dialog");//设置标题内容
        builder.setPositiveButton("取消", new OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                txtTextView.setText("点击了取消");
            }
        });
        builder.setNegativeButton("确定", new OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                txtTextView.setText("点击了确定");
            }
        });
        builder.show();
    }

  Builder设置单选对话框:

    private Builder builder;
    private TextView txtTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // setContentView(new BitmapBodyView(this));
        setContentView(R.layout.activity_main);
        txtTextView = (TextView) this.findViewById(R.id.lab);
        builder = new Builder(this);
        builder.setIcon(R.drawable.ic_launcher); // 设置图标
        builder.setTitle("Dialog");// 设置标题
        builder.setSingleChoiceItems(new String[] { "单选一", "单选二" }, 1,  //为对话框设置单选框
                new OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub
                        txtTextView.setText("你选择了"+which);
                    }
                });
        builder.setPositiveButton("取消", new OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                txtTextView.setText("点击了取消"+which);
            }
        });
        builder.setNegativeButton("确定", new OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                txtTextView.setText("点击了确定"+which);
            }
        });
        builder.show();
    }

Builder设置多选对话框:

    private Builder builder;
    private TextView txtTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // setContentView(new BitmapBodyView(this));
        setContentView(R.layout.activity_main);
        txtTextView = (TextView) this.findViewById(R.id.lab);
        builder = new Builder(this);
        builder.setIcon(R.drawable.ic_launcher); // 设置图标
        builder.setTitle("Dialog");// 设置标题
        builder.setMultiChoiceItems(new String[] { "多选一", "多选二" },
                new boolean[] { false, false },
                new OnMultiChoiceClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which,
                            boolean isChecked) {
                        // TODO Auto-generated method stub
                        String msg=isChecked?"有":"没有";
                        txtTextView.setText("你"+msg+"选择了" + which);
                    }
                });
        builder.show();
    }

Builder设置列表选择

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // setContentView(new BitmapBodyView(this));
        setContentView(R.layout.activity_main);
        txtTextView = (TextView) this.findViewById(R.id.lab);
        builder = new Builder(this);
        builder.setIcon(R.drawable.ic_launcher); // 设置图标
        builder.setTitle("Dialog");// 设置标题
        builder.setItems(new String[]{"列表一","列表二"},new OnClickListener() {
            
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
            }
        });
        builder.show();

 

转载于:https://www.cnblogs.com/cindyOne/archive/2013/03/30/2990262.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值