软键盘覆盖edittext 使用ScrollView的详解

本文解决了一个问题,在Android应用中,当软键盘弹出时如何确保底部的输入框始终可见而不被覆盖。通过修改XML布局文件和设置软键盘的行为模式,实现了无论点击哪个输入框,软键盘都不会遮挡底部输入框。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原贴地址

 http://www.eoeandroid.com/thread-53414-1-1.html

 

 

我的布局是这样的, 屏幕最上方 和屏幕最下方 各有一个edittext  (输入框) ,我点击 屏幕下方的输入框 软键盘出来后 可以把最下面的输入框顶上去 ,但是我点击屏幕上方的输入框  软键盘不会把最下方的顶上去而是覆盖了最下方的输入框。我想要的是不管我点击上面的输入框 还是下面的输入框  出现软键盘后都会把 最下方的输入框顶上去、。 下面是我的xml 高手速来啊,,小弟在线等啊。。。55555555


< ?xml version="1.0" encoding="utf-8"?>

< RelativeLayout  xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
< ScrollView xmlns:android="http://schemas.android.com/apk/res/android"   
     android:layout_width="fill_parent"   
     android:layout_height="fill_parent">
 
<RelativeLayout  xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >    
<Button android:id="@+id/edit_button2"
    android:layout_width="wrap_content"
    android:layout_height="100px"
    android:gravity="center"
    android:layout_marginTop="370dip"
    android:text="添加"
/>
<EditText android:id="@+id/edit_text1"
    android:layout_width="200px"
    android:layout_height="100px"
     android:gravity = "top"
    android:layout_toRightOf="@id/edit_button2"
    android:hint="请输入信息内容"
    android:layout_marginTop="370dip"
/>
  <Button android:id="@+id/edit_button"
    android:layout_width="wrap_content"
    android:layout_height="100px"
    android:layout_marginTop="370dip"
    android:gravity="center"
    android:layout_toRightOf="@id/edit_text1"
    android:text="发送"
/>
  </RelativeLayout>
 
</ScrollView>
  <EditText android:id="@+id/edit_text"
    android:layout_width="260px"
    android:layout_height="wrap_content"
/>
<Button android:id="@+id/edit_button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@id/edit_text"
    android:text="编辑"
/>
</RelativeLayout>

 

 

 

 

 

 

 

改了下你的布局:

 

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

        <RelativeLayout 
                android:layout_width="fill_parent" android:layout_height="wrap_content"
 
                android:layout_alignParentBottom="true">
 
                
                <Button android:id="@+id/edit_button2" android:layout_width="wrap_content"
 
                        android:layout_height="100px" android:gravity="center"
 
                         android:text="添加" />
 
                <EditText android:id="@+id/edit_text1" android:layout_width="200px"
 
                        android:layout_height="100px" android:gravity="top"
 
                        android:layout_toRightOf="@id/edit_button2" android:hint="请输入信息内容"
 
                        />
 
                <Button android:id="@+id/edit_button" android:layout_width="wrap_content"
 
                        android:layout_height="100px" 
                        android:gravity="center" android:layout_toRightOf="@id/edit_text1"
 
                        android:text="发送" />
 
        </RelativeLayout>
 

        <EditText android:id="@+id/edit_text" android:layout_width="260px"
 
                android:layout_height="wrap_content" />
 
        <Button android:id="@+id/edit_button1" android:layout_width="wrap_content"
 
                android:layout_height="wrap_content" android:layout_toRightOf="@id/edit_text"
 
                android:text="编辑" />
 
</RelativeLayout>
然后在setconentView后面加这句话:
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE |
                WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN); 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值