Android軟鍵盤的隱藏顯示

來源:互聯網
上載者:User
Android軟鍵盤的隱藏顯示對輸入框和布局的影響。 1. 平移模式:android:windowSoftInputMode="adjustPan" layout 檔案:
<com.hualu.softinput.RelativeLayoutResize xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context=".MainActivity" >    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentTop="true"        android:layout_marginTop="25dp"        android:text="@string/hello_world" />    <EditText        android:id="@+id/editText1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentBottom="true"        android:layout_centerHorizontal="true"        android:ems="10" >    </EditText></com.hualu.softinput.RelativeLayoutResize>

com.hualu.softinput.RelativeLayoutResize:

 

package com.hualu.softinput;import android.content.Context;import android.util.AttributeSet;import android.widget.RelativeLayout;public class RelativeLayoutResize extends RelativeLayout {private OnRelativeLayoutResizeListener listener ;public RelativeLayoutResize(Context context) {super(context);}public RelativeLayoutResize(Context context, AttributeSet attrs) {super(context, attrs) ;}@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {super.onMeasure(widthMeasureSpec, heightMeasureSpec);System.out.println("onMeasure() ---> width = " +  widthMeasureSpec + " , height = " + heightMeasureSpec);}@Overrideprotected void onLayout(boolean changed, int l, int t, int r, int b) {super.onLayout(changed, l, t, r, b);System.out.println("onLayout() -----> changed = " + changed + " , l = " + l + " , t = " + t + " , r = " + r + " , b = " + b );}@Overrideprotected void onSizeChanged(int w, int h, int oldw, int oldh) {super.onSizeChanged(w, h, oldw, oldh);System.out.println("onSizeChanged() ----> w = " + w + " , h = " + h + " , oldW = " + oldw + " , oldH = " + oldh);if(listener != null && h < oldh){listener.onResize() ;}}public void setOnRelativeLayoutResizeListener(OnRelativeLayoutResizeListener listener){this.listener = listener ;}public interface OnRelativeLayoutResizeListener{void onResize() ;}}

 

啟動應用的介面:

列印的Log:

IME起來:

Log資訊:

當介面改變布局時:

 

 

<com.hualu.softinput.RelativeLayoutResize xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context=".MainActivity" >    <EditText        android:id="@+id/editText1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentLeft="true"        android:layout_alignParentTop="true"        android:layout_marginLeft="20dp"        android:ems="10" >         <requestFocus />   </EditText>    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentBottom="true"        android:layout_alignParentLeft="true"        android:layout_marginBottom="64dp"        android:text="@string/hello_world" /> </com.hualu.softinput.RelativeLayoutResize>

啟動介面為:

 

Log資訊:

啟動IME之後:

Log資訊:

結論:

從上面log資訊,可以得出在平移模式下,啟動IME和退出IME都不會出發onSizeChange()方法。

2. 壓縮調整模式android:windowSoftInputMode="adjustResize"

 

當為藍色Layout時:

 

啟動介面:

 


Log資訊:


退出IME之後:


Log資訊:



當Layout為綠色的內容時:

啟動應用介面:

Log資訊:

退出IME之後:

Log資訊:

結論:

在壓縮模式下, IME的啟動和退出都會觸發onSizeChange()方法的調用。

3. 壓縮模式,監聽IME的顯示和隱藏:

因此,在壓縮模式下可以做到監聽IME的顯示與隱藏。

方法如com.hualu.softinput.RelativeLayoutResize中在onSizeChange()方法裡面自訂一個事件,監聽高度的變法:

 

if(listener != null && h < oldh){listener.onResize() ;}

在Activity中的調用是:

 

 

package com.hualu.softinput;import android.app.Activity;import android.os.Bundle;import android.view.Menu;import android.view.View;import android.widget.TextView;import com.hualu.softinput.RelativeLayoutResize.OnRelativeLayoutResizeListener;public class MainActivity extends Activity {private TextView text ;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);text = (TextView)this.findViewById(R.id.text) ;RelativeLayoutResize rlr = (RelativeLayoutResize)this.findViewById(R.id.contenter) ;rlr.setFocusableInTouchMode(true ) ;rlr.setOnRelativeLayoutResizeListener(new OnRelativeLayoutResizeListener() {@Overridepublic void onResize() {text.setVisibility(View.GONE) ;}}) ;}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.activity_main, menu);return true;}}

 









 

相關文章

聯繫我們

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