您的位置:首页 > 移动开发 > Android开发

Android Dialog简单使用

2017-04-12 22:48 323 查看
虽然现在官方已经推荐用DialogFragemnt来代替Dialog,但是我还是觉得Dialog的基本使用及其显示原理有必要理解的。后续文章我会逐步向大家介绍Fragment,DialogFragment等相关的知识。本文章只是简单介绍android framework定义好的Dialog的基本使用。

1.Dialog

Dialog是android对话框的基类,封装了对话框相关的逻辑(本文先不讲它)。

2.AlertDialog

Dialog的直接子类,这个类非常重要也是很常用的类。AlertDialog对话框把区域划分为三个区域如下图分别为:



1.标题区域 [可添加自定义布局]

2.内容区域[可添加自定义布局]

3.按钮区域[横向排列最多可以显示三个按钮]

我们可以向某个区域中添加内容但如果某个区域内没有添加任何内容,则该区域不会显示

下面是AlertDialog的一个简单用例

public void showDialog(Activity activity){
//AlertDialog的创建用到AlertDialog.Builder,AlertDialog.Builder构造函数中的Context必须传Activity的实例(先记着)
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
//设置对话框标题,该标题会显示在标题区域中
builder.setTitle("dialog title")
//设置对话框图标,该标题会显示在标题区域中
.setIcon(null)
//setMessage方法中的内容会显示在内容区域中
.setMessage("dialog content")
/*以下三个setXXXButton(CharSequence text, final OnClickListener listener)方法
都向对话框的按钮区域添加了一个按钮,方法的第一个参数是按钮文本,第二个是按钮点击监听器。
注意按钮的顺序和代码的添加顺序无关,按钮的位置是固定的(如图1),只有调用了对应的setXXXButton()
方法该按钮才显示。
*/
.setNegativeButton("button2", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
})
.setPositiveButton("button1", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
}).setNeutralBu
4000
tton("button3", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
})
//真正实例化AlertDialog对象
.create()
//显示对话框
.show();
}


3.AlertDialog添加列表

1).添加文本列表

public void showListDialog(Activity activity){
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
final String[] items = new String[]{"111","222","333"};
/*这里用setItems(CharSequence[] items, final OnClickListener listener)
用setAdapter(final ListAdapter adapter, final OnClickListener listener),
setCursor(final Cursor cursor, final OnClickListener listener)也可达到同样的效果*/
builder.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Log.i("zxz",items(which));
}
}) .setAdapter().setCursor().show();
}


2).添加单选框或复选框列表

public void showChoiceDialog(Activity activity){
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
final String[] items = new String[]{"111","222","333"};
final boolean[ ]isChecks = new boolean[]{false,true,true};
//添加单选框列表,第二个参数为默认选中的索引,setSingleChoiceItems还有另外几个重载的函数读者可自行查看AlertDialog文档
builder.setSingleChoiceItems(items, 0, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Log.i("zxz",items(which));
}
}).show();

//添加复选框列表,第二个参数指定对应的item默认是否被选中,setMultiChoiceItems还有另外几个重载的函数读者可自行查看AlertDialog文档
/*builder.setMultiChoiceItems(items, isChecks, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
Log.i("zxz",items(which) + " is" + (isChecked ? " checked" : " not check"));
}
}).show();*/
}


纯文本列表点击任意一个item对话框消失,单选框和复选框列表的item被选择时对话框不会消失。我们务必以在item的onClick回调中保存列表状态,一旦我们触发对话框关闭就再也拿不到列表状态了。以上列表都是添加到了内容区域中的

4.添加自定义布局

很多时候AlertDialog自带的ui是无法满足我们平常开发的需求的,比如说对话框大小,对话框里面的布局以及一些字体颜色大小等等。所以如果要满足各自产品要求,做出漂亮的对话框就务必用到自定义布局。如上文讲到的AlertDialog对话框的三个区域中标题区域和内容区域是可以自定添加自定义布局的,而按钮区域不可以,所以自定义时最好不要在按钮区域添加按钮,而是把自定义的按钮一起放在内容区域中然后添加onClickListener。其中setCustomTitle()用于把自定义布局添加到标题区域,seView()用于把自定义布局添加到内容区域,下面是用自定义View做的一个简单的登录对话框的事例.

xml文件

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:background="@color/white"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:orientation="vertical"
android:gravity="center_horizontal">

<TextView
android:id="@+id/login_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="登录对话框"
android:textSize="23sp"
android:textColor="#323232"/>

<EditText
android:id="@+id/account_et"
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="#00000000"
android:gravity="center|left"
android:textSize="20sp"
android:hint="帐号"/>

<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#E6E6E6"/>

<EditText
android:id="@+id/password_et"
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="#00000000"
android:gravity="center|left"
android:textSize="20sp"
android:inputType="textPassword"
android:hint="密码"/>

<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#E6E6E6"/>
<!--CornerButton是我自定义的圆角按钮 -->
<com.example.myapplication.view.CornerView.CornerButton
android:id="@+id/login_btn"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="10dp"
android:text="登录"
android:textSize="26dp"
android:textColor="#ffffff"
app:radius="25dp"
app:backgroundColor="#0096FF"/>

</LinearLayout>

</FrameLayout>


java代码

public void showCustomDialog(Activity activity){
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
//通过LayoutInflater获取布局
LayoutInflater lf = LayoutInflater.from(activity);
View customView = lf.inflate(R.layout.layout_dialog, null);
//设置自定义View并创建AlertDialog
final AlertDialog alertDialog = builder.setView(customView).create();
final EditText accountEt = (EditText) customView.findViewById(R.id.account_et);
final EditText passwordEt = (EditText) customView.findViewById(R.id.password_et);
Button loginBtn = (Button) customView.findViewById(R.id.login_btn);
loginBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//点击按钮打印帐号密码,并将对话框关闭
Log.i("zxz","帐号:" + accountEt.getText().toString());
Log.i("zxz","密码:" + passwordEt.getText().toString());
alertDialog.dismiss();
}
});
alertDialog.show();
}


下面是效果图和打印输出







5.AlertDialog的子类

AlertDialog有三个子类,分别为:

1.DatePickerDialog,日期选择器,提供给ui用于选择日期

2.TimePickerDialog 时间选择器,提供给ui用于选择时间

3.ProgressDialog 用于显示一个progress,就是一个圈在转,有STYLE_SPINNER和STYLE_HORIZONTAL两种类型

其实AlertDialog的子类内部就是封装了对应功能的逻辑,用setView方法把ui控件添加到内容区域中的,用法非常简单。

public void showDatePickerDialog(Activity activity){
new DatePickerDialog(this,new DatePickerDialog.OnDateSetListener(){

@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
Log.i("zxz", year + "-" + monthOfYear + "-" + dayOfMonth);
}
//默认显示2017年5月12日
},2017,4,12).show();
}

public void showTimePickerDialog(Activity activity) {
//最后一个参数指定是否为24小时制
new TimePickerDialog
9ccc
(this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
Log.i("zxz", hourOfDay + ":" + minute);
}
//默认显示22点30分
}, 22, 30, true).show();
}

public void showProgressDialog(Activity activity) {
//第二个参数知道进度条类型,默认为STYLE_SPINNER
new ProgressDialog(activity, ProgressDialog.STYLE_HORIZONTAL).show();
}


效果很不错







6.小结

至此,android的对话的简单使用就介绍完了,总结一下:对话框常用的类是AlertDialog,该类布局分为三部分,其中内容区域可添加文本,文本列表,单选框复选框列表和自定义布局,标题区域可添加标题信息和图标,按钮区域可最多添加三个按钮。AlertDialog的三个子类1.DatePickerDialog,TimePickerDialog,ProgressDialog 为我们提供了选择日期时间以及加载圈的功能。

但是这里面还有一些问题,比如AlertDialog虽然可以让我们添加自定义布局,但我们只能把布局作为子空间添加进去,并不能改变对话框最底层控件的大小。自己尝试以下就会发现,无论自定义布局最外层控件的layout_width是多少AlertDialog的宽都是固定的,这该怎么办呢?高度可以改变但并不能超过最大高度而且有时候layout_heigh设为tmatch_parent却没把显示出来,这又是为什么呢?如果我们希望对话框带有圆角呢?请看下一篇。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: