Android(Java):focus

android中,要使控件獲得焦點,需要先setFocus,再requestFocus。

以Button爲例:
                btn.setFocusable(true);
                btn.setFocusableInTouchMode(true);
                btn.requestFocus();
                btn.requestFocusFromTouch();

 

//獲得失去焦點的監聽器

btn.setOnFocusChangeListener(new OnFocusChangeListener() {
  
  @Override
  public void onFocusChange(View v, boolean hasFocus) {
   // TODO Auto-generated method stub
   if (hasFocus) {
    btn_box.setBackgroundResource(R.drawable.book_green);
   }else {
    btn_box.setBackgroundResource(R.drawable.book);
   }   
  }
 });

獲取焦點是獲取焦點,彈輸入法是彈輸入法。獲取焦點後並不一定會彈出輸入法,在網上搜了一圈,主流回答是“還有就是已開啓界面就是focus的text的話有可能也是不行的,UI渲染是需要時間的”......
  由於對源碼不懂,我對這一點也沒有個全面的認識。只能將焦點和輸入法分成兩塊來處理。焦點的打開和關閉特別簡單。
  焦點的獲取:
  titleInput.setFocusable(true);
  titleInput.requestFocus();
  焦點的取消:

  titleInput.setFocusable(false);


  1、打開輸入法窗口:


InputMethodManager inputMethodManager = (InputMethodManager)               getSystemService(Context.INPUT_METHOD_SERVICE);

// 接受軟鍵盤輸入的編輯文本或其它視圖

imm.showSoftInput(submitBt,InputMethodManager.SHOW_FORCED);

 


  2、關閉出入法窗口


InputMethodManager inputMethodManager = (InputMethodManager)               getSystemService(Context.INPUT_METHOD_SERVICE);


inputMethodManager.hideSoftInputFromWindow(OpeListActivity.this.getCurrentFocus().getWindowToken(),


  InputMethodManager.HIDE_NOT_ALWAYS);


//接受軟鍵盤輸入的編輯文本或其它視圖

inputMethodManager

.showSoftInput(submitBt,InputMethodManager.SHOW_FORCED);

  3、如果輸入法打開則關閉,如果沒打開則打開


  InputMethodManager m=(InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);


  m.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);


  4、獲取輸入法打開的狀態


  InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
  boolean isOpen=imm.isActive();
  isOpen若返回true,則表示輸入法打開

常量

    public static final int HIDE_IMPLICIT_ONLY

           hideSoftInputFromWindow(IBinder, int)中的標記,默示若是用戶未顯式地顯示軟鍵盤窗口,則隱蔽窗口。

           常量值: 1 (0 x00000001)

    public static final int HIDE_NOT_ALWAYS

    hideSoftInputFromWindow(IBinder, int)中的標記,默示軟鍵盤窗口老是隱蔽,除非開端時以SHOW_FORCED顯示。

    常量值: 2 (0 x00000002)

    public static final int RESULT_HIDDEN

    showSoftInput(View, int, ResultReceiver)和hideSoftInputFromWindow(IBinder, int, ResultReceiver)中ResultReceiver成果代碼標記:軟鍵盤窗口從顯示切換到隱蔽時的狀況。

    常量值: 3 (0 x00000003)

    public static final int RESULT_SHOWN

    showSoftInput(View, int, ResultReceiver)和hideSoftInputFromWindow(IBinder, int, ResultReceiver)中ResultReceiver成果代碼標記:軟鍵盤窗口從隱蔽切換到顯示時的狀況。

    常量值: 2 (0 x00000002)

    public static final int RESULT_UNCHANGED_HIDDEN

    showSoftInput(View, int, ResultReceiver)和hideSoftInputFromWindow(IBinder, int, ResultReceiver)中ResultReceiver成果代碼標記:軟鍵盤窗口不變對峙隱蔽時的狀況。

    常量值: 1 (0 x00000001)

    public static final int RESULT_UNCHANGED_SHOWN

    showSoftInput(View, int, ResultReceiver)和hideSoftInputFromWindow(IBinder, int, ResultReceiver)中ResultReceiver成果代碼標記:軟鍵盤窗口不變對峙顯示時的狀況。

    常量值: 0 (0 x00000000)

    public static final int SHOW_FORCED

    showSoftInput(View, int)標記,默示用戶強迫打開輸入法(如長按菜單鍵),一向對峙打開直至只有顯式封閉。

    常量值: 2 (0 x00000002)

    public static final int SHOW_IMPLICIT

      showSoftInput(View, int)標記,默示隱式顯示輸入窗口,非用戶直接請求。窗口可能不顯示。

      常量值: 1 (0 x00000001)
  
  四、公共辦法

    public void displayCompletions (View view, CompletionInfo[] completions)

    (譯者注:輸入法主動完成)

    public InputMethodSubtype getCurrentInputMethodSubtype ()

    (譯者注:獲取當前輸入法類型?)

    public List<InputMethodInfo> getEnabledInputMethodList ()

    (譯者注:獲取已啓用輸入法列表?)

    public List<InputMethodSubtype> getEnabledInputMethodSubtypeList (InputMethodInfo imi, boolean allowsImplicitlySelectedSubtypes)

    public List<InputMethodInfo> getInputMethodList ()

    (譯者注:獲取輸入法列表)

    public Map<InputMethodInfo, List<InputMethodSubtype>> getShortcutInputMethodsAndSubtypes ()

    public void hideSoftInputFromInputMethod (IBinder token, int flags)

    封閉/隱蔽輸入法軟鍵盤區域,用戶不再看到或與其交互。只能由當前激活輸入法調用,因需令牌(token)驗證。

    參數

    token       在輸入法啓動時供給令牌驗證,驗證後可對其進行操縱。

    flags        供給額外的操縱標記。當前可認爲0或 HIDE_IMPLICIT_ONLY, HIDE_NOT_ALWAYS等位設置。

    public boolean hideSoftInputFromWindow (IBinder windowToken, int flags)

      hideSoftInputFromWindow(IBinder, int, ResultReceiver)的無返回值版:從窗口高低文中斷定當前接管輸入的窗口,隱蔽其輸入法窗口

    參數

    windowToken 由窗口懇求View.getWindowToken()返回獲得的令牌(token)。

    flags        供給額外的操縱標記。當前可認爲0或 HIDE_IMPLICIT_ONLY位設置。

    public boolean hideSoftInputFromWindow (IBinder windowToken, int flags, ResultReceiver resultReceiver)

    從窗口高低文中斷定當前接管輸入的窗口,請求隱蔽其軟鍵盤窗口。它可由用戶調用並獲得成果而不僅僅是顯式請求輸入法窗口隱蔽。

    參數

    windowToken 由窗口懇求View.getWindowToken()返回獲得的令牌(token)。

    flags        供給額外的操縱標記。當前可認爲0或 HIDE_IMPLICIT_ONLY位設置。

        resultReceiver  如不爲空,當IME處理懲罰懇求告訴你完成時調用。你收到的成果碼可所以RESULT_UNCHANGED_SHOWN, RESULT_UNCHANGED_HIDDEN, RESULT_SHOWN, 或RESULT_HIDDEN。

    public void hideStatusIcon (IBinder imeToken)

    (譯者注:隱蔽狀況欄圖標?)

    public boolean isAcceptingText ()

    當前辦事視圖接管全文編輯返回真。沒有輸入法聯接爲false,這時其只能處理懲罰原始按鍵事務。

    public boolean isActive (View view)

    視圖爲當前輸入的激活視圖時返回真。

    public boolean isActive ()

    輸入法中的隨便率性視圖激活時返回真。

    public boolean isFullscreenMode ()

    斷定相干輸入法是否以全屏模式運行。全屏時,完全覆蓋你的UI時,返回真,不然返回假。

    public boolean isWatchingCursor (View view)

    如當前輸入法要看到輸入編輯者的光標地位時返回真。

    public void restartInput (View view)

    如有輸入法聯接至視圖,重啓輸入以顯示新的內容。可在以下景象時調用此辦法:視圖的文字導致輸入法外觀變更或有按鍵輸入流,如應用法度調用TextView.setText()時。

             參數

    view        文字產生變更的視圖。

    public void sendAppPrivateCommand (View view, String action, Bundle data)

    對當前輸入法調用 InputMethodSession.appPrivateCommand()。

             參數

    view         可選的發送號令的視圖,如你要發送號令而不推敲視圖附加到輸入法,此項可認爲空。

    action      履行的號令名稱。必須是感化域的名稱,如前綴包名稱,如許不合的開闢者就不會創建衝突的號令。

    data         號令中包含的任何數據。

    public boolean setCurrentInputMethodSubtype (InputMethodSubtype subtype)

    (譯者注:此辦法爲3.0中新增的辦法)

    public void setInputMethod (IBinder token, String id)

    強迫切換到新輸入法部件。只能由持有token的應用法度(application)或辦事(service)調用當前激活輸入法。

             參數

    token       在輸入法啓動時供給令牌驗證,驗證後可對其進行操縱。

    id              切換到新輸入法的獨一標識。
    public void setInputMethodAndSubtype (IBinder token, String id, InputMethodSubtype subtype)

    強迫切換到一個新的輸入法和指定的類型。只能由持有token的應用法度(application)或辦事(service)調用當前激活輸入法。(譯者注:此辦法爲3.0中新增的辦法)

             參數

    token       在輸入法啓動時供給令牌驗證,驗證後可對其進行操縱。

    id              切換到新輸入法的獨一標識。

    subtype 切換到新輸入法的新類型。

    public void showInputMethodAndSubtypeEnabler (String topId)

    (譯者注:此辦法爲3.0中新增的辦法)

    public void showInputMethodPicker ()

    (譯者注:顯示輸入法菜單列表)

    public boolean showSoftInput (View view, int flags, ResultReceiver resultReceiver)

    如須要,顯式請求當前輸入法的軟鍵盤區域向用戶顯示。當用戶與視圖交互,用戶默示要開端履行輸入操縱時,可以調用此辦法。

             參數

    view         當前核心視圖,可接管軟鍵盤輸入。

    flags        供給額外的操縱標記。當前可所以0或SHOW_IMPLICIT 位設置。

    resultReceiver         如不爲空,當IME處理懲罰懇求告訴你完成時調用。你收到的成果碼可所以RESULT_UNCHANGED_SHOWN, RESULT_UNCHANGED_HIDDEN, RESULT_SHOWN, 或 RESULT_HIDDEN 。

    public boolean showSoftInput (View view, int flags)

showSoftInput(View, int, ResultReceiver)的無返回值版:如須要,顯式請求當前輸入法的軟鍵盤區域向用戶顯示。

             參數

    view         當前核心視圖,可接管軟鍵盤輸入。

    flags        供給額外的操縱標記。當前可所以0或SHOW_IMPLICIT 位設置。

    public void showSoftInputFromInputMethod (IBinder token, int flags)

    顯示輸入法的軟鍵盤區域,如許用戶可以到看到輸入法窗口並能與其交互。只能由當前激活輸入法調用,因需令牌(token)驗證。

             參數

    token       在輸入法啓動時供給令牌驗證,驗證後可對其進行操縱。

    flags        供給額外的操縱標記。可所以0或 SHOW_IMPLICIT, SHOW_FORCED位設置。

    public void showStatusIcon (IBinder imeToken, String packageName, int iconId)

    (譯者注:顯示狀況欄圖標?)

    public boolean switchToLastInputMethod (IBinder imeToken)

    public void toggleSoftInput (int showFlags, int hideFlags)

    (譯者注:切換軟鍵盤)

    public void toggleSoftInputFromWindow (IBinder windowToken, int showFlags, int hideFlags)

    本辦法切換輸入法的窗口顯示。如輸入窗口已顯示,它隱蔽。如無輸入窗口則顯示。

             參數

    windowToken  由窗口懇求View.getWindowToken()返回獲得的令牌(token)。

    showFlags       供給額外的操縱標記。當前可認爲0或 HIDE_IMPLICIT_ONLY位設置。

    hideFlags         供給額外的操縱標記。可所以0或 HIDE_IMPLICIT_ONLY, HIDE_NOT_ALWAYS位設置。

    public void Cursor (View view, int left, int top, int right, int bottom)

    返回窗口的當前光標地位。

    public void ExtractedText (View view, int token, ExtractedText text)

    (譯者注:當內容變更時文本編輯器調用此辦法,通知其新提取文本。)

    public void Selection (View view, int selStart, int selEnd, int candidatesStart, int candidatesEnd)

    返回當前選擇區域。

 

發佈了51 篇原創文章 · 獲贊 2 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章