1、android 对话框常见的两种:dialog,popupWindow。其中dialog是android提供的,包含了很多,现成的定制,同时,功能也受到了限制。普通窗口建议用dialog。
dialog满足不了的用popupWindow,popupWindow 是很自由的自定义显示界面。官方文档的描述是:A popup window that can be used to display an arbitrary view(任意视图). The popup window is a floating container that appears on top of the current activity(显示在当前activity 前).
2、popupWindow使用 参见官网:http://developer.android.com/reference/android/widget/PopupWindow.html
AlertDialog 使用和普通控件类似只要定义在指定activity 的Context,需要的时候show出来就行了。
popopWindow 使用上有区别:1、定义popupwindow关联自己的视图,2、显示popupWindow 需要一个指定视图做参考坐标
它的构造函数有好多个,我用到的是
public PopupWindow (View contentView, int width, int height, boolean focusable)
contentView | the popup's content |
---|---|
width | the popup's width |
height | the popup's height |
focusable | true if the popup can be focused, false otherwise |
public void showAtLocation (View parent, int gravity, int x, int y)
parent | a parent view to get the getWindowToken() token from |
---|---|
gravity | the gravity which controls the placement of the popup window |
x | the popup's x location offset |
y | the popup's y location offset |
gracity 可以选择头部,中部(Gravity.CENTER),底部
mPopupWindow.setBackgroundDrawable(new BitmapDrawable());// 响应返回键必须的语句。
mPopupWindow.setOutsideTouchable(true);// 点击外部按钮 取消。按返回按键也取消
android:focusableInTouchMode="true"
public boolean onTouchEvent(MotionEvent event) {
InputMethodManager imm = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
return super.onTouchEvent(event);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
InputMethodManager imm = (InputMethodManager) mContext
.getSystemService(Context.INPUT_METHOD_SERVICE);
// login_auto_account.setCursorVisible(false);
imm.hideSoftInputFromWindow(edit_mony.getWindowToken(), 0);
}
});
* 带测量popwindow高度的方法
*/
@Deprecated
private void hintPopWindow() {
final View fatherView = getLayoutInflater().inflate(
R.layout.activity_set, null);
//外圈布局是指定pop大小的时候会指定出这个大小。所以无法代表原来窗口的大小
final View popView = getLayoutInflater().inflate(
R.layout.popwindow_set, null);
//内圈布局-真实窗口大小。到时候测出这个大小 回调函数设置具体的大小
final View subView = popView.findViewById(R.id.layout_set);
//设置回调函数 在新生成的窗口的瞬间回调该函数 重新写窗口高度 宽度
ViewTreeObserver vto = popView.getViewTreeObserver();
vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
public boolean onPreDraw() {
int h = subView.getMeasuredHeight();
int w = subView.getMeasuredWidth();
mPopupWindow.dismiss();
mPopupWindow.setHeight(h);
mPopupWindow.showAtLocation(fatherView, Gravity.BOTTOM, 0, 0);// 指定视图偏移
return true;
}
});
int width = (int) (getWindowManager().getDefaultDisplay().getWidth());
int height = (int) (getWindowManager().getDefaultDisplay().getHeight());
Log.d("11", height + " -----height");
// mDatePicker.set
mPopupWindow = new PopupWindow(popView, width, height, true);// 自己视图
// 这两句加了,才能点击并且响应
mPopupWindow.setBackgroundDrawable(new BitmapDrawable());// 响应返回键必须的语句。
mPopupWindow.setOutsideTouchable(true);// 点击外部按钮 取消。按返回按键也取消
// 显示视图与指定视图之间的偏差
// mPopupWindow.showAsDropDown(mListView);//指定视图下方
mPopupWindow.showAtLocation(fatherView, Gravity.BOTTOM, 0, 0);// 指定视图偏移
}
private void updatePopwindows(Activity context, View view) {
DisplayMetrics dm = new DisplayMetrics();
context.getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels;
int height = dm.heightPixels;
View popView = LayoutInflater.from(context).inflate(R.layout.popupwindow_task, null);
TextView logTextView = (TextView) popView.findViewById(R.id.txt_task_info);
// mDatePicker.set
final PopupWindow mPopupWindow = new PopupWindow(popView, (int) (width), (int) (height), true);// 自己视图
// 这两句加了,才能点击并且响应
mPopupWindow.setBackgroundDrawable(new BitmapDrawable());// 响应返回键必须的语句。
mPopupWindow.setOutsideTouchable(true);// 点击外部按钮 取消。按返回按键也取消
// 显示视图与指定视图之间的偏差
// mPopupWindow.showAsDropDown(mListView);//指定视图下方
mPopupWindow.showAtLocation(view, Gravity.CENTER, 0, 0);// 指定视图偏移
popView.findViewById(R.id.imageView_close).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
mPopupWindow.dismiss();
}
});
popView.findViewById(R.id.imageView_close).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
mPopupWindow.dismiss();
}
});
}