Android:在键盘解除后使用EditText的DrawerLayout在按下时关闭应用程序

时间:2014-02-13 16:54:05

标签: android android-softkeyboard drawerlayout

我们有一个NavigationDrawer(使用DrawerLayout)从右侧滑入,这个抽屉有一个EditText。

正常情况下,按下后退按钮时抽屉会关闭。

然而,当我们在EditText中输入文本并使用完成按钮或后退按钮关闭软键盘,然后再次按后退按钮以关闭抽屉时它只关闭底层片段/活动 - 在我们的案例中关闭应用程序。

还有其他人遇到过这个问题吗?

布局

<?xml version="1.0" encoding="utf-8"?>
<!-- A DrawerLayout is intended to be used as the top-level content view using match_parent for both width and height to consume the full space available. -->
<com.example.CustomDrawerLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <!-- As the main content view, the view below consumes the entire
    space available using match_parent in both dimensions. -->
    <FrameLayout android:id="@+id/main_fragment_container"
                 android:layout_weight="1"
                 android:layout_width="match_parent"
                 android:layout_height="match_parent"/>

    <LinearLayout
            android:id="@+id/left_drawer"
            android:layout_width="280dp"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:choiceMode="singleChoice"
            android:clickable="true"
            android:dividerHeight="2dp">
        <fragment
                android:id="@+id/global_nav_fragment"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                class="com.example.GlobalNavFragment"
                tools:layout="@layout/fragment_global_nav_v3"/>
    </LinearLayout>

    <LinearLayout
            android:id="@+id/right_drawer"
            android:layout_width="280dp"
            android:layout_height="match_parent"
            android:layout_gravity="end"
            android:choiceMode="singleChoice"
            android:dividerHeight="2dp">

        <FrameLayout
                android:id="@+id/search_form_fragment"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                tools:layout="@layout/fragment_search_form_v3"/>
    </LinearLayout>

</com.example.CustomDrawerLayout>

的EditText

<EditText 
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight=".90"
    android:id="@+id/option_value"
    android:layout_gravity="center_vertical"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:minWidth="30dp"
    android:ellipsize="end"
    android:singleLine="true"
    android:textColor="@color/white"
    android:textSize="@dimen/search_form_text"
    android:editable="true"
    android:background="@null"
    android:inputType="text"
    android:textColorHint="@color/any_option"
    android:imeOptions="actionDone"
    android:focusable="true"/>

CustomDrawerLayout

public class CustomDrawerLayout extends DrawerLayout {

private BackButtonManager backButtonManager;
private float xDistance, yDistance, lastX, lastY, firstX;


public ATDrawerLayout(Context context) {
    super(context);
}

public ATDrawerLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public ATDrawerLayout(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    if(isPeeking()) {
        return false;
    }

    switch (ev.getAction()) {
        case MotionEvent.ACTION_DOWN:
            xDistance = yDistance = 0f;
            lastX = ev.getX();
            lastY = ev.getY();
            firstX = ev.getX();
            break;
        case MotionEvent.ACTION_MOVE:
            final float curX = ev.getX();
            final float curY = ev.getY();
            xDistance += Math.abs(curX - lastX);
            yDistance += Math.abs(curY - lastY);
            lastX = curX;
            lastY = curY;
            if(isScrollingVertically() || isOpenAndSwipingRightToLeft()) {
                return false;
            }
    }

    return super.onInterceptTouchEvent(ev);
}

private boolean isPeeking() {
    return isDrawerVisible(GravityCompat.END) && !isDrawerOpen(GravityCompat.END);
}

private boolean isScrollingVertically() {
    return yDistance > xDistance;
}

private boolean isOpenAndSwipingRightToLeft() {
    return isDrawerOpen(GravityCompat.END) && firstX > lastX;
}

public void setBackButtonManager(BackButtonManager manager) {
    this.backButtonManager = manager;
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    return keyCode == KeyEvent.KEYCODE_BACK && backButtonManager.hasListeners() || super.onKeyDown(keyCode, event);
}

@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
    //This is to allow the override of back button behaviour
    return keyCode == KeyEvent.KEYCODE_BACK && backButtonManager.back() || super.onKeyUp(keyCode, event);
}

2 个答案:

答案 0 :(得分:0)

我遇到了类似的问题。我的一些片段打开键盘,当我打开自定义抽屉时,应用程序将返回/关闭而不是关闭键盘。

我意识到发生了什么:抽屉失去了焦点,因为键盘正在维护它!

关闭键盘时,请求焦点为抽屉布局:

InputMethodManager inputManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
                if(inputManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(),0)){
                    drawerLayout.requestFocus();
                }

答案 1 :(得分:0)

我遇到了类似的问题。我的一些片段打开键盘,当我打开自定义抽屉时,应用程序将返回/关闭而不是关闭键盘。

我意识到发生了什么:抽屉失去了焦点,因为键盘正在维护它!

关闭键盘时,请求焦点为抽屉布局:

...

@Transient
var _login: String? = null,

...

@NotNull
@Pattern(regexp = Constants.LOGIN_REGEX)
@Size(min = 1, max = 50)
@Column(name = "login", length = 50, unique = true, nullable = false)
var login: String?
    get() = _login
    set(value) {
        _login = StringUtils.lowerCase(value, Locale.ENGLISH)
    }