0

语境

我正在使用 popupwindows 以允许用户快速重命名活动中的卡片视图。

我通过使用 aViewSwitcherTextView(原始名称)交换为EditText(新名称)来做到这一点。

问题

当用户按下EditText和确认时,由于某种原因,您无法返回应用程序。IE。当你点击它时,它不会响应。PopUpWindow"RECENT APPS"

诊断

我认为这是Window Focus的问题。我已经尝试过EditText.clearFocus()ET 并解雇了所有 PopUps onPause,但没有运气。

有没有办法使用 onFocusChangeListener 来解决这个问题?

代码(我试图删除尽可能多的多余项目)

TheHubActivity.java

public class TheHubActivity extends AppCompatActivity implements RecyclerViewAdapter.onCardClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        // KEYBOARD
        imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);

        recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
        //... Set up recycle view

        rvContent = new ArrayList<>();
    }

    @Override
    public void onCardLongClick(Flow longClickedFlow, int cardPosition, View cardViewClicked) {
        showLongClickPopUpMenu(longClickedFlow,cardPosition, cardViewClicked);
    }

    private void showLongClickPopUpMenu(final Flow longClickedFlow, final int cardPosition, final View cardViewClicked) {

        LayoutInflater layoutInflater = (LayoutInflater) this
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View layout = layoutInflater.inflate(R.layout.popup_window_longclick, null);

        LinearLayout viewGroup = (LinearLayout)  layout.findViewById(R.id.popup_longclick);

        // Creating the PopupWindow
        final PopupWindow popup = new PopupWindow(layout, RecyclerView.LayoutParams.WRAP_CONTENT,
                RecyclerView.LayoutParams.WRAP_CONTENT);


        popup.setFocusable(true);

        // Getting a reference to Close button, and close the popup when clicked.
        ImageView delete = (ImageView) layout.findViewById(R.id.popup_delete_item);

        delete.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                /*.... Delete current Flow from internal file and UI */
                popup.dismiss();
            }
        });

        ImageView edit = (ImageView) layout.findViewById(R.id.popup_edit_item);

        edit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popup.dismiss();
                renameFlow(cardPosition, cardViewClicked);
            }
        });

        // Displaying the popup at the specified location, + offsets.
        popup.showAsDropDown(cardViewClicked, cardViewClicked.getMeasuredWidth(),popupDisplayHeight, Gravity.TOP);

        longClickPopup = popup;
    }

    private void renameFlow(final int cardPosition, final View cardViewClicked) {
        final ViewSwitcher switcher = (ViewSwitcher) cardViewClicked.findViewById(R.id.rename_switcher);
        final EditText rename = (EditText) switcher.findViewById(R.id.item_flow_rename);

        rename.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (rename.hasFocus()) {
                    showEditPopupWindow(rename, cardViewClicked, switcher, cardPosition);
                } else {
                    imm.hideSoftInputFromWindow(rename.getWindowToken(), 0);
                }

            }
        });

        switcher.showNext();

        rename.requestFocus();
        imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, InputMethodManager.HIDE_IMPLICIT_ONLY);
        /* Forces keyboard */


    }

    private void showEditPopupWindow(final EditText newName, View cardViewClicked, final ViewSwitcher switcher, final int cardPosition) {
        LayoutInflater layoutInflater = (LayoutInflater) this
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View layout = layoutInflater.inflate(R.layout.popup_window_editing, null);

        LinearLayout viewGroup = (LinearLayout)  layout.findViewById(R.id.popup_editing);

        // Creating the PopupWindow
        final PopupWindow popup = new PopupWindow(layout, RecyclerView.LayoutParams.WRAP_CONTENT,
                RecyclerView.LayoutParams.WRAP_CONTENT);

        popup.setFocusable(false); // So that user can edit text

        // Getting a reference to Close button, and close the popup when clicked.
        ImageView confirmEdit = (ImageView) layout.findViewById(R.id.popup_confirm_item_changes);

        confirmEdit.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                /* .. Changes name of cardview through edit text */
                    switcher.showNext();
                    popup.dismiss();
                    newName.clearFocus();
                }

            }
        });

        ImageView cancelEdit = (ImageView) layout.findViewById(R.id.popup_cancel_item_changes);

        cancelEdit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switcher.showNext();
                popup.dismiss();
            }
        });

        popup.showAsDropDown(cardViewClicked, cardViewClicked.getMeasuredWidth(),popupDisplayHeight, Gravity.TOP);
        editingPopup = popup;
    }


    @Override
    protected void onPause() {
        dismissPopups();
        super.onPause();
    }

    private void dismissPopups() {
        if (longClickPopup!=null && longClickPopup.isShowing()) {
            longClickPopup.dismiss();
        }

        if (editingPopup!=null && editingPopup.isShowing()) {
            editingPopup.dismiss();
        }
    }
}

对于视觉人士

在此处输入图像描述

在此处输入图像描述

在此处输入图像描述

4

1 回答 1

0

我解决了这个问题......而且它惊人地大并且与焦点/弹出窗口完全无关(我猜是隧道视觉)。

在我的 Manifest 中android:launchMode="singleTop",当 TheHubActivity 被发送到最近的应用程序时,我正在使用它创建奇怪的行为,因为这是我的入口活动。从 Developer Docs singleTop 函数中,如下所示:

类似地,也可以创建“singleTop”活动的新实例来处理新意图。但是,如果目标任务在其堆栈顶部已经有活动的现有实例,则该实例将接收新意图(在 onNewIntent() 调用中);未创建新实例。在其他情况下——例如,如果“singleTop”活动的现有实例在目标任务中,但不在堆栈的顶部,或者如果它在堆栈的顶部,但不在目标任务中 -将创建新实例并将其推送到堆栈上。

<activity
            android:name=".TheHubActivity"
            android:label="@string/app_name"
            ~~~~~~android:launchMode="singleTop"~~~~~~~~
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
于 2016-08-21T20:22:02.520 回答