布局中的一小技巧

在有的界面上我们要让用户输入一些东西,输完东西后,用户就要按"确定"按钮保存,或者按"取消"按钮取消操作,如下面一图中的操作:

 


      当用户点击上面的输入框时,这个时候,软键盘就会自动弹出来,用户输入完成后,那用户还得按一下返回键,用户才能看到“确定”和“取消”按键,这样用户就有点麻烦,如果用户在上面的输入入框中输入后,就想直接保存,而不用再按一下返回键。

      也就是当用户点击输入框的时候,键盘弹出来,同时下面的"确定"和"取消"这两个按钮也同时向上移动,停靠在软键盘之上。就像下面这张图片:

 


 

       这个怎么实现呢,看下面的图:


     按照上面图里面这样布局,那么当键盘弹出的时候,下面一块就都要向上抬起,停靠在键盘之上,在有的时候,这样比较方便用户输入完成后提交或取消。

 


 这个里面还必须要有一个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>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值