<Android>監聽軟鍵盤開啟收合事件(軟鍵盤內建收合按鈕)

來源:互聯網
上載者:User

標籤:android   edittext   

最近在公司開發cocos2dx上的android輸入框控制項,遇到軟鍵盤的事件監聽,通常軟鍵盤的收合方式大致3種:

1.點擊軟鍵盤右下角的Return按鈕(系統收合)

2.輸入框焦點時按返回按鈕(系統收合)

3.點擊軟鍵盤和輸入框的外部(自發收合)

4.點擊軟鍵盤內建的收合按鈕(軟鍵盤收合)


前三種事件可以監聽,方式都比較簡單

1.點擊軟鍵盤右下角的Return按鈕

給輸入框設定監聽

editText.setOnEditorActionListener(new OnEditorActionListener() {
@Override
public boolean onEditorAction(final TextView v, final int actionId, final KeyEvent event) {
// 可捕捉右下角的Return按鈕

//添加拋出收合事件代碼
return false;
}
});


2.輸入框焦點時按返回按鈕

給輸入框增加按鈕監聽

editText.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
if (listener != null) {
// 可添加拋出收合事件代碼
}
return true;
}
return false;
}
});


3.點擊軟鍵盤和輸入框的外部

給輸入框的父容器增加觸摸監聽

@Override
public boolean onTouchEvent(MotionEvent event) {

if (indexOfChild(editText) > -1) {

// 可添加拋出收合事件代碼

}

return super.onTouchEvent(event);

}


4.點擊軟鍵盤內建的收合按鈕(軟鍵盤收合)

問題卡在此處,經過資料的搜查,還是麼能找到軟鍵盤收合按鈕事件監聽的辦法,最後在stackoverflow網站找到了從布局高度的變化來判斷軟鍵盤的開啟和收合事件。

import android.graphics.Rect;import android.view.View;import android.view.ViewTreeObserver;import java.util.LinkedList;import java.util.List;public class SoftKeyboardStateHelper implements ViewTreeObserver.OnGlobalLayoutListener {    public interface SoftKeyboardStateListener {        void onSoftKeyboardOpened(int keyboardHeightInPx);        void onSoftKeyboardClosed();    }    private final List<SoftKeyboardStateListener> listeners = new LinkedList<SoftKeyboardStateListener>();    private final View activityRootView;    private int        lastSoftKeyboardHeightInPx;    private boolean    isSoftKeyboardOpened;    public SoftKeyboardStateHelper(View activityRootView) {        this(activityRootView, false);    }    public SoftKeyboardStateHelper(View activityRootView, boolean isSoftKeyboardOpened) {        this.activityRootView     = activityRootView;        this.isSoftKeyboardOpened = isSoftKeyboardOpened;        activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(this);    }    @Override    public void onGlobalLayout() {        final Rect r = new Rect();        //r will be populated with the coordinates of your view that area still visible.        activityRootView.getWindowVisibleDisplayFrame(r);        final int heightDiff = activityRootView.getRootView().getHeight() - (r.bottom - r.top);        if (!isSoftKeyboardOpened && heightDiff > 100) { // if more than 100 pixels, its probably a keyboard...            isSoftKeyboardOpened = true;            notifyOnSoftKeyboardOpened(heightDiff);        } else if (isSoftKeyboardOpened && heightDiff < 100) {            isSoftKeyboardOpened = false;            notifyOnSoftKeyboardClosed();        }    }    public void setIsSoftKeyboardOpened(boolean isSoftKeyboardOpened) {        this.isSoftKeyboardOpened = isSoftKeyboardOpened;    }    public boolean isSoftKeyboardOpened() {        return isSoftKeyboardOpened;    }    /**     * Default value is zero (0)     * @return last saved keyboard height in px     */    public int getLastSoftKeyboardHeightInPx() {        return lastSoftKeyboardHeightInPx;    }    public void addSoftKeyboardStateListener(SoftKeyboardStateListener listener) {        listeners.add(listener);    }    public void removeSoftKeyboardStateListener(SoftKeyboardStateListener listener) {        listeners.remove(listener);    }    private void notifyOnSoftKeyboardOpened(int keyboardHeightInPx) {        this.lastSoftKeyboardHeightInPx = keyboardHeightInPx;        for (SoftKeyboardStateListener listener : listeners) {            if (listener != null) {                listener.onSoftKeyboardOpened(keyboardHeightInPx);            }        }    }    private void notifyOnSoftKeyboardClosed() {        for (SoftKeyboardStateListener listener : listeners) {            if (listener != null) {                listener.onSoftKeyboardClosed();            }        }    }}

代碼的使用:

final SoftKeyboardStateHelper softKeyboardStateHelper = new SoftKeyboardStateHelper(findViewById(R.id.activity_main_layout);softKeyboardStateHelper.addSoftKeyboardStateListener(...);// then just handle callbacks


地址連結:http://stackoverflow.com/questions/2150078/how-to-check-visibility-of-software-keyboard-in-android

從中選取了這段代碼。

這樣能一併解決以上4種情況,比較好的監聽軟鍵盤的開啟和收合事件,目前正在使用中。


<Android>監聽軟鍵盤開啟收合事件(軟鍵盤內建收合按鈕)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.