2010-03-19 311 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();