final String[] arrayFruit = new String[] { “苹果”, “橘子”, “草莓”, “香蕉” };
final boolean[] arrayFruitSelected = new boolean[] {true, true, false, false};
Dialog alertDialog = new AlertDialog.Builder(this).
setTitle(“你喜欢吃哪种水果?”).
setIcon(R.drawable.ic_launcher)
.setMultiChoiceItems(arrayFruit, arrayFruitSelected, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
arrayFruitSelected[which] = isChecked;
}
}).
setPositiveButton(“确认”, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < arrayFruitSelected.length; i++) {
if (arrayFruitSelected[i] == true)
{
stringBuilder.append(arrayFruit[i] + “、”);
}
}
Toast.makeText(Dialog_AlertDialogDemoActivity.this, stringBuilder.toString(), Toast.LENGTH_SHORT).show();
}
}).
setNegativeButton(“取消”, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).
create();
alertDialog.show();
}
}
实际中,我们也经常会自定义View,来满足我们的开发需求
比如说我们要实现一个Login画面,有用户名和密码,这时我们就要用到自定义View的AlertDialog
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”
android:layout_width=“fill_parent”
android:layout_height=“fill_parent”
android:orientation=“vertical” >
<LinearLayout
android:layout_width=“fill_parent”
android:layout_height=“wrap_content”
android:gravity=“center” >
<TextView
android:layout_width=“0dip”
android:layout_height=“wrap_content”
android:layout_weight=“1”
android:text=“@string/user” />
<EditText
android:layout_width=“0dip”
android:layout_height=“wrap_content”
android:layout_weight=“1” />
<LinearLayout
android:layout_width=“fill_parent”
android:layout_height=“wrap_content”
android:gravity=“center” >
<TextView
android:layout_width=“0dip”
android:layout_height=“wrap_content”
android:layout_weight=“1”
android:text=“@string/passward” />
<EditText
android:layout_width=“0dip”
android:layout_height=“wrap_content”
android:layout_weight=“1” />
然后在Activity中去加载我们的布局
// 取得自定义View
LayoutInflater layoutInflater = LayoutInflater.from(this);
View myLoginView = layoutInflater.inflate(R.layout.login, null);
Dialog alertDialog = new AlertDialog.Builder(this).
setTitle(“用户登录”).
setIcon(R.drawable.ic_launcher).
setView(myLoginView).
setPositiveButton(“登录”, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).
setNegativeButton(“取消”, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).
create();
alertDialog.show();
}
2.PopupWindow的属性和方法及使用
它可以使用任意布局的View作为其内容
,这个弹出框是悬浮在当前activity之上的。
PopupWindow的位置按照有无偏移分,可以分为偏移和无偏移两种;按照参照物的不同,可以分为相对于某个控件(Anchor锚)和相对于父控件。具体如下
showAsDropDown(View anchor):相对某个控件的位置(正左下方),无偏移
showAsDropDown(View anchor, int xoff, int yoff):相对某个控件的位置,有偏移
showAtLocation(View parent, int gravity, int x, int y):相对于父控件的位置(例如正中央Gravity.CENTER,下方Gravity.BOTTOM等),可以设置偏移或无偏移
View popView = LayoutInflater.from(this).inflate(
R.layout.pop_style, null);
pop = new PopupWindow(popView, 400, 350);
pop.setBackgroundDrawable(new BitmapDrawable());//这些要在show之前设置,否则无法作用
pop.setOutsideTouchable(true);
Button popButton = (Button) popView.findViewById(R.id.button1);
popButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO 自动生成的方法存根
Log.e(“pop----->”, “bt”);
}
});
pop.showAtLocation(bt2, Gravity.CENTER, 0, 0);
其中的bt2是点击弹出框相对于它的显示位置
int[] location = new int[2];
view.getLocationOnScreen(location);
pop.showAtLocation(view, Gravity.NO_GRAVITY, (ScreenUtils.getScreenWidth() - pop.getWidth()) / 2, location[1] - pop.getHeight() + 10);
修改popwindown显示的位置
注意:有时候比如点击button后天弹出对话框,当点击外部时,弹框消失。这时候可能会遇到一个问题:假如点击的外部区域是button,这个时候就会出现button先消失,后又出现的问题。大多数情况下,我们这时候还是希望点击button时弹框消失就好了,再次点击button时就出现。为此,可以这样做:
1)在你点击弹出popowinddow的view上添加点击事件view.setOnClick(false)
2)在popwind初始化后设置
popupwindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
handler.postDelayed(new Runnable() {
@Override
public void run() {
view.setClickable(true);
}
}, 100);
}
});
3.AlertDialog和PopupWindow的区别
总结:
各行各样都会淘汰一些能力差的,不仅仅是IT这个行业,所以,不要被程序猿是吃青春饭等等这类话题所吓倒,也不要觉得,找到一份工作,就享受安逸的生活,你在安逸的同时,别人正在奋力的向前跑,这样与别人的差距也就会越来越遥远,加油,希望,我们每一个人,成为更好的自己。
-
BAT大厂面试题、独家面试工具包,
-
资料包括 数据结构、Kotlin、计算机网络、Framework源码、数据结构与算法、小程序、NDK、Flutter,
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
-
BAT大厂面试题、独家面试工具包,
-
资料包括 数据结构、Kotlin、计算机网络、Framework源码、数据结构与算法、小程序、NDK、Flutter,
[外链图片转存中…(img-p7FWcLmx-1714269410717)]
[外链图片转存中…(img-RLDUp575-1714269410718)]
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!