对话框简介

android提供了丰富的对话框支持,支持四种如下的对话框。
这里写图片描述
Android对话框AlertDialog-android学习之旅(四十二)_第1张图片

AlertDialog简介

Android对话框AlertDialog-android学习之旅(四十二)_第2张图片
这里写图片描述
Android对话框AlertDialog-android学习之旅(四十二)_第3张图片

介绍上面六个方法的代码示例

setMessage()

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" >    <TextView  android:layout_width="match_parent" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:text="Large Text" android:id="@+id/text02" />    <Button  android:layout_width="match_parent" android:layout_height="wrap_content" android:text="setMessage" android:id="@+id/button" android:onClick="dialog"/></LinearLayout>
package peng.liu.test;import android.app.Activity;import android.app.AlertDialog;import android.app.Notification;import android.app.NotificationManager;import android.app.PendingIntent;import android.app.TabActivity;import android.content.DialogInterface;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.TabHost;import android.widget.TextView;public class MainActivity extends Activity {    TextView text02;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        text02 = (TextView) findViewById(R.id.text02);        Button setMessage = (Button) findViewById(R.id.button);    }    public void dialog(View view){        AlertDialog.Builder builder = new AlertDialog.Builder(this).setIcon(R.drawable.ic_launcher)                .setTitle("dialog")                .setMessage("hello world");        setPositiveButton(builder);        setNegitiveButton(builder);        builder.create().show();    }    public void setPositiveButton(AlertDialog.Builder builder){        builder.setPositiveButton("确定",new DialogInterface.OnClickListener() {            @Override            public void onClick(DialogInterface dialogInterface, int i) {                     text02.setText("单击了确定按钮");            }        });    }    public void setNegitiveButton(AlertDialog.Builder builder){        builder.setNegativeButton("取消",new DialogInterface.OnClickListener() {            @Override            public void onClick(DialogInterface dialogInterface, int i) {                text02.setText("单击了取消按钮");            }        });    }}

简单列表对话框setItems()

需要传入一个数组或者数组的资源id

package peng.liu.test;import android.app.Activity;import android.app.AlertDialog;import android.app.Notification;import android.app.NotificationManager;import android.app.PendingIntent;import android.app.TabActivity;import android.content.DialogInterface;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.TabHost;import android.widget.TextView;public class MainActivity extends Activity {    TextView text02;    private String[] items = new String[]{            "java","python","html","css"    };    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        text02 = (TextView) findViewById(R.id.text02);        Button setMessage = (Button) findViewById(R.id.button);    }    public void dialog(View view){        AlertDialog.Builder builder = new AlertDialog.Builder(this).setIcon(R.drawable.ic_launcher)                .setTitle("dialog")                .setItems(items,new DialogInterface.OnClickListener() {                    @Override                    public void onClick(DialogInterface dialogInterface, int i) {                        text02.setText(items[i]);                    }                });        setPositiveButton(builder);        setNegitiveButton(builder);        builder.create().show();    }    public void setPositiveButton(AlertDialog.Builder builder){        builder.setPositiveButton("确定",new DialogInterface.OnClickListener() {            @Override            public void onClick(DialogInterface dialogInterface, int i) {                     text02.setText("单击了确定按钮");            }        });    }    public void setNegitiveButton(AlertDialog.Builder builder){        builder.setNegativeButton("取消",new DialogInterface.OnClickListener() {            @Override            public void onClick(DialogInterface dialogInterface, int i) {                text02.setText("单击了取消按钮");            }        });    }}

单选列表对话框setSingleChooseItems

参数是数组,sursor,或者ListAdapter。

package peng.liu.test;import android.app.Activity;import android.app.AlertDialog;import android.app.Notification;import android.app.NotificationManager;import android.app.PendingIntent;import android.app.TabActivity;import android.content.DialogInterface;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.TabHost;import android.widget.TextView;public class MainActivity extends Activity {    TextView text02;    private String[] items = new String[]{            "java","python","html","css"    };    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        text02 = (TextView) findViewById(R.id.text02);        Button setMessage = (Button) findViewById(R.id.button);    }    public void dialog(View view){        AlertDialog.Builder builder = new AlertDialog.Builder(this).setIcon(R.drawable.ic_launcher)                .setTitle("dialog")                //1表示第二个框被选中                .setSingleChoiceItems(items,1,new DialogInterface.OnClickListener() {                    @Override                    public void onClick(DialogInterface dialogInterface, int i) {                        text02.setText(items[i]);                    }                });        setPositiveButton(builder);        setNegitiveButton(builder);        builder.create().show();    }    public void setPositiveButton(AlertDialog.Builder builder){        builder.setPositiveButton("确定",new DialogInterface.OnClickListener() {            @Override            public void onClick(DialogInterface dialogInterface, int i) {                     text02.setText("单击了确定按钮");            }        });    }    public void setNegitiveButton(AlertDialog.Builder builder){        builder.setNegativeButton("取消",new DialogInterface.OnClickListener() {            @Override            public void onClick(DialogInterface dialogInterface, int i) {                text02.setText("单击了取消按钮");            }        });    }}

多选列表对话框 setMultiChooseItems

参数是数组或者cursor

package peng.liu.test;import android.app.Activity;import android.app.AlertDialog;import android.app.Notification;import android.app.NotificationManager;import android.app.PendingIntent;import android.app.TabActivity;import android.content.DialogInterface;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.TabHost;import android.widget.TextView;public class MainActivity extends Activity {    TextView text02;    private String[] items = new String[]{            "java","python","html","css"    };    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        text02 = (TextView) findViewById(R.id.text02);        Button setMessage = (Button) findViewById(R.id.button);    }    public void dialog(View view){        AlertDialog.Builder builder = new AlertDialog.Builder(this).setIcon(R.drawable.ic_launcher)                .setTitle("dialog")                //布尔数组表示第二个和第四个被选中                .setMultiChoiceItems(items,new boolean[]{false,true,false,true},null);        setPositiveButton(builder);        setNegitiveButton(builder);        builder.create().show();    }    public void setPositiveButton(AlertDialog.Builder builder){        builder.setPositiveButton("确定",new DialogInterface.OnClickListener() {            @Override            public void onClick(DialogInterface dialogInterface, int i) {                     text02.setText("单击了确定按钮");            }        });    }    public void setNegitiveButton(AlertDialog.Builder builder){        builder.setNegativeButton("取消",new DialogInterface.OnClickListener() {            @Override            public void onClick(DialogInterface dialogInterface, int i) {                text02.setText("单击了取消按钮");            }        });    }}

自定义列表项对话框 setAdapter

参数是Adapter,该方法和setSingleChooseItems都可以接受Adapter作为参数。

package peng.liu.test;import android.app.Activity;import android.app.AlertDialog;import android.app.Notification;import android.app.NotificationManager;import android.app.PendingIntent;import android.app.TabActivity;import android.content.DialogInterface;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.widget.ArrayAdapter;import android.widget.Button;import android.widget.TabHost;import android.widget.TextView;public class MainActivity extends Activity {    TextView text02;    private String[] items = new String[]{            "java","python","html","css"    };    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        text02 = (TextView) findViewById(R.id.text02);        Button setMessage = (Button) findViewById(R.id.button);    }    public void dialog(View view){        AlertDialog.Builder builder = new AlertDialog.Builder(this).setIcon(R.drawable.ic_launcher)                .setTitle("dialog")                //布尔数组表示第二个和第四个被选中                .setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,items),null);        setPositiveButton(builder);        setNegitiveButton(builder);        builder.create().show();    }    public void setPositiveButton(AlertDialog.Builder builder){        builder.setPositiveButton("确定",new DialogInterface.OnClickListener() {            @Override            public void onClick(DialogInterface dialogInterface, int i) {                     text02.setText("单击了确定按钮");            }        });    }    public void setNegitiveButton(AlertDialog.Builder builder){        builder.setNegativeButton("取消",new DialogInterface.OnClickListener() {            @Override            public void onClick(DialogInterface dialogInterface, int i) {                text02.setText("单击了取消按钮");            }        });    }}

自定义View对话框

自定义的任何的View组件

package peng.liu.test;import android.app.Activity;import android.app.AlertDialog;import android.content.DialogInterface;import android.os.Bundle;import android.view.View;import android.view.ViewGroup;import android.widget.ArrayAdapter;import android.widget.Button;import android.widget.ImageView;import android.widget.TextView;public class MainActivity extends Activity {    TextView text02;    private String[] items = new String[]{            "java","python","html","css"    };    ImageView image;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        text02 = (TextView) findViewById(R.id.text02);        Button setMessage = (Button) findViewById(R.id.button);        image = new ImageView(this);        image.setImageResource(R.drawable.ic_launcher);        image.setScaleType(ImageView.ScaleType.FIT_XY);        image.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));    }    public void dialog(View view){        AlertDialog.Builder builder = new AlertDialog.Builder(this).setIcon(R.drawable.ic_launcher)                .setTitle("dialog")                //布尔数组表示第二个和第四个被选中                .setView(image);    }    public void setPositiveButton(AlertDialog.Builder builder){        builder.setPositiveButton("确定",new DialogInterface.OnClickListener() {            @Override            public void onClick(DialogInterface dialogInterface, int i) {                     text02.setText("单击了确定按钮");            }        });    }    public void setNegitiveButton(AlertDialog.Builder builder){        builder.setNegativeButton("取消",new DialogInterface.OnClickListener() {            @Override            public void onClick(DialogInterface dialogInterface, int i) {                text02.setText("单击了取消按钮");            }        });    }}

更多相关文章

  1. 学习笔记----Android的对话框
  2. 改变Android 对话框位置及边框
  3. Android - 按钮组件详解
  4. Android点击左右按钮实现左右滑动页面切换
  5. Android中设计具有背景图的按钮—ImageButton的焦点及事件处理
  6. android 对话框实例
  7. Android 中的 BACK 和 HOME 按钮的区别

随机推荐

  1. Android上开发Android软件
  2. android - 为安全而设计 - 2 - 开发文档
  3. 深入理解Android内核——Android启动分析
  4. Android(安卓)Studio下载安装教程及开发
  5. android下socket的ip配置
  6. Android的多媒体框架OpenCore介绍
  7. 用VS2010开发Android应用的配置方法
  8. Android(安卓)根文件系统分析
  9. Android(安卓)libv4l2-android 摄像头视
  10. android layout属性简介