天天看点

Android软键盘强制弹出,隐藏输入法.

http://blog.csdn.net/kdsde/article/details/31397583

当我们弹出一个dialog时候,如果这个dialog需要输入数据,然后确定后又需要关闭输入法,一般系统的hide,跟show方法总会有各种问题,最霸道的解决方法就是写一个定时器,定时弹出或者关闭输入法。

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)

        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)

            inputmethodmanager imm = ( inputmethodmanager ) v.getcontext( ).getsystemservice( context.input_method_service );     

            if ( imm.isactive( ) )

            {     

                imm.hidesoftinputfromwindow( v.getapplicationwindowtoken( ) , 0 );

            }    

        }, 10);

      //输入法是否显示着

      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;

}

继续阅读