【Android】解决软键盘遮挡登陆按钮

在应用登陆页面我们需要填写用户名和密码。当填写这些信息的时候,软键盘会遮挡登陆按钮,这使得用户体验较差,所以今天就来解决这个问题

1:登陆布局界面如下

[html]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:background="@drawable/login_bg" >  
  6.   
  7.     <LinearLayout  
  8.         android:id="@+id/ll_center"  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="wrap_content"       
  11.         android:orientation="vertical" >  
  12.   
  13.         <ScrollView  
  14.             android:id="@+id/sl_center"  
  15.             android:layout_width="fill_parent"  
  16.             android:layout_height="0dp"  
  17.             android:layout_weight="1"  
  18.             android:fadingEdge="none"  
  19.             android:scrollbars="none" >  
  20.   
  21.             <RelativeLayout  
  22.                 android:id="@+id/rl_center"  
  23.                 android:layout_width="fill_parent"  
  24.                 android:layout_height="wrap_content" >  
  25.   
  26.                 <LinearLayout  
  27.                     android:id="@+id/sms_login_ll_title"  
  28.                     android:layout_width="wrap_content"  
  29.                     android:layout_height="wrap_content"  
  30.                     android:layout_centerHorizontal="true"  
  31.                     android:layout_marginTop="90dip"  
  32.                     android:orientation="horizontal" >  
  33.   
  34.                     <ImageView  
  35.                         android:id="@+id/sms_login_iv_icon"  
  36.                         android:layout_width="70dip"  
  37.                         android:layout_height="70dip"  
  38.                         android:layout_gravity="center_vertical"  
  39.                         android:src="@drawable/login_top_icon" />  
  40.   
  41.                     <ImageView  
  42.                         android:id="@+id/sms_login_iv_big_text"  
  43.                         android:layout_width="wrap_content"  
  44.                         android:layout_height="wrap_content"  
  45.                         android:layout_gravity="center_vertical"  
  46.                         android:layout_marginLeft="15dip"  
  47.                         android:src="@drawable/sms_login_icon_big" />  
  48.                 </LinearLayout>  
  49.   
  50.                 <ImageView  
  51.                     android:id="@+id/sms_login_iv_name"  
  52.                     android:layout_width="wrap_content"  
  53.                     android:layout_height="wrap_content"  
  54.                     android:layout_below="@id/sms_login_ll_title"  
  55.                     android:layout_centerHorizontal="true"  
  56.                     android:layout_marginTop="28dip"  
  57.                     android:background="@drawable/sms_login_icon_small" />  
  58.   
  59.                 <RelativeLayout  
  60.                     android:id="@+id/sms_login_rl_input_name"  
  61.                     android:layout_width="fill_parent"  
  62.                     android:layout_height="43dip"  
  63.                     android:layout_below="@id/sms_login_iv_name"  
  64.                     android:layout_centerHorizontal="true"  
  65.                     android:layout_marginLeft="40dip"  
  66.                     android:layout_marginRight="40dip"  
  67.                     android:layout_marginTop="40dip"  
  68.                     android:background="@drawable/login_top_input" >  
  69.   
  70.                     <ImageView  
  71.                         android:id="@+id/sms_login_iv_input_name_icon"  
  72.                         android:layout_width="wrap_content"  
  73.                         android:layout_height="wrap_content"  
  74.                         android:layout_alignParentLeft="true"  
  75.                         android:layout_centerVertical="true"  
  76.                         android:layout_marginLeft="10dip"  
  77.                         android:layout_marginRight="10dip"  
  78.                         android:background="@drawable/login_input_icon_user" />  
  79.   
  80.                     <FrameLayout  
  81.                         android:layout_width="fill_parent"  
  82.                         android:layout_height="wrap_content"  
  83.                         android:layout_centerVertical="true"  
  84.                         android:layout_toRightOf="@id/sms_login_iv_input_name_icon" >  
  85.   
  86.                         <EditText  
  87.                             android:id="@+id/sms_login_et_accout"  
  88.                             android:layout_width="fill_parent"  
  89.                             android:layout_height="wrap_content"  
  90.                             android:background="@drawable/transparent_white"  
  91.                             android:digits="@string/sms_login_accout_text"  
  92.                             android:hint="请输入账号"  
  93.                             android:singleLine="true"  
  94.                             android:text=""  
  95.                             android:textSize="20sp" />  
  96.                     </FrameLayout>  
  97.                 </RelativeLayout>  
  98.   
  99.                 <RelativeLayout  
  100.                     android:id="@+id/sms_login_rl_input_pass"  
  101.                     android:layout_width="fill_parent"  
  102.                     android:layout_height="43dip"  
  103.                     android:layout_below="@id/sms_login_rl_input_name"  
  104.                     android:layout_centerHorizontal="true"  
  105.                     android:layout_marginLeft="40dip"  
  106.                     android:layout_marginRight="40dip"  
  107.                     android:background="@drawable/login_top_input" >  
  108.   
  109.                     <ImageView  
  110.                         android:id="@+id/sms_login_iv_input_pass_icon"  
  111.                         android:layout_width="wrap_content"  
  112.                         android:layout_height="wrap_content"  
  113.                         android:layout_alignParentLeft="true"  
  114.                         android:layout_centerVertical="true"  
  115.                         android:layout_marginLeft="10dip"  
  116.                         android:layout_marginRight="10dip"  
  117.                         android:background="@drawable/login_input_icon_pwd" />  
  118.   
  119.                     <FrameLayout  
  120.                         android:layout_width="fill_parent"  
  121.                         android:layout_height="wrap_content"  
  122.                         android:layout_centerVertical="true"  
  123.                         android:layout_toRightOf="@id/sms_login_iv_input_pass_icon" >  
  124.   
  125.                         <EditText  
  126.                             android:id="@+id/sms_login_et_password"  
  127.                             android:layout_width="fill_parent"  
  128.                             android:layout_height="wrap_content"  
  129.                             android:background="@drawable/transparent_white"  
  130.                             android:digits="@string/sms_et_change_password_old_text"  
  131.                             android:hint="请输入密码"  
  132.                             android:inputType="textPassword"  
  133.                             android:singleLine="true"  
  134.                             android:text=""  
  135.                             android:textSize="20sp" />  
  136.                     </FrameLayout>  
  137.                 </RelativeLayout>  
  138.             </RelativeLayout>  
  139.         </ScrollView>  
  140.   
  141.         <Button  
  142.             android:id="@+id/sms_login_bt_confirm"  
  143.             android:layout_width="fill_parent"  
  144.             android:layout_height="wrap_content"  
  145.             android:layout_below="@id/sms_login_rl_input_pass"  
  146.             android:layout_centerHorizontal="true"  
  147.             android:layout_marginLeft="40dip"  
  148.             android:layout_marginRight="40dip"  
  149.             android:layout_marginTop="16dip"  
  150.             android:background="@drawable/sms_update_pass_bg_selector"  
  151.             android:text="登 录"  
  152.             android:textColor="@color/white"  
  153.             android:textSize="20sp" />  
  154.     </LinearLayout>  
  155.    
  156. </RelativeLayout>  

需要注意的是:

1:层级关系

RelativeLayout-----

    LinearLayout----

     ScrollView,  

     Button

2:在AndroidManifest.xml中的该activity配置  android:windowSoftInputMode="stateHidden|adjustResize"

3:看如下代码

[java]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. etAccount = (EditText) this.findViewById(R.id.sms_login_et_accout);  
  2.         etAccount.setOnClickListener(this);  
  3.         etAccount.setOnTouchListener(new OnTouchListener() {  
  4.             @Override  
  5.             public boolean onTouch(View v, MotionEvent event) {  
  6.                 changeScrollView();   
  7.                 return false;  
  8.             }  
  9.         });  
  10.     /** 
  11.      * 使ScrollView指向底部 
  12.      */  
  13.     private void changeScrollView(){  
  14.         h.postDelayed(new Runnable() {  
  15.             @Override  
  16.             public void run() {  
  17.                 sl_center.scrollTo(0, sl_center.getHeight());   
  18.             }  
  19.         }, 300);  
  20.     }  
  21.       
  22.     Handler h = new Handler(){  
  23.         public void handleMessage(Message msg) {  
  24.         };  
  25.     };  

4:具体问题具体分析,方法有多种,欢迎大家讨论。


//----------------------------------------------------------------------------------------------------------------

etAccount = (EditText) this.findViewById(R.id.sms_login_et_accout);

实践过程中要注意,上面这段让ScrollView滚动的代码很关键。

http://blog.csdn.net/yigelangmandeshiren/article/details/36185181

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值