指定VIEW的onClick事件:
private PopupWindow popup_message;// 留言对话框
/**
* 全局Click事件
*/
@Override
public void onClick(View v) {
if (v == btnMessage) {// 留言
getPopupWindowByMessage();
popup_message.showAtLocation(findViewById(R.id.layout_main), Gravity.CENTER | Gravity.CENTER, 0, 0);
}
}
/**
* 获取PopupWindow实例
*
* @author SHANHY
*/
private void getPopupWindowByMessage() {
if (null != popup_message) {
popup_message.dismiss();
return;
} else {
initPopupWindowByMessage();
}
}
/**
* 创建PopupWindow(留言)
*
* @author SHANHY
*/
protected void initPopupWindowByMessage() {
View popupWindow_view = getLayoutInflater().inflate(R.layout.popupwindow_message, null, false); // 获取自定义布局文件popup.xml的视图
popup_message = new PopupWindow(popupWindow_view, LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, true);// 创建PopupWindow实例
Button btnSure = (Button) popupWindow_view.findViewById(R.id.btn_sure);// dialog.xml视图里面的控件
Button btnCancel = (Button) popupWindow_view.findViewById(R.id.btn_cancel);// dialog.xml视图里面的控件
// popup.setBackgroundDrawable(getResources().getDrawable(R.drawable.panel_bg));
popup_message.setBackgroundDrawable(new BitmapDrawable()); // 响应返回键必须的语句。请放心,设置 BackgroundDrawable 并不会改变你在配置文件中设置的背景颜色或图像。
popup_message.setFocusable(true);// menu菜单获得焦点 如果没有获得焦点menu菜单中的控件事件无法响应
popup_message.update();
// 返回键关闭
popupWindow_view.findViewById(R.id.layout_main).setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_BACK)
if (popup_message != null && popup_message.isShowing())
popup_message.dismiss();
return false;
}
});
btnSure.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {// 留言
}
});
btnCancel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (popup_message != null && popup_message.isShowing())
popup_message.dismiss();
}
});
}