我有以下代码,它创建一个包含EditText的PopupWindow:
PopupWindows具有setBackGroundDrawable功能,因此当用户点击窗口外部时,应自动关闭该框。我知道该框已经关闭,因为当我在框外单击时,我的OnDismiss方法运行,但实际窗口直到我在框外部两次点击后才消失,因此onDismiss运行了两次,这是不可接受的。我在这里错过了什么?
编辑:我稍微更新了一下代码。如果我不在popupwindow上设置焦点,则除了无法使edittext获得焦点之外,一切都完美无缺。如果我在popup上设置setFocusable,则edittext不会立即获得焦点,我必须双击才能移除它。
lbs.setOnTouchListener(new OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent arg1) {
int pWidth = 100;
int pHeight = 80;
int vHeight = mView.getHeight();
int[] location = new int[2];
v.getLocationOnScreen(location);
final View view = inflater.inflate(R.layout.list_popup, null, false);
final PopupWindow pw = new PopupWindow(view, pWidth, pHeight, false);
pw.setTouchable(true);
//pw.setFocusable(true);
pw.setOutsideTouchable(true);
pw.setBackgroundDrawable(new BitmapDrawable());
pw.setContentView(view);
pw.showAtLocation(v, Gravity.NO_GRAVITY, location[0]-(pWidth/4), location[1]+vHeight);
//final LinearLayout layout = (LinearLayout)view.findViewById(R.id.PopupLayout);
final EditText input = (EditText)view.findViewById(R.id.Input);
input.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
Log.i("Focus", "Focus Changed");
/*
if (hasFocus) {
InputMethodManager inputMgr = (InputMethodManager)myContext.getSystemService(Context.INPUT_METHOD_SERVICE);
inputMgr.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
inputMgr.showSoftInput(v, InputMethodManager.SHOW_IMPLICIT);
}
*/
}
});
input.setText(lbs.getText().toString());
input.requestFocus();
pw.setOnDismissListener(new OnDismissListener(){
@Override
public void onDismiss() {
parentActivity.changeWeight(getId, Double.parseDouble(input.getText().toString()));
Log.i("View Visibility", "" + view.getVisibility());
}
});
pw.setTouchInterceptor(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
Log.i("Background", "Back Touched");
pw.dismiss();
return true;
}
return false;
}
});
return true;
}
});
PopupWindows具有setBackGroundDrawable功能,因此当用户点击窗口外部时,应自动关闭该框。我知道该框已经关闭,因为当我在框外单击时,我的OnDismiss方法运行,但实际窗口直到我在框外部两次点击后才消失,因此onDismiss运行了两次,这是不可接受的。我在这里错过了什么?
编辑:我稍微更新了一下代码。如果我不在popupwindow上设置焦点,则除了无法使edittext获得焦点之外,一切都完美无缺。如果我在popup上设置setFocusable,则edittext不会立即获得焦点,我必须双击才能移除它。
pw.setFocusable(true);
- Mohammad Ersan