android - 当我从对话框 fragment 返回并且编​​辑文本获得焦点时,不会调用 onBackPressed

标签 android android-fragments

我在 Android 中构建的 UI 表单中遇到问题。 在那种形式中,我有一些编辑文本,用户必须在其中触摸它们才能打开对话框 fragment 。在对话框 fragment 中,用户可以设置一个值,然后这个值显示在触摸的编辑文本上。问题如下:当用户关闭对话框 fragment 并且触摸的编辑文本获得焦点时,如果用户按下后退按钮退出,则不会调用 onBackPressed() 方法。

我必须澄清,打开对话框 fragment 的编辑文本不显示键盘,因为用户不能在上面写字。我不想使用 TextView 。

这里我展示了部分布局:

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        ....

        <android.support.design.widget.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/text_input_when"
            android:layout_marginTop="30dp">

            <EditText
                android:hint="@string/meeting_when"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/edit_text_when"
                android:textSize="18sp"
                android:inputType="text|date"
                android:textIsSelectable="true"
                android:focusable="true"
                android:drawableLeft="@drawable/ic_black_18dp"
                android:drawableStart="@drawable/ic_black_18dp"
                android:drawablePadding="10dp"
                android:onClick="onEditTextWhenClicked"
                android:nextFocusForward="@+id/edit_text_time"/>

        </android.support.design.widget.TextInputLayout>

        <android.support.design.widget.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/text_input_time"
            android:layout_marginTop="30dp">

            <EditText
                android:hint="@string/meeting_time"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/edit_text_time"
                android:textSize="18sp"
                android:inputType="time"
                android:textIsSelectable="true"
                android:nextFocusForward="@+id/edit_text_place"
                android:focusable="true"
                android:drawableLeft="@drawable/ic__black_18dp"
                android:drawableStart="@drawable/ic__black_18dp"
                android:drawablePadding="10dp"
                android:onClick="onEditTextTimeClicked" />

        </android.support.design.widget.TextInputLayout>
....
....
</LinearLayout>

enter image description here

因此,例如,如果用户触摸“何时”编辑文本,则会打开一个日期选择器对话框:

enter image description here

当用户设置日期时,对话框 fragment 关闭并在编辑文本上设置值

enter image description here

现在,如果用户按下后退按钮,它就不起作用。

在我的 Activity 中

@Override
public void onBackPressed(){

    if ( !areAllFieldEmpty() ) {

        showAlertCloseDialog();
    }else
        super.onBackPressed();

}

但是这些方法并没有被调用。 . 我不知道如何解决它。请帮我。如果您需要更多信息,请告诉我。谢谢。

最佳答案

在您的 fragment 中注册一个回调,以便在您的 View 中按下硬件键时调用:

if (mLayout != null) {
    mLayout.setFocusableInTouchMode(true);
}
mLayout.setOnKeyListener(new View.OnKeyListener() {
    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            getActivity().onBackPressed();
            return true;
        }
        return false;
    }
});

如果是返回键,则返回并消费返回true的事件,否则返回false

如果您在使用背部和键盘时遇到问题 override onKeyPreIme() extending your EditText :

@Override
public boolean onKeyPreIme(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        // User has pressed Back key. So hide the keyboard
        InputMethodManager mgr = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
        mgr.hideSoftInputFromWindow(this.getWindowToken(), 0);
        // Hide your view as you do it in your activity
    } else if (keyCode == KeyEvent.KEYCODE_MENU) {
        // Eat the event
        return true;
    }
    return false;
}

关于android - 当我从对话框 fragment 返回并且编​​辑文本获得焦点时,不会调用 onBackPressed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42352385/

相关文章:

java - 打开相机后未调用 onActivityResult

android - 捕获 4 :3 camera picture via android Camera2 API on camera with 16:9 sensor array

android - Azure 流式传输仅在 Android 应用程序内缓慢

android - fragment 中的 AdMob 横幅

android - ViewPager 中的 RecyclerView 不会滚动

android - 使用 Retrofit 和 Gson 提取 JSON 数据

java - 跨不同类使用 A 变量。安卓系统

android - 从 Android 内容 URI 获取 FileInputStream(或如何在 InputStream 上查找)

android - 在 fragment 中单击按钮后填充 ListView

android - 应用程序被杀死并恢复后,如何避免 Activity 中的多个 fragment 实例?