Android在开发中经常会遇到有弹框的需求。经常使用的有Dialog 弹框,Window弹框,Activity伪弹框这三种。
感谢http://sigechuizi.cn/article/107
Activity伪弹框:https://blog.csdn.net/qq_35698774/article/details/117438989
今天主要是讲解Dialog的使用:
Dialog继承Object,异步调用,不会阻塞UI线程。以下是对他的整体框架:
最常用的还是AlertDialog.今天主要是讲他的用法。AlertDialog的用法也很简单
setTitle :为对话框设置标题
setIcon :为对话框设置图标
setMessage:为对话框设置内容
setView : 给对话框设置自定义样式
setItems :设置对话框要显示的一个list,一般用于显示几个命令时
setMultiChoiceItems :用来设置对话框显示一系列的复选框
setSingleChoiceItems :用来设置对话框显示一系列的单选框
setNeutralButton :普通按钮
setPositiveButton :给对话框添加"Yes"按钮
setNegativeButton :对话框添加"No"按钮
create : 创建对话框
show :显示对话框
1.只显示标题和内容
AlertDialog alertDialog1 = new AlertDialog.Builder(this)
.setTitle("这是标题")//标题
.setMessage("这是内容")//内容
.setIcon(R.mipmap.ic_launcher)//图标
.create();
alertDialog1.show();
2.有多个按钮
setPositiveButton 设置一个确定按钮
setNegativeButton 设置一个取消按钮
setNeutralButton 设置一个普通按钮
这三个按钮可以随意组合使用不冲突。
AlertDialog alertDialog2 = new AlertDialog.Builder(this)
.setTitle("这是标题")
.setMessage("有多个按钮")
.setIcon(R.mipmap.ic_launcher)
.setPositiveButton("确定", new DialogInterface.OnClickListener() {//添加"Yes"按钮
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(AlertDialogActivity.this, "这是确定按钮", Toast.LENGTH_SHORT).show();
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {//添加取消
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(AlertDialogActivity.this, "这是取消按钮", Toast.LENGTH_SHORT).show();
}
})
.setNeutralButton("普通按钮", new DialogInterface.OnClickListener() {//添加普通按钮
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(AlertDialogActivity.this, "这是普通按钮按钮", Toast.LENGTH_SHORT).show();
}
})
.create();
alertDialog2.show();
3.一个列表
setItems 里面写列表数据
final String[] items3 = new String[]{"苍老湿", "小泽老湿", "波多野结衣老湿", "吉泽明步老湿"};//创建item
AlertDialog alertDialog3 = new AlertDialog.Builder(this)
.setTitle("选择您喜欢的老湿")
.setIcon(R.mipmap.ic_launcher)
.setItems(items3, new DialogInterface.OnClickListener() {//添加列表
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(AlertDialogActivity.this, "点的是:" + items3[i], Toast.LENGTH_SHORT).show();
}
})
.create();
alertDialog3.show();
4.单选列表
setSingleChoiceItems 单选框列表
final String[] items4 = new String[]{"苍老湿", "小泽老湿", "波多野结衣老湿", "吉泽明步老湿"};//创建item
AlertDialog alertDialog4 = new AlertDialog.Builder(this)
.setTitle("选择您喜欢的老湿")
.setIcon(R.mipmap.ic_launcher)
.setSingleChoiceItems(items4, 0, new DialogInterface.OnClickListener() {//添加单选框
@Override
public void onClick(DialogInterface dialogInterface, int i) {
index = i;
}
})
.setPositiveButton("确定", new DialogInterface.OnClickListener() {//添加"Yes"按钮
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(AlertDialogActivity.this, "这是确定按钮" + "点的是:" + items4[index], Toast.LENGTH_SHORT).show();
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {//添加取消
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(AlertDialogActivity.this, "这是取消按钮", Toast.LENGTH_SHORT).show();
}
})
.create();
alertDialog4.show();
5.多选列表
setMultiChoiceItems 多选框
final String[] items5 = new String[]{"苍老湿", "小泽老湿", "波多野结衣老湿", "吉泽明步老湿"};//创建item
final boolean[] booleans = {true, true, false, false};
AlertDialog alertDialog5 = new AlertDialog.Builder(this)
.setTitle("选择您喜欢的老湿")
.setIcon(R.mipmap.ic_launcher)
.setMultiChoiceItems(items5, booleans, new DialogInterface.OnMultiChoiceClickListener() {//创建多选框
@Override
public void onClick(DialogInterface dialogInterface, int i, boolean b) {
booleans[i] = b;
}
})
.setPositiveButton("确定", new DialogInterface.OnClickListener() {//添加"Yes"按钮
@Override
public void onClick(DialogInterface dialogInterface, int i) {
StringBuilder stringBuilder = new StringBuilder();
for (int a = 0; a < booleans.length; a++) {
if (booleans[a]) {
stringBuilder.append(items5[a] + " ");
}
}
Toast.makeText(AlertDialogActivity.this, "这是确定按钮" + "点的是:" + stringBuilder.toString(), Toast.LENGTH_SHORT).show();
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {//添加取消
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(AlertDialogActivity.this, "这是取消按钮", Toast.LENGTH_SHORT).show();
}
})
.create();
alertDialog5.show();
6.setAdapter的用法
final String[] items6 = new String[]{"苍老湿", "小泽老湿", "波多野结衣老湿", "吉泽明步老湿"};//创建item
AlertDialog alertDialog6 = new AlertDialog.Builder(this)
.setTitle("选择您喜欢的老湿")
.setIcon(R.mipmap.ic_launcher)
.setAdapter(new ArrayAdapter<String>(AlertDialogActivity.this, R.layout.item, R.id.tv, items6), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(AlertDialogActivity.this, "点的是:" + items6[i], Toast.LENGTH_SHORT).show();
}
})
.create();
alertDialog6.show();
7.自定义view的用法
final AlertDialog.Builder alertDialog7 = new AlertDialog.Builder(this);
View view1 = View.inflate(this, R.layout.activity_alter_dialog_setview, null);
final EditText et = view1.findViewById(R.id.et);
Button bu = view1.findViewById(R.id.button);
alertDialog7
.setTitle("Login")
.setIcon(R.mipmap.ic_launcher)
.setView(view1)
.create();
final AlertDialog show = alertDialog7.show();
bu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(AlertDialogActivity.this, "电话" + et.getText().toString(), Toast.LENGTH_SHORT).show();
show.dismiss();
}
});
8.自定义view的用法透明背景
public void showCaptainCarTimeAlertDialog(final Context context, String intro, int time) {
AlertDialog.Builder builder = new AlertDialog
.Builder(context, R.style.dialogNoBg);
View contentView = View.inflate(context, R.layout.team_pop, null);
builder.setView(contentView);
builder.setCancelable(false);
final AlertDialog dialogTime = builder.create();
dialogTime.show();
Button bottomConfirm = contentView.findViewById(R.id.bottom_confirm);
Button bottomCancel = contentView.findViewById(R.id.bottom_cancel);
TextView tvTitie = contentView.findViewById(R.id.tv_titie);
TextView tvMess = contentView.findViewById(R.id.tv_mess);
LinearLayout ll = contentView.findViewById(R.id.ll);
final TextView tvTime = contentView.findViewById(R.id.tv_time);
// tvTitie.setText(title);
tvMess.setText(intro);
final LinearLayout clPop = contentView.findViewById(R.id.cl_pop);
LinearLayout.LayoutParams linearParams_tvMess = (LinearLayout.LayoutParams) tvMess.getLayoutParams(); //
linearParams_tvMess.topMargin = dp2px(context, 40);
tvMess.setLayoutParams(linearParams_tvMess);//使设置好的布局参数应用到控件
LinearLayout.LayoutParams linearParams_tvTime = (LinearLayout.LayoutParams) tvTime.getLayoutParams(); //
linearParams_tvTime.topMargin = dp2px(context, 18);
tvTime.setLayoutParams(linearParams_tvTime);//使设置好的布局参数应用到控件
LinearLayout.LayoutParams linearParams_ll = (LinearLayout.LayoutParams) ll.getLayoutParams(); //
linearParams_ll.topMargin = dp2px(context, 12);
tvTime.setLayoutParams(linearParams_ll);//使设置好的布局参数应用到控件
FrameLayout.LayoutParams linearParams_clPop = (FrameLayout.LayoutParams) clPop.getLayoutParams(); //
linearParams_clPop.height = dp2px(context, 400);
linearParams_clPop.width = dp2px(context, 574);
clPop.setLayoutParams(linearParams_clPop);//使设置好的布局参数应用到控件
//倒计时
final CountDownTimer mCountDownTimer = new CountDownTimer(time, 1000) {
@Override
public void onTick(long millisUntilFinished) {
tvTime.setText(millisUntilFinished / 1000 + "s");
}
@Override
public void onFinish() {
if (dialogTime != null) {
tvTime.setText("0s");
dialogTime.dismiss();
}
}
};
mCountDownTimer.start();
bottomCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialogTime.dismiss();
}
});
bottomConfirm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//确定
dialogTime.dismiss();
}
});
dialogTime.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
if (mCountDownTimer != null) {
mCountDownTimer.cancel();
}
}
});
}
public static int dp2px(Context context, float dpVal) {
float scale = context.getResources().getDisplayMetrics().density;
return (int) (dpVal * scale + 0.5f);
// return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
// dpVal, context.getResources().getDisplayMetrics());
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/cl_pop"
android:layout_width="574dp"
android:layout_height="400dp"
android:orientation="vertical"
android:gravity="center"
android:background="@drawable/bg_team_popup">
<TextView
android:id="@+id/tv_titie"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="标题"
android:textColor="#ff009ffa"
android:textSize="36sp" />
<TextView
android:id="@+id/tv_mess"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tv_titie"
android:layout_marginTop="40dp"
android:text="是是是"
android:textColor="#ffffff"
android:textSize="28sp" />
<TextView
android:id="@+id/tv_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tv_mess"
android:layout_marginTop="18dp"
android:text="5"
android:textColor="#ffffff"
android:textSize="60sp" />
<LinearLayout
android:id="@+id/ll"
android:layout_marginTop="12dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/bottom_confirm"
android:layout_width="162dp"
android:layout_height="60dp"
android:background="#009ffa"
android:text="确定"
android:textColor="#ffffff"
android:textSize="28sp" />
<Button
android:id="@+id/bottom_cancel"
android:layout_width="162dp"
android:layout_height="60dp"
android:background="@drawable/shape_team_pop_cancel"
android:text="取消"
android:layout_marginLeft="60pt"
android:textColor="#ffffff"
android:textSize="28sp" />
</LinearLayout>
</LinearLayout>
<style name="dialogNoBg" >
<!--<item name="android:background">#00000000</item>-->
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">true</item>
<!--<item name="windowFrame">@null</item>-->
<!--<item name="android:windowIsTranslucent">true</item>-->
<!--<item name="backgroundDimEnabled">true</item>-->
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowFrame">@null</item>
<item name="android:backgroundDimEnabled">true</item>
<item name="android:windowIsTranslucent">true</item>
</style>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke android:width="2pt" android:color="#009ffa" />
</shape>
Window弹框和activity弹框将在另外的文章中说明。
Android互助群:
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/37790.html