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 メソッドが実行されるため、ボックスが閉じていることはわかっていますが、ボックスの外側を 2 回タッチするまで実際のウィンドウは消えません。ここで何が欠けていますか?
編集: コードを少し更新しました。ポップアップウィンドウにフォーカスを設定しないと、編集テキストにフォーカスできないという事実を除いて、すべてが完璧に機能します。ポップアップで setFocusable を実行すると、編集テキストはすぐにフォーカスされず、ダブルクリックして削除する必要があります。