[英]Android. How to catch the moment when keyboard gets hidden?
我正在嘗試捕獲從屏幕上刪除鍵盤的事件,並且我正在使用以下代碼:
searchTextField.setOnEditorActionListener(new OnEditorActionListener()
{
public boolean onEditorAction(TextView v, int actionId, KeyEvent event)
{
System.out.println("ACTION ID " + actionId);
if(actionId == EditorInfo.IME_ACTION_DONE)
{
System.out.println("ACTION DONE!!!!!!!!!!");
return true;
}
return false;
}
searchTextField.setOnFocusChangeListener( new View.OnFocusChangeListener()
{
public void onFocusChange(View v, boolean hasFocus)
{
if (hasFocus)
System.out.println("HAS FOCUS");
else
System.out.println("FOCUS LOST");
}
});
但不幸的是,它不起作用。 onEditorAction
從未調用過,無論我是否開始編輯或完成。 關於onFocusChange
方法,只有在鍵盤上升時才第一次調用它。 當鍵盤下降或第二次上升時,不會被調用。 誰能解釋我在做什么錯?
我在活動rootView上使用GlobalLayoutListener來檢查鍵盤是隱藏還是可見:
其工作方式如下:
final View activityRootView = findViewById(R.id.activityRoot);
activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
int heightDiff = activityRootView.getRootView().getHeight() - activityRootView.getHeight();
if (heightDiff > 100) { // if more than 100 pixels, its probably a keyboard...
... do something here
}
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.