解决软键盘遮挡按钮

news/2024/7/5 1:43:52

原文链接:http://www.jianshu.com/p/49efa382352a#

前言

比如在进行登录的操作中,用户输入完密码之后,肯定是想直接点击登录按钮的。返回键隐藏软键盘这样的体验肯定很糟糕,程序员,遇到问题解决问题。


实现1

xml

<ScrollView  
    android:id="@+id/scrollview"  
    android:layout_width="match_parent"  
    android:layout_height="wrap_content"  
    android:fadingEdge="none"  
    android:scrollbars="none">  

    <LinearLayout  
        android:layout_width="match_parent"  
        android:layout_height="wrap_content"  
        android:orientation="vertical">  

        <ImageView  
            android:layout_width="100dp"  
            android:layout_height="100dp"  
            android:layout_gravity="center_horizontal"  
            android:layout_marginTop="20dp"  
            android:src="@mipmap/ic_loginhead"/>  

        <EditText  
            android:id="@+id/et_usernamelogin_username"  
            style="@style/customEditText"  
            android:layout_width="match_parent"  
            android:layout_height="40dp"  
            android:layout_marginTop="10dp"  
            android:background="@null"  
            android:hint="请输入已验证手机"  
            android:inputType="number"  
            android:lines="1"  
            android:maxLength="11"/>  

        <ImageView  
            android:layout_width="match_parent"  
            android:layout_height="2px"  
            android:layout_marginLeft="50dp"  
            android:layout_marginRight="50dp"  
            android:background="@color/pating_line"/>  

        <EditText  
            android:id="@+id/et_usernamelogin_password"  
            style="@style/customEditText"  
            android:layout_width="match_parent"  
            android:layout_height="40dp"  
            android:layout_marginTop="20dp"  
            android:background="@null"  
            android:digits="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_?"  
            android:hint="请输入密码"  
            android:inputType="textPassword"/>  

        <ImageView  
            android:layout_width="match_parent"  
            android:layout_height="2px"  
            android:layout_marginLeft="50dp"  
            android:layout_marginRight="50dp"  
            android:background="@color/pating_line"/>  

        <Button  
            android:id="@+id/btn_usernamelogin_dologin"  
            android:layout_width="match_parent"  
            android:layout_height="wrap_content"  
            android:layout_gravity="center_horizontal"  
            android:layout_marginLeft="50dp"  
            android:layout_marginRight="50dp"  
            android:layout_marginTop="30dp"  
            android:background="@drawable/btn_selecter"  
            android:enabled="false"  
            android:text="登录"  
            android:textColor="@color/white"  
            />  

    </LinearLayout>  
</ScrollView>

java

             mScrollView = (ScrollView) view.findViewById(R.id.scrollview);  
            usernamelogin_username.setOnTouchListener(new View.OnTouchListener() {  
                @Override  
                public boolean onTouch(View v, MotionEvent event) {  
                    changeScrollView();  

                    return false;  
                }  
            });  
            usernamelogin_password.setOnTouchListener(new View.OnTouchListener() {  
                @Override  
                public boolean onTouch(View v, MotionEvent event) {  
                    changeScrollView();  

                    return false;  
                }  
            });




  /** 
   * 使ScrollView指向底部 
   */  
      private void changeScrollView() {  
        new Handler().postDelayed(new Runnable() {  
          @Override  
          public void run() {  
            mScrollView.scrollTo(0, mScrollView.getHeight());  
          }  
      }, 300);  
    }

实现2

xml同上

anim下新建gone.xml

<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"               
  android:fromXScale="1.0"      
  android:toXScale="0.0"    
  android:fromYScale="1.0"  
  android:toYScale="0.0"    
  android:pivotX="50%"    
  android:pivotY="50%"    
  android:duration="500" 
  android:repeatCount="0"/>

visiable.xml

  <?xml version="1.0" encoding="utf-8"?>
  <scale xmlns:android="http://schemas.android.com/apk/res/android" 
  android:fromXScale="0.0"   
  android:toXScale="1.0"   
  android:fromYScale="0.0" 
  android:toYScale="1.0"    
  android:pivotX="50%"     
  android:pivotY="50%"    
  android:duration="500"     
  android:repeatCount="0"/>

或者直接在代码中

import android.os.Bundle;  
import android.os.Handler;  
import android.support.v7.app.AppCompatActivity;  
import android.view.KeyEvent;  
import android.view.MotionEvent;  
import android.view.View;  
import android.view.animation.Animation;  
import android.view.animation.AnimationSet;  
import android.view.animation.ScaleAnimation;  
import android.widget.Button;  
import android.widget.EditText;  
import android.widget.ImageView;  

public class MainActivity extends AppCompatActivity {  

private ImageView mHead;        //头部ImageView  

@Override  
protected void onCreate(Bundle savedInstanceState) {  
    super.onCreate(savedInstanceState);  
    setContentView(R.layout.activity_main);  
    mHead = (ImageView) findViewById(R.id.iv_head);  
    final Button btn= (Button) findViewById(R.id.btn_usernamelogin_dologin);  

    final EditText et_pass = (EditText) findViewById(R.id.et_usernamelogin_password);  
    final EditText et_name = (EditText) findViewById(R.id.et_usernamelogin_username);  


    /** 
     * 当输入被点击 
     */  
    et_name.setOnTouchListener(new View.OnTouchListener() {  
        @Override  
        public boolean onTouch(View v, MotionEvent event) {  

            start();  

            return false;  
        }  
    });  

    btn.setEnabled(false);  

    btn.setOnClickListener(new View.OnClickListener() {  
        @Override  
        public void onClick(View v) {  


        }  
    });  


}  

private void start() {  
    AnimationSet animationSet = new AnimationSet(true);  
    ScaleAnimation scaleAnimation = new ScaleAnimation(  
            1, 0.1f, 1, 0.1f,  
            Animation.RELATIVE_TO_SELF, 0.5f,  
            Animation.RELATIVE_TO_SELF, 0.5f);  
    scaleAnimation.setDuration(500);  
    animationSet.addAnimation(scaleAnimation);  
    animationSet.setFillAfter(true);  
    animationSet.setFillBefore(false);  
    animationSet.setRepeatCount(0);//设置重复次数  
    mHead.startAnimation(scaleAnimation);  
    new Handler().postDelayed(new Runnable() {  
        @Override  
        public void run() {  
            mHead.setVisibility(View.GONE);  
        }  
    }, 500);  
}  

/** 
 * 菜单、返回键响应 
 */  
@Override  
public boolean onKeyDown(int keyCode, KeyEvent event) {  
    // TODO Auto-generated method stub  
    if (keyCode == KeyEvent.KEYCODE_BACK) {  
        if(mHead.getVisibility()==View.GONE){  
            AnimationSet animationSet = new AnimationSet(true);  
            ScaleAnimation scaleAnimation = new ScaleAnimation(  
                    0.1f, 1f, 0.1f, 1f,  
                    Animation.RELATIVE_TO_SELF, 0.5f,  
                    Animation.RELATIVE_TO_SELF, 0.5f);  
            scaleAnimation.setDuration(500);  
            animationSet.addAnimation(scaleAnimation);  
            animationSet.setFillAfter(true);  
            animationSet.setFillBefore(false);  
            mHead.startAnimation(scaleAnimation);  

            mHead.setVisibility(View.VISIBLE);  

        }else {  
            finish();  
        }  

    }  
    return false;  
  }  
}

效果呢:




http://www.niftyadmin.cn/n/4581758.html

相关文章

i7-10700K和i7-9700KF哪个好

i7-10700K除了超线程回归外&#xff0c;基础频率和加速频率都提高了&#xff0c;后者更是原厂就能突破5.0GHz&#xff01;&#xff0c;三级缓存和内存支持频率也更高&#xff0c;当然不变的是祖传14nm&#xff0c;因此TDP水涨船高也是“理所当然”了。 选i7 10700K还是i7-9700K…

小米11和小米11pro什么时候上市

9月16日晚&#xff0c;小米集团副总裁常程为5nm处理器新品预热&#xff0c;表示概念到用户间的距离只有5nm。小米手机部总裁曾学忠强调5nm很重要&#xff0c;敬请期待。 小米手机爆降800这活动太给力了 http://www.xiaomi.com 其上一代小米10手机则发布于2020年2月13日。 小米…

Tint Drawable为图标着色

原文链接&#xff1a;http://www.race604.com/tint-drawable/ 其实在 Android Support V4 的包中提供了 DrawableCompat 类&#xff0c;我们很容易写出如下的辅助方法来实现 Drawable 的着色&#xff0c;如下&#xff1a; public static Drawable tintDrawable(Drawable drawab…

各种在线工具收集

C to arm:https://gcc.godbolt.org/https://bbs.pediy.com/user-578992.htm转载于:https://blog.51cto.com/haidragon/2337555

锐龙R7 Pro 4700G和R7 3700x 的区别 哪个好

锐龙7 4700G&#xff1a;8核心16线程&#xff0c;主频3.6-4.45GHz&#xff0c;Vega 8 GPU频率2100MHz&#xff0c;也非常准确&#xff0c;图形得分达4301&#xff0c;物理得分为23392。 选锐龙R7 Pro 4700G还是R7 3700x这些点很重要!看完你就知道了 https://list.jd.com/list.h…

让你的程序实现MaterialDesign风格

原文链接&#xff1a;http://android-developers.blogspot.sg/2014/10/implementing-material-design-in-your.html 材料设计 是一个全面的方法来可视化、交互和运动设计的多屏幕的世界。 Android 5.0棒棒糖和更新的支持库帮助您创建ui。 这里有一个破旧的一些材料设计和api的主…

R7 3700x配什么主板

AMD锐龙R7 3700X采用了目前最先进的7纳米工艺制程&#xff0c;采用依然基于AM4接口设计&#xff0c;这也是AMD良心的一面&#xff0c;方便锐龙平台老用户进行升级。 R7 3700x配什么主板看完你就知道了 https://list.jd.com/list.html? 拥有8核16线程&#xff0c;基础频率3.6GH…

R7 3700X和R7 2700X 的区别

AMD锐龙R7-3700X基于目前最为先进的台积电7纳米工艺的Zen2架构设计&#xff0c;核心代号为Matisse&#xff0c;接口类型依然为AM4&#xff0c;拥有8核16线程&#xff0c;基础频率为3.6GHz&#xff0c;加速频率为4.4GHz&#xff0c;拥有三级缓存高达32MB&#xff0c;4MB二级缓存…