码迷,mamicode.com
首页 > 移动开发 > 详细

Android 监听键盘弹出和收起.

时间:2016-04-27 01:41:46      阅读:258      评论:0      收藏:0      [点我收藏+]

标签:

entends:http://stackoverflow.com/questions/36837066/how-to-validate-virtual-keyboard-visibility

监听键盘弹出和收起.

 

/*
Somewhere else in your code
*/

RelativeLayout mainLayout = findViewById(R.layout.main_layout); // You must use your root layout
InputMethodManager im = (InputMethodManager) getSystemService(Service.INPUT_METHOD_SERVICE);     

/*
Instantiate and pass a callback
*/

SoftKeyboard softKeyboard;
softKeyboard = new SoftKeyboard(mainLayout, im);
softKeyboard.setSoftKeyboardCallback(new SoftKeyboard.SoftKeyboardChanged()
{
 @Override  
  public void onSoftKeyboardHide()   
  {
        // Code here  
  }  

  @Override 
   public void onSoftKeyboardShow()  
   { 
       // Code here 
   }   
});     

/*
Open or close the soft keyboard programatically
*/

softKeyboard.openSoftKeyboard();
softKeyboard.closeSoftKeyboard();


/*
SoftKeyboard can catch keyboard events when an EditText gains focus and keyboard appears
*/


/* Prevent memory leaks:
*/

@Override
public void onDestroy()
{  
  super.onDestroy(); 
   softKeyboard.unRegisterSoftKeyboardCallback();
}

 

 

final View parentView= findViewById(R.id.myrootview);  
       parentView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            int heightDiff = root.getRootView().getHeight() - root.getHeight();

            Rect rectgle= new Rect();
            Window window= getWindow();
            window.getDecorView().getWindowVisibleDisplayFrame(rectgle);
            int contentViewTop= 
                window.findViewById(Window.ID_ANDROID_CONTENT).getTop();

            if(heightDiff <= contentViewTop){
                //Soft KeyBoard Hidden---button visible

            }else{
                //Soft KeyBoard Shown---button hide
            }


         }
    });

 

Android 监听键盘弹出和收起.

标签:

原文地址:http://www.cnblogs.com/niray/p/5437320.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!