佈局中的一小技巧

在有的界面上我們要讓用戶輸入一些東西,輸完東西后,用戶就要按"確定"按鈕保存,或者按"取消"按鈕取消操作,如下面一圖中的操作:

 


      當用戶點擊上面的輸入框時,這個時候,軟鍵盤就會自動彈出來,用戶輸入完成後,那用戶還得按一下返回鍵,用戶才能看到“確定”和“取消”按鍵,這樣用戶就有點麻煩,如果用戶在上面的輸入入框中輸入後,就想直接保存,而不用再按一下返回鍵。

      也就是當用戶點擊輸入框的時候,鍵盤彈出來,同時下面的"確定"和"取消"這兩個按鈕也同時向上移動,停靠在軟鍵盤之上。就像下面這張圖片:

 


 

       這個怎麼實現呢,看下面的圖:


     按照上面圖裏面這樣佈局,那麼當鍵盤彈出的時候,下面一塊就都要向上擡起,停靠在鍵盤之上,在有的時候,這樣比較方便用戶輸入完成後提交或取消。

 


 這個裏面還必須要有一個ScollView,或者ListView,ExtanspabeListView等的一個組件,之間沒有說,現在補上,並附代碼:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <EditText 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:inputType="text"/>
    
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="0dp">
    </ScrollView>

    <LinearLayout
        style="@android:style/ButtonBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:orientation="horizontal" >

        <Button
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="確定" />

        <Button
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="取消" />
    </LinearLayout>

</RelativeLayout>




經過測試,最外面如果<LinearLayout>那麼裏面的那個“確定”和“取消”所在的佈局必須在最下面要用weight屬性,並且也要有ListView或ScollView如下面的代碼,這個也要ScollView等開始的位置在彈出的鍵盤的上面開始,這個設置不好容易出錯,最好的就是上面的方法:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <EditText 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:inputType="text"/>
    
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">
    </ScrollView>

    <LinearLayout
        style="@android:style/ButtonBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="確定" />

        <Button
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="取消" />
    </LinearLayout>

</LinearLayout>


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章