我有以下代码创建一个包含 EditText 的 PopupWindow:
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 运行两次,这是我无法拥有的。我在这里想念什么?
编辑:我稍微更新了我的代码。如果我没有将焦点放在弹出窗口上,那么一切都会完美,除了我无法专注于我的编辑文本。如果我在弹出窗口上执行 setFocusable,则 edittext 不会立即获得焦点,我必须双击以将其删除。