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 需要一个指定视图做参考坐标

它的构造函数有好多个,我用到的是

publicPopupWindow(ViewcontentView, 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


显示函数用:其中parent 及是 参考坐标视图

public voidshowAtLocation(Viewparent, int gravity, int x, int y)

parent a parent view to get thegetWindowToken()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),底部

3、使用注意 popupWindow 默认是不响应内部的响应事件的,所以用了前面说的构造函数,最后一个参数设置为true 这样子还不够必须,添加以下代码:
// 这两句加了,才能点击并且响应
mPopupWindow.setBackgroundDrawable(new BitmapDrawable());// 响应返回键必须的语句。
mPopupWindow.setOutsideTouchable(true);// 点击外部按钮 取消。按返回按键也取消

在popupwindow的xml界面 外部布局里可以添加android:focusableInTouchMode="true" (不是必须的,如果已经有添加上面的几步的话)
4、如果popupwindow里有edit的话可以添加以下代码,增强用户体验,及点击空白区域,缩下键盘 更好的方式: 首先让edit失去焦点 让其父类控件获取焦点(就不会一进入就获取键盘) android:focusable="true"
android:focusableInTouchMode="true"

其次重写 @Override
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);

}


普通方式:


popView.setOnClickListener(new OnClickListener() {

@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);
}
});




5、精确设置popwindow 大小 /*
* 带测量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);// 指定视图偏移
}

ex:

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();

}

});

}



更多相关文章

  1. Android(安卓)编译重要参数 LOCAL_MODULE_TAGS
  2. Android(安卓)5.0新控件常用属性收集
  3. 安卓:View的基本信息
  4. [Android算法] bitmap 将图片压缩到指定的大小
  5. android:shape的使用(+圆角ListView)
  6. Android自定义Dialog二次调用报错解决方法:The specified child a
  7. View的测量
  8. Android(安卓)Material Design之CoordinatorLayout全面使用介绍
  9. android 文件操作方法集合类分享

随机推荐

  1. Javascript sort()不适用于Chrome中超过10
  2. 五十行javascript代码实现简单的双向数据
  3. 缩放图像后的鼠标位置(比率/比率)
  4. 正则表达式以任何顺序匹配多个模式
  5. 使用javascript或jquery将具有相同id的多
  6. 12个非常有用的JavaScript技巧
  7. 浏览器独立文件io在javascript中
  8. 根据php对ajax请求的响应制作动态html表
  9. Javascript XML DOM将属性设置为特定元素
  10. arcgis api for js入门开发系列十 自定义