弹出键盘遮挡按钮的处理方法

来源:互联网 发布:网络隔离卡作用 编辑:程序博客网 时间:2024/05/01 21:28

在有的界面上我们要让用户输入一些东西,输完东西后,用户就要按”确定”按钮保存,或者按”取消”按钮取消操作,如下面一图中的操作:
这里写图片描述
当用户点击上面的输入框时,这个时候,软键盘就会自动弹出来,用户输入完成后,那用户还得按一下返回键,用户才能看到“确定”和“取消”按键,这样用户就有点麻烦,如果用户在上面的输入入框中输入后,就想直接保存,而不用再按一下返回键。
也就是当用户点击输入框的时候,键盘弹出来,同时下面的”确定”和”取消”这两个按钮也同时向上移动,停靠在软键盘之上。就像下面这张图片:
这里写图片描述
这个怎么实现呢,看下面的图:

这里写图片描述

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

这个里面还必须要有一个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="取消" />      </LinearL`yout>  </RelativeLayout>  

经过测试,最外面如果那么里面的那个“确定”和“取消”所在的布局必须在最下面要用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
原创粉丝点击