Android PopupWindow Does Not Dismiss
I've got the following code that creates a PopupWindow containing an 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.ACTIO开发者_运维百科N_OUTSIDE) {
Log.i("Background", "Back Touched");
pw.dismiss();
return true;
}
return false;
}
});
return true;
}
});
The PopupWindows has a setBackGroundDrawable so that when the user touches outside the window the box should automatically dismiss. I know the box is dismissing because my OnDismiss method runs when I click outside the box, but the actual window does not disappear until I touch twice outside the box and because of that the onDismiss runs twice which I can't have. What am I missing here?
EDIT: I updated my code slightly. If I DO NOT set focus on the popupwindow everything works perfect except for the fact that I cannot get focus on my edittext. If I do setFocusable on the popup then the edittext does not get focus right away and I have to double click to remove it.
I think you might be opening more than one popup window. The onTouch method will be called at least for touch down and touch up, and probably a couple touch moves in there too. Try checking for (arg1.getAction() == MotionEvent.ACTION_UP) and showing the window only then.
Your popupwindow does not get a context of parent activity. Try to make like this instead of "final PopupWindow pw = new PopupWindow(view, pWidth, pHeight, false);"
final PopupWindow pw = new PopupWindow(getApplicationContext());
pw.setContentView(view);
pw.setHeight(pHeight);
pw.setWidth(pWidth);
pw.setFocusable(false);
精彩评论