【Android】- UI常用组件 -Dialog对话框
一、AlertDialog
1.常用方法
void setTitle(CharSequence title) 设置Dialog窗口的标题。
void setTitle(int titleId) 设置Dialog窗口的标题。
setMessage(message: CharSequence!)
void create() 强制性立即创建对话框Dialog。
void show() 启动Dialog并在屏幕上显示。
更多详情:
AlertDialong的文档
2.一些特点
3.设置方法
例子:点击按钮出现对话框。
这节的代码都是在onCreate中通过findViewById直接设置了点击事件监听器,然后在监听器中设置对话框。如果有多个按钮,为了代码更模块化,可以在Activity中写一个处理多个按钮点击事件的监听函数。如
public void myClick(View v){
switch (v.getId()){
case R.id.按钮1的id:
break;
case R.id.按钮2的id:
break;
……
case R.id.按钮n的id:
break;
}
需要注意的是,别忘了在xml布局文件中,为相关按钮设置onClick属性,如 android:onClick=“myClick” ,其中,myClick就是点击事件监听器的方法名。
法一(更常用)
利用AlertDialog.Builder类创建对象,并用这个类提供的方法设置对话框。
需要注意的是,“ AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);”这个MainActivity.this是生成对话框的上下文,因为这段代码是在onCreate的findviewById返回的view的setOnClickListener方法里new的点击事件监听器中,所以this是事件监听器对象,MainActivity.this才是Activity对象。如果是在Activity中写一个myClick函数,然后每个标签里生成对话框,那么这个参数直接是this就行。
AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
builder.setTitle("退出?");
builder.setMessage("是否要退出程序?");
builder.setPositiveButton("yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
finish();
}
});
builder.setNegativeButton("no", null);
builder.show();
法二:直接用AlertDialog类创建对象,从代码中可以看出,这个方法更麻烦的地方在于,在构造的时候得调用create方法,设置对话框按钮时,不能直接设置确定取消按钮,而是只有一个setButton方法,然后在参数中表明是positive还是negative,并且按钮的响应事件不能为null,哪怕不做任何操作,也得new一个点击事件监听器。
AlertDialog alg=new AlertDialog.Builder(MainActivity.this).create();
alg.setTitle("退出?");
alg.setMessage("是否要退出程序?");
alg.setButton(DialogInterface.BUTTON_POSITIVE, "yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
finish();
}
});
alg.setButton(DialogInterface.BUTTON_NEGATIVE, "no", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
alg.show();
总结一下生成AlertDialog有以下几步:
生成一个AlertDialog.Builder或者AlertDialog类对象,设置这个对话框对象的标题、文本和按钮(包括按钮文本、按钮点击事件监听器等)、显示这个对话框。
二、自定义Dialog
即自己定义对话框的样式。要分这几步:设置布局、设置style、定义Dialog、显示。
3.设置方法
在layout文件夹下新建一个布局作为对话框的样式。可以在最外层LinearLayout中加入背景图,里层嵌套按钮和关于提示的文本(示例代码没加文本)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_gravity="center"
android:gravity="bottom|center_horizontal"
android:layout_width="380dp"
android:layout_height="260dp"
android:background="@mipmap/quitdialog_bg">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginBottom="25dp">
<ImageButton
android:id="@+id/btn_yes"
android:layout_width="110dp"
android:layout_height="50dp"
android:background="@mipmap/quitok"/>
<ImageButton
android:id="@+id/btn_no"
android:layout_width="110dp"
android:layout_height="50dp"
android:background="@mipmap/quitcancel"
android:layout_marginLeft="50dp"/>
</LinearLayout>
</LinearLayout>
②在values目录下,themes.xml中添加对话框的style(标题栏和背景),本例中将其设置为透明背景并去除标题栏(这里的去除背景不是去除我们设置的对话框样式的背景,而是修改系统中对于对话框的样式的默认设定)
<style name="diyDialog" parent="android:style/Theme.Dialog">
<item name="android:windowNoTitle">true</item>
<item name="android:windowBackground">@android:color/transparent</item>
</style>
③自定义一个继承了Dialog的类。重写构造函数,在构造函数中实现样式绑定和对话框按钮点击事件监听。
public class DiyDiaLog extends Dialog {
public DiyDiaLog(@NonNull Context context, int themeResId) {
super(context,themeResId);
setContentView(R.layout.diydialog);//样式绑定
findViewById(R.id.btn_yes).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
System.exit(0);
}
});
findViewById(R.id.btn_no).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dismiss();
}
});
}
}
④显示(此例是在定义的myClick方法中)
//根据自定义的DiyDiaLog类的构造函数,其参数为上下文和相应的主题样式名
DiyDiaLog myDialog=new DiyDiaLog(this,R.style.diyDialog);
myDialog.show();
三、PopupWindow
3.设置方法
首先,制作好PopupWindow的xml样式,动画样式及style,再创建PopupWindow对象实例,然后设置背景、注册事件监听器和添加动画、显示。
①PopupWindow样式(比较简陋)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/teal_200">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="复制"
android:background="@color/black"
android:textColor="@color/white"
android:layout_marginLeft="5dp"
android:padding="5dp"
></TextView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="粘贴"
android:background="@color/black"
android:textColor="@color/white"
android:layout_marginLeft="5dp"
android:padding="5dp"
></TextView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="全选"
android:background="@color/black"
android:textColor="@color/white"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:padding="5dp"
></TextView>
</LinearLayout>
</LinearLayout>
②编写动画资源文件并在style中引用,android动画效果可参考下面这篇博文。
Android动画效果translate、scale、alpha、rotate详解
需要在res目录下新建“Android Resource File”,Resource Type选择Animation。然后在新生成的动画资源目录下新建动画资源xml文件。
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="0"
android:toXDelta="0"
android:fromYDelta="1000"
android:toYDelta="0"
android:duration="2000"></translate>
<alpha android:fromAlpha="0.0"
android:toAlpha="1.0"
android:duration="2000"></alpha>
<rotate android:fromDegrees="-5"
android:toDegrees="0"
android:duration="2000"></rotate>
<scale android:fromXScale="0.0"
android:fromYScale="0.0"
android:toYScale="1.0"
android:toXScale="1.0"
android:duration="2000"
android:pivotX="100%"
android:pivotY="100%"
></scale>
</set>
在values目录下的themes.xml中新建style标签,在名为“android:windowEnterAnimation”的item中引用刚刚编写的动画资源xml文件。
<style name="animation_popupw">
<item name="android:windowEnterAnimation">@anim/anim_popupwindow</item>
</style>
③创建PopupWindow对象实例(接上文,直接在myclik中设置),设置背景、注册事件监听器和添加动画、显示。
View vw= LayoutInflater.from(this).inflate(R.layout.layout_popupwindow,null);
PopupWindow popw=new PopupWindow(vw,500,200,true);
popw.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
popw.setOutsideTouchable(true);
popw.setTouchable(true);
在制作菜单时,我们通过“getMenuInflater().inflate(R.menu.option,menu);”将菜单样式资源(布局文件)和定义的菜单对象进行绑定。而此处制作PopupWindow是通过“ LayoutInflater.from(this).inflate(R.layout.layout_popupwindow,null);”用一个布局文件得到一个以该布局文件为样式的view类对象,有这个view类对象后就可以生成一个设定大小的PopupWindow。
popw.setAnimationStyle(R.style.animation_popupw);
设置动画,R.style.animation_popupw是自定义的动画。
vw.findViewById(R.id.popw_copy).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this,"复制", LENGTH_LONG).show();
popw.dismiss();
}
});
popw.showAsDropDown(v,0,0);
设置PopupWindow的点击事件。注意此处是 vw.findViewById……”因为我们要找到的是PopupWindow各个选项对应的id,这是在PopupWindow的样式文件中设置的,所以要通过对应的view类对象去“find”.
四、使用了适配器的AlertDialog
1.设置方法
和普通的Dialog一样声明,只不过多了一步setAdapter。而既然是setAdapter,那必然要把适配器创建好,调用合适的构造函数。
例:带有图标的列表选项对话框。
要做的准备:写好每项item的样式xml文件,其中TextView要设置id(因为ArrayAdapter要将text匹配到样式中具体且合适的位置)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:padding="10dp">
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:src="@mipmap/ic_launcher"></ImageView>
<TextView
android:id="@+id/tv_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"
></TextView>
</LinearLayout>
编写代码。写好具体条目,创建适配器,创建AlertDialog.Builder对象,并setTitle,setAdapter,并编写条目的点击监听事件。
final String[]items={"orange","blue","pink","white","black"};
ArrayAdapter adapter=new ArrayAdapter(this,R.layout.style_arrayadpater,R.id.tv_text,items);
AlertDialog.Builder adaDialog=new AlertDialog.Builder(this)
.setTitle("请选择:")//可以这样连续.set是因为这两个函数的返回值都是builder对象
.setAdapter(adapter, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(MainActivity.this,items[i],LENGTH_LONG).show();
dialogInterface.dismiss();
}
});
adaDialog.show();
参考:
Android Developers - AlertDialog
Developers - 对话框
Android动画效果translate、scale、alpha、rotate详解
慕课网