更新:
虚拟按键不覆盖:
mPopupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
全屏幕设置(有了虚拟按键全屏设置不要再使用直接设置屏幕高度的办法)用
mPopupWindow = new PopupWindow(popView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);//全屏幕设置
或者
setContentView(contentView); setWidth(ViewGroup.LayoutParams.MATCH_PARENT); setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
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);
}
});
private void updatePopwindows(final HashMap
map) {
edit_business.setText(String.valueOf(map.get(BUSINESS)));
radioGroup.clearCheck();
if (map.get(TAB_NUM).equals(TAB_INCOME)) {
edit_mony.setText(String.valueOf(map.get(INCOME)));
radio_income.setChecked(true);
} else {
edit_mony.setText(String.valueOf(map.get(OUTLAY)));
radio_outlay.setChecked(true);
}
String datesString = (String) map.get(DATE);
String[] dates = datesString.split("-");
mDatePicker.init(Integer.valueOf(dates[0]),
Integer.valueOf(dates[1]) - 1, Integer.valueOf(dates[2]), null);
int width = (int) (getWindowManager().getDefaultDisplay().getWidth());
int height = (int) (getWindowManager().getDefaultDisplay().getHeight());
// mDatePicker.set
final PopupWindow mPopupWindow = new PopupWindow(popView, width,
height, true);// 自己视图
// 这两句加了,才能点击并且响应
mPopupWindow.setBackgroundDrawable(new BitmapDrawable());// 响应返回键必须的语句。
mPopupWindow.setOutsideTouchable(true);// 点击外部按钮 取消。按返回按键也取消
// 判断2.3版本
// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB){
// mDatePicker.setCalendarViewShown(false);
// }
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
// 新的 api
mDatePicker.setCalendarViewShown(false);
} else {
// 2.3 api
}
btn_cancel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
mPopupWindow.dismiss();
}
});
btn_ok.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String dateString = mDatePicker.getYear() + "-"
+ mDatePicker.getMonth() + "-"
+ mDatePicker.getDayOfMonth();
HashMap
mHashMap = new HashMap
();
// api 插入
ContentValues cValues = new ContentValues();
mDatabase = mdatebaseUtils.getWritableDatabase();
switch (radioGroup.getCheckedRadioButtonId()) {
case R.id.radio_income:
cValues.put(DATE, dateString);
cValues.put(BUSINESS, edit_business.getText().toString());
cValues.put(INCOME, edit_mony.getText().toString());
// mDatabase.insert(TAB_INCOME, null, cValues);
mDatabase.update(TAB_INCOME, cValues, "num=?",
new String[] { String.valueOf(map.get(NUM)) });
break;
case R.id.radio_outlay:
cValues.put(DATE, dateString);
cValues.put(BUSINESS, edit_business.getText().toString());
cValues.put(OUTLAY, edit_mony.getText().toString());
// mDatabase.insert(TAB_OUTLAY, null, cValues);
mDatabase.update(TAB_OUTLAY, cValues, "num=?",
new String[] { String.valueOf(map.get(NUM)) });
break;
default:
break;
}
mDatabase.close();
// mAdapter.notifyDataSetChanged();
query();
mPopupWindow.dismiss();
}
});
// 显示视图与指定视图之间的偏差
// mPopupWindow.showAsDropDown(mListView);//指定视图下方
mPopupWindow.showAtLocation(mListView, Gravity.TOP, 0, 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) {
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 = new PopupWindow(popView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);//全屏幕设置
mPopupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);//虚拟按键不覆盖的处理
// 这两句加了,才能点击并且响应
mPopupWindow.setBackgroundDrawable(newBitmapDrawable());// 响应返回键必须的语句。
mPopupWindow.setOutsideTouchable(true);// 点击外部按钮 取消。按返回按键也取消
// 显示视图与指定视图之间的偏差
// mPopupWindow.showAsDropDown(mListView);//指定视图下方
mPopupWindow.showAtLocation(view, Gravity.CENTER, 0, 0);// 指定视图偏移
popView.findViewById(R.id.imageView_close).setOnClickListener(new OnClickListener() {
@Override
publicvoid onClick(View v) {
//TODO Auto-generated method stub
mPopupWindow.dismiss();
}
});
popView.findViewById(R.id.imageView_close).setOnClickListener(new OnClickListener() {
@Override
publicvoid onClick(View v) {
//TODO Auto-generated method stub
mPopupWindow.dismiss();
}
});
}