现在的位置: 首页 > 综合 > 正文

Android学习札记4:PopupWindow在设置setFocusable后监听返回键盘事件

2017年12月17日 ⁄ 综合 ⁄ 共 595字 ⁄ 字号 评论关闭

PopupWindow在设置了焦点之后,那么这个弹出窗口就是当前操作的窗口,如果想要在此时响应按键事件,就需要在PopupWindow上注册键盘事件来响应。但是PopupWindow好像只有一个OnDismissListener,怎么来注册键盘事件监听呢?

可以利用PopupWindow里的最外层View,对该子View监听键盘事件setOnKeyListener即可。

popupwindow.setBackgroundDrawable(new BitmapDrawable());
popupwindow.setFocusable(true);
popupwindow.setFocusableInTouchMode(true);

childrenView.setOnKeyListener(new OnKeyListener()
{
	@Override
	public boolean onKey(View v, int keyCode, KeyEvent event)
	{
		// TODO Auto-generated method stub
		if (keyCode == KeyEvent.KEYCODE_BACK)
		{
			Log.v("keyCode", "/" + keyCode);
 			if(popupwindow != null) {
				popupwindow.dismiss();
				popupwindow = null;
			} 
		}
		 return false; 
	}
});


抱歉!评论已关闭.