1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > android 键盘 能复制 android – 从EditText中禁用软键盘 但仍允许复制/粘贴?

android 键盘 能复制 android – 从EditText中禁用软键盘 但仍允许复制/粘贴?

时间:2022-10-23 02:47:01

相关推荐

android 键盘 能复制 android – 从EditText中禁用软键盘 但仍允许复制/粘贴?

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

如果您正在开发API> = 11,解决方案很简单:

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

android:inputType="none"

android:textIsSelectable="true"

要么

2)程序地做如下

myEditText.setInputType(InputType.TYPE_NULL);

myEditText.setTextIsSelectable(true);

你做完了。

如果你想要迎合API< 11,我发现没有办法禁用键盘弹出,如果你想选择文本复制粘贴目的。将焦点设置为false将禁用键盘,但它不会有帮助,因为它会禁用您选择文本的功能。在stackoverflow中找到的任何其他解决方案都不会同时工作或禁用文本选择。 一个丑陋的解决办法就是这样 首先,将此属性添加到EditText的xml文件中

android:editable="false"

是的,这是不推荐的,但是使EditText不能在API版本< 11。 接下来,我们需要在显示屏幕时立即隐藏键盘,以便我们可以继续选择文本而不用键盘阻止。 使用以下代码检测键盘显示(从/a/9108219/1241783获得的解决方案),并立即隐藏。

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);

}

}

}

});

}

它适用于我的情况。虽然你可以看到键盘显示在一个分秒(这是丑的一部分),但我不能想到任何其他方式让这个工作在写作时。如果您有更好的解决方案,请发表评论!

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

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。