下面是:
1:
2:
3:(訊息框下面的部分將不再顯示)
android目前還沒有直接對軟鍵盤事件進行直接監聽的方法,實現這樣的效果需要自己寫一個布局類繼承LinearLayout,並重寫onSizeChanged方法。
注意:
不能直接在OnResizeListener中對要改變的View變更,因為OnSizeChanged函數實際上是運行在View的layout方法中,如果直接在onSizeChange中改變view的顯示內容,那麼很可能需要重新調用layout方法才能顯示正確。然而我們的方法又是在layout中調用的,因此會出現錯誤。因此我們在例子中採用了Handler的方法。
附:代碼package cn.itcast.test.ui;<br />import cn.itcast.test.chat.R;<br />import android.content.Context;<br />import android.os.Handler;<br />import android.util.AttributeSet;<br />import android.util.Log;<br />import android.view.View;<br />import android.widget.LinearLayout;<br />public class ResizeLayout extends LinearLayout {<br />int count = 0;<br />int count1 = 0;<br />int count2 = 0;<br />//定義預設的軟鍵盤最小高度,這是為了避免onSizeChanged在某些下特殊情況下出現的問題。<br />private static final int SOFTKEYPAD_MIN_HEIGHT = 50;<br />private Handler uiHandler = new Handler();<br />private static final String TAG = "ResizeLayout";<br />public ResizeLayout(Context context) {<br />super(context);<br />}<br />public ResizeLayout(Context context, AttributeSet attrs) {<br />super(context, attrs);<br />}<br />@Override<br />protected void onSizeChanged(int w, final int h, int oldw, final int oldh) {<br />super.onSizeChanged(w, h, oldw, oldh);<br />Log.i(TAG, "onSizeChanged " + count++ + "=>onResize called! w=" + w + ",h=" + h + ",oldw=" + oldw + ",oldh=" + oldh);<br />uiHandler.post(new Runnable() {<br />@Override<br />public void run() {<br />if (oldh - h > SOFTKEYPAD_MIN_HEIGHT)<br />// 必須設定為View.GONE不佔空間<br />findViewById(R.id.talk_panel).setVisibility(View.GONE);<br />else<br />findViewById(R.id.talk_panel).setVisibility(View.VISIBLE);<br />}<br />});<br />}<br />@Override<br />protected void onLayout(boolean changed, int l, int t, int r, int b) {<br />super.onLayout(changed, l, t, r, b);<br />Log.e(TAG, "onLayout " + count1++ + "=>OnLayout called! l=" + l + ", t=" + t + ",r=" + r + ",b=" + b);<br />}<br />@Override<br />protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {<br />super.onMeasure(widthMeasureSpec, heightMeasureSpec);<br />Log.e(TAG, "onMeasure " + count2++ + "=>onMeasure called! widthMeasureSpec=" + widthMeasureSpec + ", heightMeasureSpec="<br />+ heightMeasureSpec);<br />}<br />}<br />
csdn似乎不能上傳附件。
源碼:
http://www.eoeandroid.com/forum.php?mod=viewthread&tid=74595&page=1&extra=#pid783682
推薦給大家一篇博文:
關於Android軟鍵盤的隱藏顯示研究:http://winuxxan.blog.51cto.com/2779763/522810