Android Hide Soft Keyboard from EditText while not losing cursor(Android在不丢失光标的情况下从EditText隐藏软键盘)
问题描述
我已经完成了 这个 这让我走到了一半,但并不完全.我有一个拨号器 Fragment
,它有所有常用的 Button
来输入数字,包括退格,所以我不需要软键盘.我还想让用户能够粘贴文本(长按...默认情况下可以正常工作),以及编辑输入的内容,所以我需要光标.
I've come about as far as this which gets me halfway there, but not quite.
I have a dialer Fragment
that has all the usual Button
s to enter a number including backspace, so I don't need the soft keyboard. I'd also like to give the user the ability to paste text (long click... works fine per default), as well as to edit what has been entered so I need the cursor.
如果用户在 EditText
内单击,我发现确保不会弹出软键盘的最简单方法是将 inputType
设置为 null - 但是也杀死光标.
The easiest way I found to make sure the soft keyboard doesn't pop up if the user clicks inside the EditText
is to set the inputType
to null - but that kills the cursor as well.
那么,我该如何声明我的 EditText
以及我应该启动什么样的命令才能让我的 EditText
字段永远不会显示软键盘,无论用户尝试什么,但仍保留粘贴功能和光标?
So, how do I declare my EditText
and what kind of commands should I launch to have my EditText
field never ever show the soft keyboard no matter what the user attempts, but still retain paste functionality and the cursor?
我也在清单中尝试了 android:windowSoftInputMode="stateAlwaysHidden"
,但无济于事.
I've also tried android:windowSoftInputMode="stateAlwaysHidden"
in my manifest, but to no avail.
推荐答案
这对我有用:
// Update the EditText so it won't popup Android's own keyboard, since I have my own.
EditText editText = (EditText)findViewById(R.id.edit_mine);
editText.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
v.onTouchEvent(event);
InputMethodManager imm = (InputMethodManager)v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null) {
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
return true;
}
});
这篇关于Android在不丢失光标的情况下从EditText隐藏软键盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Android在不丢失光标的情况下从EditText隐藏软键盘
基础教程推荐
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01