Android 强制打开软键盘
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2479504/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Forcing the Soft Keyboard open
提问by jax
I am trying to force the Soft Keyboard open in an Activity and grab everything that is entered as I want to handle the input myself, I don't have an EditText. Currently I have tried this but it does not work. I would like the Soft Keyboardto open below mAnswerTextView (Note: it is a TextView not EditText).
我试图在活动中强制打开软键盘并抓取输入的所有内容,因为我想自己处理输入,我没有 EditText。目前我已经尝试过这个,但它不起作用。我希望软键盘在 manswerTextView 下方打开(注意:它是一个 TextView 而不是 EditText)。
InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
// only will trigger it if no physical keyboard is open
mgr.showSoftInput(mAnswerTextView, InputMethodManager.SHOW_IMPLICIT);
- how do I force the Soft Keyboard open
- How do I gab everything that is entered so that I can handle each character. I would like to flush each character from the Soft Keyboard after I have handled it. ie, the user should not be able to enter whole words in the Soft Keyboard.
- 如何强制打开软键盘
- 我如何处理输入的所有内容,以便我可以处理每个字符。我想在处理完软键盘后刷新每个字符。即,用户不应该能够在软键盘中输入整个单词。
回答by Dmitry
try this to force open soft keyboard:
试试这个来强制打开软键盘:
((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE)).toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
then you can to use this code to close the keyboard:
然后您可以使用此代码关闭键盘:
((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(_pay_box_helper.getWindowToken(), 0);
回答by Yoni Samlan
You'll probably need to have an editable text area of some kind to take focus. You can probably have one invisible or on a transparent background with no cursor, though. You may need to toy around with the focusability settings for the view.
您可能需要有某种可编辑的文本区域才能聚焦。不过,您可能有一个不可见的或在没有光标的透明背景上。您可能需要调整视图的可聚焦性设置。
Use a TextWatcher to check for edits to that EditText with addTextChangedListener, or if you need an even-lower-level hook, set the textview's key listener with its setOnKeyListener() method. See the KeyListener documentation.
使用TextWatcher检查编辑到的EditText与addTextChangedListener,或者如果你需要一个偶数较低级别挂钩,其setOnKeyListener()方法设置的TextView的主要听众。请参阅KeyListener 文档。
Use this call to force the soft keyboard open:
使用此调用强制打开软键盘:
((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE))
.showSoftInput(myEditText, InputMethodManager.SHOW_FORCED);
and this one to close it:
和这个关闭它:
((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE))
.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);
note that this is really not recommended - forcing the keyboard open is kind of messy. What's your use case that really necessitates your taking user input without a normal edit box and requires eating user input on a key-by-key basis without echoing it back?
请注意,这确实是不推荐的 - 强制打开键盘有点麻烦。您的用例是什么,真正需要您在没有正常编辑框的情况下获取用户输入,并且需要逐个键地处理用户输入而不回显?
回答by Aditya Mehta
To force the keyboard to open I used
强制键盘打开我用过
this.getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);
it worked for me.
它对我有用。
回答by tricknology
Sometimes the other answers won't work.
Here is another way..
有时其他答案不起作用。
这是另一种方式..
It will force the keyboard to show when the activity starts by listening to the window focus. onWindowFocusChanged()
it will clear and request focus of the EditText, then set the soft input mode to visible and set the selection to the text in the box. This should always work if you are calling it from the activity.
它将通过侦听窗口焦点强制键盘显示活动何时开始。onWindowFocusChanged()
它将清除并请求 EditText 的焦点,然后将软输入模式设置为可见并将选择设置为框中的文本。如果您从活动中调用它,这应该始终有效。
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (hasFocus) {
mEditText.clearFocus();
mEditText.requestFocus();
getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);
mEditText.setSelection(mEditText.getText().toString().length());
}
}
You may also need
您可能还需要
mEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(mEditText, InputMethodManager.SHOW_IMPLICIT);
}
}
});
Edit:I have also seen the keyboard not open inside nested fragments, beware of those kinds of situations.
编辑:我还看到键盘未在嵌套片段内打开,请注意这些情况。
回答by user3309567
if you want to control soft keyboard inside activity then use this code:
如果您想在活动中控制软键盘,请使用以下代码:
//create soft keyboard object
InputMethodManager imm = (InputMethodManager)this.getSystemService(INPUT_METHOD_SERVICE);
//1.USE
your_view.setFocusableInTouchMode(true); //Enable touch soft keyboard to this view
//or
your_view.setFocusable(true); //Enable keyboard to this view
imm.showInputMethod(your_view, InputMethodManager.SHOW_IMPLICIT);
//2.USE show keyboard if is hidden or hide if it is shown
imm.toggleSoftInputFromWindow(your_view.getWindowToken(),InputMethodManager.SHOW_IMPLICIT, InputMethodManager.HIDE_IMPLICIT_ONLY);
//or
imm.toggleSoftInputFromWindow(InputMethodManager.SHOW_IMPLICIT, InputMethodManager.HIDE_IMPLICIT_ONLY);
//3.USE (you cannot control imm)
this.getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);
//4.USE (with Dialog)
Dialog d = new Dialog(this, android.R.style.Theme_Panel);
d.getWindow().setTitle(null);
d.setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE);
d.setOnKeyListener(keyListener);
d.setCanceledOnTouchOutside(true);
d.setCancelable(true);
d.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
d.show();
//to hide keyboard call:
d.dismiss();
//if you want get soft keyboard visibility call:
d.isShowing();
回答by strangetimes
Sadly, as much as I'd have liked to up-vote one of the replies, none worked for me. It seems the solution is to wait for the layout phase to complete. In the code below, notice how I check if the showKeyboard
method returns TRUE, and that's when I remove the global layout listener. Without doing this, it was hit and miss. Now it seems to work perfectly.
可悲的是,尽管我很想对其中一个回复投赞成票,但没有一个对我有用。似乎解决方案是等待布局阶段完成。在下面的代码中,注意我如何检查showKeyboard
方法是否返回 TRUE,那是我删除全局布局侦听器的时候。如果不这样做,它就会受到打击。现在它似乎完美地工作。
You need to do the below ideally in onResume()
您需要在理想情况下执行以下操作 onResume()
@Override
public void onResume()
{
super.onResume();
ViewTreeObserver vto = txtTaskTitle.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener()
{
@Override
public void onGlobalLayout()
{
if (txtTaskTitle.requestFocus())
{
if (showKeyboard(getContext(), txtTaskTitle))
{
txtTaskTitle.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
}
}
});
}
public static boolean showKeyboard(Context context, EditText target)
{
if (context == null || target == null)
{
return false;
}
InputMethodManager imm = getInputMethodManager(context);
((InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE)).showSoftInput(target, InputMethodManager.SHOW_FORCED);
boolean didShowKeyboard = imm.showSoftInput(target, InputMethodManager.SHOW_FORCED);
if (!didShowKeyboard)
{
didShowKeyboard = ((InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE)).showSoftInput(target, InputMethodManager.SHOW_FORCED);
}
return didShowKeyboard;
}
回答by Piyush
Working Great.........
干得好…………
edt_searchfilter_searchtext.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus){
edt_searchfilter_searchtext.post(new Runnable() {
@Override
public void run() {
InputMethodManager imm = (InputMethodManager) getFragmentActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(edt_searchfilter_searchtext, InputMethodManager.SHOW_IMPLICIT);
}
});
}
}
});
Below line call when you want to open keyboard
当您想打开键盘时,请在下线调用
edt_searchfilter_searchtext.requestFocus();
回答by sud007
Simply, using adding 2 lines will work like a charm:
简单地说,使用添加 2 行就像一个魅力:
If using XML
如果使用 XML
android:focusable="true"
android:focusableInTouchMode="true"
Else in Java:
其他在 Java 中:
view.setFocusableInTouchMode(true);
view.requestFocus();
回答by Shanu S
if(search.getText().toString().trim().isEmpty()) {
InputMethodManager imm = (InputMethodManager)getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(search.getWindowToken(), 0);
}
回答by beginner
I have tested and this is working:
我已经测试过,这是有效的:
... //to show soft keyboard
... //显示软键盘
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
//to hide it, call the method again
//要隐藏它,再次调用该方法
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);