Android強制彈出,隱藏IME.

來源:互聯網
上載者:User

標籤:des   android   style   class   blog   c   

當我們彈出一個Dialog時候,如果這個Dialog需要輸入資料,然後確定後又需要關閉IME,一般系統的hide,跟show方法總會有各種問題,最霸道的解決方案就是寫一個定時器,定時彈出或者關閉IME。

import java.util.Timer;import java.util.TimerTask;import android.content.Context;import android.view.View;import android.view.inputmethod.InputMethodManager;import android.widget.EditText;public class InputTools {    //隱藏虛擬鍵盤      public static void HideKeyboard(View v)      {          InputMethodManager imm = ( InputMethodManager ) v.getContext( ).getSystemService( Context.INPUT_METHOD_SERVICE );             if ( imm.isActive( ) ) {                 imm.hideSoftInputFromWindow( v.getApplicationWindowToken( ) , 0 );                       }          }            //顯示虛擬鍵盤      public static void ShowKeyboard(View v)      {          InputMethodManager imm = ( InputMethodManager ) v.getContext( ).getSystemService( Context.INPUT_METHOD_SERVICE );                     imm.showSoftInput(v,InputMethodManager.SHOW_FORCED);              }            //強制顯示或者關閉系統鍵盤      public static void KeyBoard(final EditText txtSearchKey,final String status)      {                     Timer timer = new Timer();        timer.schedule(new TimerTask(){        @Override        public void run()        {            InputMethodManager m = (InputMethodManager)            txtSearchKey.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);             if(status.equals("open"))             {                 m.showSoftInput(txtSearchKey,InputMethodManager.SHOW_FORCED);              }             else             {                 m.hideSoftInputFromWindow(txtSearchKey.getWindowToken(), 0);              }         }           }, 300);       }            //通過定時器強制隱藏虛擬鍵盤      public static void TimerHideKeyboard(final View v)      {          Timer timer = new Timer();        timer.schedule(new TimerTask(){        @Override        public void run()        {            InputMethodManager imm = ( InputMethodManager ) v.getContext( ).getSystemService( Context.INPUT_METHOD_SERVICE );                 if ( imm.isActive( ) )            {                     imm.hideSoftInputFromWindow( v.getApplicationWindowToken( ) , 0 );            }             }          }, 10);       }      //IME是否顯示著      public static boolean KeyBoard(EditText edittext)      {          boolean bool = false;          InputMethodManager imm = ( InputMethodManager ) edittext.getContext( ).getSystemService( Context.INPUT_METHOD_SERVICE );             if ( imm.isActive( ) )        {                bool = true;         }            return bool;                }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.