欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android 列表对话框 setItems

程序员文章站 2022-04-21 16:38:08
private Button button; private final CharSequence[] items = { "北京", "上海", "广州" }; @Override protected void onCreate(Bundle savedInstanceState) { super... ......
    private button button;
    private final charsequence[] items = { "北京", "上海", "广州" };

    @override
    protected void oncreate(bundle savedinstancestate) {
        super.oncreate(savedinstancestate);
        setcontentview(r.layout.activity_main);

        button = (button) findviewbyid(r.id.button);

        button.setonclicklistener(new view.onclicklistener() {

            @override
            public void onclick(view v) {
                // todo auto-generated method stub

                alertdialog.builder builder = new alertdialog.builder(mainactivity.this);
                builder.settitle("请选择以下城市");
                // builder.setmessage("请选择以下城市"); // 使用setmessage会覆盖setitems内容

                builder.setitems(items, new onclicklistener() {

                    @override
                    public void onclick(dialoginterface dialog, int which) {
                        // todo auto-generated method stub
                        charsequence sequence = items[which];
                        toast.maketext(mainactivity.this, "select " + sequence, 1).show();
                    }
                });

                alertdialog dialog = builder.create();
                dialog.show();
            }
        });
    }