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

查看:38
本文介绍了从 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.

我试过 android:focusable="false" 但它禁用剪切/复制(但仍然可以粘贴).

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) 以编程方式执行以下操作

2) Programatically do the below

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

你已经完成了.

如果你想满足 API <11 同样,我发现如果您想选择文本进行复制粘贴,则无法禁用键盘弹出.将 focusable 设置为 false 将禁用键盘,但它无济于事,因为它也会禁用您选择文本的能力.我在 stackoverflow 中找到的任何其他解决方案都不起作用或同时禁用文本选择.

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"

是的,这已被弃用,但有必要在 API 版本中使 EditText 不可编辑

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.

使用下面的代码检测键盘是否出现(解决方案来自 https://stackoverflow.com/a/9108219/1241783),并立即隐藏它.

Use this code below to detect keyboard showing up (solution obtained from https://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天全站免登陆