android其他类型的对话框

来源:互联网 发布:二次元网页源码 编辑:程序博客网 时间:2024/05/29 06:58

上次已经说过那种比较简单的alertDialog,这次我在上次的基础上加大了一点难度,对话框的主体内容使用列表、单选框、复选框。以及自定义对话框的使用。这次的内容可能在自定义对话框上比较复杂一些。其他几种都是比较简单的

1.列表类型的对话框

        final CharSequence[] items={"海南","浙江","武汉"};        AlertDialog.Builder builder=new AlertDialog.Builder(this);        builder.setTitle("你最想去什么地方");        builder.setItems(items,new DialogInterface.OnClickListener() {                        @Override            public void onClick(DialogInterface dialog, int item) {                // TODO Auto-generated method stub                Toast.makeText(getApplicationContext(), items[item],Toast.LENGTH_LONG).show();            }         });              //创建对话框        AlertDialog alert = builder.create();          //把对话框显示在手机屏幕上        alert.show();
下面是运行效果图


2.单选按钮类型的对话框

 builder.setSingleChoiceItems(items,-1,new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int item) {// TODO Auto-generated method stubToast.makeText(getApplicationContext(), items[item],Toast.LENGTH_LONG).show();}});

这里的-1表示当选按钮全部是未选中状态,0则表示第一个是选中状态


3.复选框类型

boolean[] b={false,false,true};

builder.setMultiChoiceItems(items,b,new DialogInterface.OnMultiChoiceClickListener() {        @Overridepublic void onClick(DialogInterface dialog, int which, boolean isChecked) {// TODO Auto-generated method stubStringBuilder str=new StringBuilder();if(isChecked){Toast.makeText(getApplicationContext(), str.append(items[which]),Toast.LENGTH_LONG).show();}}});


4.自定义对话框

这里布局还是写在xml文件里,下面是我的xml文件

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/layout_root" android:orientation="horizontal"android:layout_width="fill_parent" android:layout_height="fill_parent"android:padding="10dp"><ImageView android:id="@+id/image" android:layout_width="wrap_content"android:layout_height="fill_parent" android:layout_marginRight="10dp" /><TextView android:id="@+id/text" android:layout_width="wrap_content"android:layout_height="fill_parent" android:textColor="#FFF" /></LinearLayout>

我把R文件也贴出来

/* AUTO-GENERATED FILE.  DO NOT MODIFY. * * This class was automatically generated by the * aapt tool from the resource data it found.  It * should not be modified by hand. */package org.lxh.activity;public final class R {    public static final class attr {    }    public static final class drawable {        public static final int android=0x7f020000;        public static final int icon=0x7f020001;    }    public static final class id {        public static final int image=0x7f050001;        public static final int layout_root=0x7f050000;        public static final int text=0x7f050002;    }    public static final class layout {        public static final int custom_dialog=0x7f030000;        public static final int main=0x7f030001;    }    public static final class string {        public static final int app_name=0x7f040001;        public static final int hello=0x7f040000;        public static final int text=0x7f040002;    }}

下面就是展开布局的代码了,大家应该可以看懂

//使用LayoutInflater展开布局        LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);         //取得布局对象        View layout = inflater.inflate(R.layout.custom_dialog,  (ViewGroup) findViewById(R.id.layout_root));          TextView text = (TextView) layout.findViewById(R.id.text);          text.setText("Hello, this is a custom dialog!");          ImageView image = (ImageView) layout.findViewById(R.id.image);          image.setImageResource(R.drawable.android);          AlertDialog.Builder builder = new AlertDialog.Builder(this);          builder.setView(layout);          AlertDialog alertDialog = builder.create();         alertDialog.setTitle("我的自定义对话框");        alertDialog.show();


很简单吧,暂时就到这里吧。


原创粉丝点击