Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 从EditText禁用软键盘,但仍允许复制/粘贴?_Android_Android Edittext - Fatal编程技术网

Android 从EditText禁用软键盘,但仍允许复制/粘贴?

Android 从EditText禁用软键盘,但仍允许复制/粘贴?,android,android-edittext,Android,Android Edittext,您好,我正在制作自定义拨号器,所以我创建了自己的输入板 问题是如何禁用编辑文本,但仍然允许剪切/复制/粘贴?股票拨号程序可以这样做 我已经尝试了android:focusable=“false”,但它禁用了剪切/复制(仍然可以粘贴) 我还尝试通过编程禁用inputType,这将禁用所有三个命令: myEditText.setInputType(InputType.TYPE_NULL); //Can't cut/copy/paste 从清单中禁用它也不起作用: android:configCha

您好,我正在制作自定义拨号器,所以我创建了自己的输入板

问题是如何禁用编辑文本,但仍然允许剪切/复制/粘贴?股票拨号程序可以这样做

我已经尝试了android:focusable=“false”,但它禁用了剪切/复制(仍然可以粘贴)

我还尝试通过编程禁用
inputType
,这将禁用所有三个命令:

myEditText.setInputType(InputType.TYPE_NULL); //Can't cut/copy/paste
从清单中禁用它也不起作用:

android:configChanges="orientation|keyboardHidden" //Keyboard still popped up
有解决办法吗?谢谢你试试这个

 EditText et = ... // your EditText

et.setKeyListener(null) //makes the EditText non-editable so, it acts like a TextView.

不需要子类。这与使您的EditText不可聚焦的主要区别在于,EditText仍然有自己的光标-您可以选择文本等。它所做的只是抑制IME弹出自己的软键盘。

经过数小时的研究,我终于找到了一个适用于所有API版本的解决方案。希望这能节省别人的时间

如果您正在为API>=11开发,那么解决方案很简单,可以是:

1) 在EditText的xml文件中添加以下两个属性

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

2) 按程序执行以下操作

myEditText.setInputType(InputType.TYPE_NULL);
myEditText.setTextIsSelectable(true);
你完成了

如果您还想满足API<11的要求,我发现如果您想选择文本进行复制粘贴,则无法禁用键盘弹出。将focusable设置为false将禁用键盘,但这没有帮助,因为它也会禁用您选择文本的功能。我在stackoverflow all中找到的任何其他解决方案要么不起作用,要么同时禁用文本选择

解决这一问题的一个丑陋的方法就是

首先,在EditText的xml文件中添加此属性

android:inputType="none"
android:textIsSelectable="true"
android:editable="false"
是的,这是不推荐的,但在API版本<11时,使EditText不可编辑是必需的

接下来,我们需要在键盘出现时立即隐藏它,这样我们就可以继续选择文本,而不会被键盘挡住

使用下面的代码检测键盘显示(从中获得的解决方案),并立即将其隐藏

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);
            }
            }
         }
        });
}
if(android.os.Build.VERSION.SDK\u INT100){//如果超过100像素,它可能是一个键盘。。。
//立即隐藏键盘!
如果(getCurrentFocus()!=null)
{
InputMethodManager imm=(InputMethodManager)getSystemService(Context.INPUT\u方法\u服务);
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),0);
}
}
}
});
}
这对我的案子有效。虽然你可以在一瞬间看到键盘的出现(这是最难看的部分),但是我想不出任何其他的方法来让它在写作时工作。如果您有更好的解决方案,请留言


如果这能节省某人的时间,也请告诉我:)

由于当前的顶级答案使用了一种不推荐的方法,并且我没有粘贴方法,因此这里有另一种不使用旧方法的方法。但是,它确实尝试通过具有回退功能的反射来使用隐藏方法

我已经将
EditText
子类化为一个名为
keyboardlesseditext
的新小部件,它仍然保留了所有很酷的编辑功能,而没有显示键盘。把文件放进去就行了


这篇文章的完整代码有点长,但只要GitHub没有关闭,这就行了:

要禁用软键盘显示,保持复制/粘贴和光标功能,只需在活动中添加这一行:

getWindow().setFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM,
    WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);

要禁用
编辑文本
文本视图
的系统键盘自动弹出窗口,请执行以下操作:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    editTextView.setShowSoftInputOnFocus(false);
} else {
    editTextView.setTextIsSelectable(true);
    //N.B. Accepting the case when non editable text will be selectable
}

由于我的自定义内联“假”输入,在焦点移动到编辑文本后,操作系统软键盘出现时,仍然可见,因此有类似的需求。 解决方案是使编辑文本隐藏软输入,直到上一个自定义输入小部件完成其编辑生命周期

用于灵感,也看到了一些相关的帖子,我将在最后附上。 我发现有效的解决方案是:

fun setInputType(inputType: Int) {
        getEditText().setRawInputType(inputType)
        if (inputType == InputType.TYPE_NULL) {
            getEditText().setTextIsSelectable(true)
            getEditText().isCursorVisible = true
        }
    }
InputType设置时,必须使用
setRawInputType()
,因为不考虑多行文本输入。键入\u NULL
返回到
InputType。键入\u text\u FLAG\u多行

似乎有用户报告与调用
setInputType(InputType.TYPE\u NULL)
有关的问题。见:

其他有用的相关员额:


我也遇到了同样的问题,但我还想稍后允许在双击后键入。。经过一个又一个小时,我找到了工作的解决方案(至少对我来说是这样)。在onCreate方法中使用此选项:

editText.setCursorVisible(false);
editText.setTextIsSelectable(true);
editText.setShowSoftInputOnFocus(false);
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);  // This just hide keyboard when activity starts
这些台词肯定会起作用。。如果您想恢复,请使用以下命令:

editText.setCursorVisible(true);
editText.setShowSoftInputOnFocus(true);
要再次显示键盘,请使用:

private void showSoftKeyboard(View view) {
    InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
    view.requestFocus();
    inputMethodManager.showSoftInput(view, 0);
}
要允许下次复制/粘贴,请使用以下三行:

editText.setCursorVisible(false);
editText.setTextIsSelectable(true);
editText.setShowSoftInputOnFocus(false);
如需进一步使用键盘隐藏:

private void hideSoftKeyboard() {
    if(getCurrentFocus() != null) {
        InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
        inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
    }
}

此代码正在API>=21上运行。希望它对某人有所帮助

谢谢你的回复。但这样会隐藏光标,因此不允许我剪切/复制/粘贴。谢谢,这比我以前使用的更好,但至少KitKat仍然没有默认的粘贴选项?我只看到长按全选和复制..这很好,但是
myEditText.setInputType(InputType.TYPE\u NULL)
使EditText单行,即使是组合
InputType.TYPE_NULL | InputType.TYPE_TEXT_FLAG_MULTI_line
我只需要做
android:textIsSelectable=“true”
。有什么新的吗