从EditText上禁用软键盘,但仍然允许复制/粘贴? [英] Disable soft-keyboard from EditText but still allow copy/paste?

查看:275
本文介绍了从EditText上禁用软键盘,但仍然允许复制/粘贴?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我正在做的自定义的拨号程序让我创造我自己的输入垫。

Hi I'm making custom dialer so I create my own input pad.

现在的问题是我怎么禁用的EditText ,但仍然允许剪切/复制/粘贴?股票拨号器可以做到这一点。

The problem is how do I disable the EditText but still allow cut/copy/paste? The stock dialer can do this.

我已经试过机器人:可调焦=假但禁用剪切/复制(仍然可以粘贴虽然)

I have tried android:focusable="false" but it disables cut/copy (can still paste though).

我也试图禁用 inputType 编程它可禁止所有三个命令:

I also tried to disable the inputType programatically which disables all three commands:

myEditText.setInputType(InputType.TYPE_NULL); //Can't cut/copy/paste

从清单中禁止它也不起作用:

Disabling it from manifest also doesn't work:

android:configChanges="orientation|keyboardHidden" //Keyboard still popped up

任何解决办法?谢谢

Any solution? Thanks

推荐答案

在时间和研究时间,我终于找到了一个解决方案,适用于所有的API版本。希望这样可以节省别人的时间。

After hours and hours of research, I finally found a solution that works for all API versions. Hope this saves someone's time.

如果您正在开发的API> = 11,解决方法很简单,无论是:

If you are developing for API >= 11, the solution is simple, either:

1)增加两个属性下面的EditText的xml文件

1) Add the two properties below in the xml file of EditText

android:inputType="none"
android:textIsSelectable="true"

2)通过编程做以下

myEditText.setInputType(InputType.TYPE_NULL);
myEditText.setTextIsSelectable(true);

和你就大功告成了。

如果你想满足API< 11还有,我发现,有没有办法从弹出,如果你想选择文本复制粘贴目的禁用键盘。设置可聚焦为false将禁用键盘,但它并没有帮助,因为它禁止你的能力来选择文字了。任何其他解决方案,我发现在所有的计算器要么不工作或禁用文本选择在同一时间了。

If you want to cater for API < 11 as well, I found that there is no way to disable to keyboard from popping out if you wanted to select the text for copy paste purpose. Setting focusable to false will disable the keyboard but it doesn't help because it disables your ability to select text too. Any other solutions I found in stackoverflow all either doesn't work or disables text selection at the same time too.

一种丑陋的方式来解决,这是因为这样的..

One ugly way to solve this is as such..

首先,在EditText上的XML文件中添加这个属性。

First, add this property in the xml file of EditText

android:editable="false"

是的,这是德precated,但必要使EditText上的API版本和LT不可编辑; 11。

Yes this is deprecated, but necessary for making the EditText not editable in API version < 11.

接下来,我们需要尽快隐藏键盘,因为它显示出来,这样我们就可以继续无键盘挡住了路选择文本。

Next, we will need to hide the keyboard as soon as it shows up, so that we can continue selecting text without the keyboard blocking the way.

使用下面这个code检测键盘显示出来(溶液从 http://stackoverflow.com/a/9108219获得/ 1241783 ),并立即将其隐藏。

Use this code below to detect keyboard showing up (solution obtained from http://stackoverflow.com/a/9108219/1241783), and hide it immediately.

if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.HONEYCOMB)
{
    final View activityRootView = findViewById(R.id.activityRoot);
    activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
    @Override
        public void onGlobalLayout() {
            Rect r = new Rect();
            //r will be populated with the coordinates of your view that area still visible.
            activityRootView.getWindowVisibleDisplayFrame(r);

            int heightDiff = activityRootView.getRootView().getHeight() - (r.bottom - r.top);
            if (heightDiff > 100) { // if more than 100 pixels, its probably a keyboard...

            //Hide the keyboard instantly!
            if (getCurrentFocus() != null) 
            {
                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
            }
            }
         }
        });
}

它为我的情况。虽然你可以看到键盘显示在一个瞬间(这是丑陋的部分),但我想不出任何其他方式得到这个工作,在写作的时候。如果你有一个更好的解决方案,请发表评论!

It works for my case. Though you can see the keyboard showing up in a split second (which is the ugly part) but I can't think of any other way to get this to work at the time of writing. If you have a better solution, please leave a comment!

让我知道太多,如果这样可以节省别人的时间:)

Let me know too if this saves someone's time :)

这篇关于从EditText上禁用软键盘,但仍然允许复制/粘贴?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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