Android-UI詳解:軟鍵盤彈出後,動態改變UI布局(IM聊天視窗設計樣本)

來源:互聯網
上載者:User

下面是:

 

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

 

 

 

相關文章

聯繫我們

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