【问题标题】:Prevent Drawer Menu from showing when keyboard is present? (Android Appcelerator)防止出现键盘时显示抽屉菜单? (安卓应用加速器)
【发布时间】:2017-10-02 11:42:01
【问题描述】:

我想阻止用户在键盘存在时打开抽屉式导航菜单。我尝试向 TextField 添加“焦点”和“模糊”侦听器:

$.searchField.addEventListener('focus', function(e) {
    Ti.API.info('locked drawer');
    $.drawerLayout.drawerLockMode = Titanium.UI.Android.DrawerLayout.LOCK_MODE_LOCKED_CLOSED;
});

$.searchField.addEventListener('blur', function(e) {
    Ti.API.info('unlocked drawer');
    $.drawerLayout.drawerLockMode = Titanium.UI.Android.DrawerLayout.LOCK_MODE_UNLOCKED;
});

但是你可以看到什么都没有发生:

【问题讨论】:

    标签: android titanium appcelerator


    【解决方案1】:

    我使用 NL Fokke Drawer Widget 完成了类似的操作。

    使用这个小部件,我创建了这样的窗口:

    XML:

    <Alloy>
      <Widget id="drawer" src="nl.fokkezb.drawer">
        <View module="xp.ui" role="leftWindow">
    
        </View>
    
        <NavigationWindow module="xp.ui" platform="ios" role="centerWindow">
            <Window>
                <Require src="homeView"></Require>
            </Window>
        </NavigationWindow>
    
        <Window module="xp.ui" platform="android" role="centerWindow">
            <Require src="homeView"></Require>
        </Window>
    </Widget>
    

    JS:

    // to close the drawer   
    $.drawer.instance.setDrawerLockMode($.drawer.module.LOCK_MODE_UNLOCKED);
    
    // to unlock the drawer
    $.drawer.instance.setDrawerLockMode($.drawer.module.LOCK_MODE_LOCKED_CLOSED);
    

    如果您不使用此小部件,那么我相信您可以将您的代码与此小部件的代码匹配并查看它是如何设置锁定模式的。

    【讨论】:

    • 我知道锁定/解锁抽屉的方法,但我想要的是当用户在 TextField 上键入时(当键盘存在时)锁定抽屉,并在键盘时解锁它不存在。
    • 您的焦点和模糊事件没有启动吗?因为其余的代码对我来说似乎很好。
    • 是的,我的事件没有启动。
    【解决方案2】:

    将此代码添加到您的活动中

        @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        View v = getCurrentFocus();
        if (v != null &&
                (ev.getAction() == MotionEvent.ACTION_UP || ev.getAction() == MotionEvent.ACTION_MOVE) &&
                v instanceof EditText &&
                !v.getClass().getName().startsWith("android.webkit.")) {
            int scrcoords[] = new int[2];
            v.getLocationOnScreen(scrcoords);
            float x = ev.getRawX() + v.getLeft() - scrcoords[0];
            float y = ev.getRawY() + v.getTop() - scrcoords[1];
            if (x < v.getLeft() || x > v.getRight() || y < v.getTop() || y > v.getBottom())
                hideKeyboard(this);
        }
        return super.dispatchTouchEvent(ev);
    }
    
    public void hideKeyboard(Activity activity) {
        if (activity != null && activity.getWindow() != null && activity.getWindow().getDecorView() != null) {
            InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(activity.getWindow().getDecorView().getWindowToken(), 0);
        }
    }
    

    【讨论】:

    • 我使用的是 Appcelerator Titanium,而不是原生开发。
    • 对不起@guillefix 我不知道
    • @RameshYogu,请在回答之前尝试正确查看问题。这清楚地表明这个问题是针对钛的。
    猜你喜欢
    • 2016-07-24
    • 2011-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多