自定义键盘可见时如何隐藏默认的Android键盘? [英] How to hide default Android keyboard when custom keyboard is visible?

查看:154
本文介绍了自定义键盘可见时如何隐藏默认的Android键盘?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有Activity的Android应用,其中包含如下所示的TextView.

I have an Android app with Activity containing a TextView as below.

                <EditText
                android:id="@+id/textEdit1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:paddingLeft="8dip"
                android:paddingRight="8dip"
                android:lines="4"
                android:hint="My hint"
                android:layout_gravity="center_horizontal|center_vertical"
                android:gravity="left"
                android:selectAllOnFocus="true"
                android:textAppearance="?android:attr/textAppearanceSmall"/>

当用户在此TextView中(仅在此TextView中)单击时,我想显示一个自定义键盘.我在此处阅读了出色的文章,并使用了其中开发的CustomKeyboard类.该类包含以下"hack"以隐藏默认键盘并显示我们的自定义键盘.

When the user clicks within this TextView (and only within this TextView), I want to show a custom keyboard. I read the excellent article here and used the CustomKeyboard class developed therein. The class contains the following "hack" to hide the default keyboard and show our custom keyboard.

// edittext is the TextView on which we want to show custom keyboard
// Rest of the code below from CustomKeyboard class
// Disable standard keyboard hard way
edittext.setOnTouchListener(new OnTouchListener() {
    @Override public boolean onTouch(View v, MotionEvent event) {
        EditText edittext = (EditText) v;
        int inType = edittext.getInputType();       // Backup the input type
        edittext.setInputType(InputType.TYPE_NULL); // Disable standard keyboard
        edittext.onTouchEvent(event);               // Call native handler
        edittext.setInputType(inType);              // Restore input type
        return true; // Consume touch event
    }
});

这将显示自定义键盘,而不显示默认键盘.但是,这有一个令人讨厌的问题.这会导致光标停留在TextView的第一个字符上.无论我在TextView中单击什么位置,光标都仍在第一个字符处.

This shows the custom keyboard and does not show the default keyboard. However, this has one nasty problem. It results in the cursor getting stuck on the first character within the TextView. No matter where I click in the TextView, the cursor is still at the first character.

根据关于StackOverflow的其他一些建议,我尝试重新编写如下代码:

Based on some other suggestions on StackOverflow, I tried re-writing the code as below:

// edittext is the TextView on which we want to show custom keyboard
    edittext.setOnTouchListener(new OnTouchListener() {
        @Override public boolean onTouch(View v, MotionEvent event) {
            EditText edittext = (EditText) v;
            int inType = edittext.getInputType();       // Backup the input type
            edittext.setInputType(InputType.TYPE_NULL); // Disable standard keyboard

            edittext.onTouchEvent(event);               // Call native handler

            float x = event.getX();
            float y = event.getY();
            int touchPosition = edittext.getOffsetForPosition(x, y);
            if (touchPosition>0){
                edittext.setSelection(touchPosition);
            }
            edittext.setInputType(inType);              // Restore input type
            return true; // Consume touch event 
        }
    });

这没有任何效果.当我记录touchPosition变量的值时,它始终显示-1,但我确实看到x和y的值在合理范围内变化.

This does not have any effect. When I log the value of touchPosition variable, it always shows -1, but I do see x and y changing with reasonable values.

我现在一无所知.我尝试了其他地方建议的其他几种解决方案,但无法使其仅显示具有适当光标处理功能的自定义键盘.

I am clueless now. I have tried several other solutions suggested elsewhere, but not able to get it show only the custom keyboard with proper cursor handling.

非常感谢您的帮助.

推荐答案

经过反复试验,我找到了解决方法.

After a lot of trial and error, I found out how to do this.

以前,我将键盘隐藏代码放置在EditText的onTouch(...)处理程序中.无论出于何种原因,这都不起作用.它仍然显示两个键盘(默认和自定义).

Earlier, I had placded the keyboard-hiding code in the onTouch(...) handler of the EditText. For whatever reason, that does not work. It still shows both keyboards (default and custom).

我将代码移至活动,如下所示:

I moved that code to the activity as follows:

private OnTouchListener m_onTouchListenerEditText = new OnTouchListener()
{
    @Override
    public boolean onTouch(View v, MotionEvent event)
    {
        if (v != m_myEditText)
        {
            // This is the function which hides the custom keyboard by hiding the keyboard view in the custom keyboard class (download link for this class is in the original question)
            m_customKeyboard.hideCustomKeyboard();
            ((EditText) v).onTouchEvent(event);
        }

        return true;
    }
};

private void hideDefaultKeyboard()
{
    getWindow().setSoftInputMode(
            WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
}

这篇关于自定义键盘可见时如何隐藏默认的Android键盘?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆