2010-03-19 665 views
37

我试图强制在活动中打开软键盘,并抓住输入的所有内容,因为我想自己处理输入,我没有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); 
  1. 我怎么强行打开
  2. 我如何瞎扯进入正题,这样我可以处理每一个字符都软键盘。我想在处理完软键盘后清除每个字符。即,用户不应该能够在软键盘中输入整个单词。
+0

嗨, 我有你同样的问题。我可以显示键盘,但是如何获取没有EditText的输入内容? Thanx! – 2013-05-20 14:59:13

回答

13

你可能需要有某种形式的可编辑文本区采取集中。不过,你可能有一个不可见的透明背景或者没有光标的透明背景。您可能需要仔细研究视图的可聚焦性设置。

使用TextWatcher检查EditText对addTextChangedListener的编辑,或者如果您需要更低级别的挂钩,请使用setOnKeyListener()方法设置textview的关键侦听器。请参阅KeyListener documentation

使用这个调用来强制软键盘开:

((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE)) 
    .showSoftInput(myEditText, InputMethodManager.SHOW_FORCED); 

而这一次将其关闭:

((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE)) 
    .hideSoftInputFromWindow(myEditText.getWindowToken(), 0); 

注意,这是真的不推荐 - 迫使键盘开放是怎么样的乱。你真正需要的用例是什么,你需要在没有正常编辑框的情况下接受用户输入,并且需要在逐个键的基础上进行用户输入而不回显?

+0

我从Droid中收到了大量的错误。 特别是java.lang.IndexOutOfBoundsException:getChars(6 ... 0)在启动bug之前已结束。这是由于EditText导致的EditText – jax 2010-03-20 03:36:36

+0

?此错误来自哪里,TextWatcher,KeyListener或showSoftInput/hideSoftInput调用?您还必须发布代码和堆栈跟踪,以便任何人都能够在此帮助您。 – 2010-03-20 18:12:57

+0

它没有为我工作。 – 2012-04-23 00:48:17

121

尝试这种强行打开软键盘:

((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE)).toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY); 

那么你可以使用此代码关闭键盘:

((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(_pay_box_helper.getWindowToken(), 0); 
+0

谢谢。 “关闭键盘”示例中的_pay_box_helper是什么?编辑:啊,我知道,这是一个文本字段变量。 – 2012-03-26 13:19:23

+3

它没有为我工作。 – 2012-04-23 00:48:26

+0

对我来说很棒,可以在Action Bar Sherlock的可折叠菜单上编辑文本。谢谢。 – Soham 2013-01-10 19:06:33

8

要强制键盘打开我用

this.getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE); 

它为我工作。

+0

适用于我,但你必须在超级之前调用它。'onCreate'中的onCreate()' – Palani 2013-06-17 13:03:45

+0

这不适用于我(API23,Google Nexus) – keinabel 2016-11-08 00:52:44

5

有时其他答案将无法正常工作。
这是另一种方式..

它会强制键盘显示活动何时开始通过听窗口焦点。 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()); 
    } 
} 

您可能还需要

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

编辑:我也看到了键盘无法打开里面嵌套的片段,这些种情况当心。

1

,如果你想控制软键盘的活动里面,然后使用此代码:

//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(); 
0
if(search.getText().toString().trim().isEmpty()) { 
    InputMethodManager imm = (InputMethodManager)getSystemService(
       Context.INPUT_METHOD_SERVICE); 
    imm.hideSoftInputFromWindow(search.getWindowToken(), 0); 
} 
+1

请在[编辑]中解释为什么/如何代码回答问题?不提供代码的答案是不鼓励的,因为它们不像代码解释那样容易学习。没有解释,需要花费更多的时间和精力去理解正在做什么,对代码所做的修改,代码是否回答问题等等。对于试图从答案中学习的人以及那些评估回答是否有效,或值得投票。 – Makyen 2015-02-25 05:59:58

+0

请参阅http://stackoverflow.com/help/how-to-answer – SMR 2015-02-25 09:02:22

-1

我已经测试过,这是工作:

... //显示软键盘

imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0); 

//隐藏它,再次调用方法

imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0); 
+1

错误的复制和粘贴 – webbi 2016-08-12 16:50:43

1

不幸的是,尽管我希望对投票中的一个回复投票,但没有人为我工作。看来解决方案是等待布局阶段完成。在下面的代码中,请注意我如何检查showKeyboard方法是否返回TRUE,这是我删除全局布局侦听器的时候。如果不这样做,它会被击中和错过。现在它看起来很完美。

你需要做以下最好是在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; 
} 
0

工作大.........

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

以下线电话时要打开键盘

edt_searchfilter_searchtext.requestFocus(); 
0

简单地说,使用添加2号线将工作就像一个魅力:

如果使用XML

android:focusable="true" 
android:focusableInTouchMode="true" 
在Java中

否则:

view.setFocusableInTouchMode(true); 
view.requestFocus();