99.android 解决登录页面软键盘遮挡控件,父布局监听软键盘,布局上升下降

导读:本篇文章讲解 99.android 解决登录页面软键盘遮挡控件,父布局监听软键盘,布局上升下降,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

99.android 解决登录页面软键盘遮挡控件,父布局监听软键盘,布局上升下降

 

99.android 解决登录页面软键盘遮挡控件,父布局监听软键盘,布局上升下降

//第一步 布局: 

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mLoginLayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/white">

    <LinearLayout
        android:id="@+id/loginLogo"
        android:layout_width="152.5dp"
        android:layout_height="105.5dp"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="100dp"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/loginLogoImg"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center_horizontal"
            android:src="@mipmap/ic_launcher" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/login_account_layout"
        android:layout_width="fill_parent"
        android:layout_height="55dp"
        android:layout_below="@+id/loginLogo"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="50dp"
        android:gravity="center"
        android:orientation="vertical">

        <EditText
            android:id="@+id/loginAccount"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_marginLeft="5dp"
            android:background="#FFFF"
            android:gravity="center_vertical"
            android:hint="账号/用户名"
            android:singleLine="true"
            android:textColor="#ea6262"
            android:textColorHint="#d78989"
            android:textSize="15sp" />

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="#484747" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/loginAccountPwd"
        android:layout_width="fill_parent"
        android:layout_height="55dp"
        android:layout_below="@+id/login_account_layout"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:gravity="center">

        <EditText
            android:id="@+id/loginPwd"
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_weight="1"
            android:background="#FFFF"
            android:gravity="center_vertical"
            android:hint="密码"
            android:inputType="textPassword"
            android:singleLine="true"
            android:textColor="#ea6262"
            android:textColorHint="#d78989"
            android:textSize="15sp" />

        <LinearLayout
            android:id="@+id/loginShowPwd"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:clickable="true"
            android:gravity="center">

            <ImageView
                android:id="@+id/loginTextClear"
                android:layout_width="25dp"
                android:layout_height="25dp"
                android:layout_marginRight="10dp"
                android:src="@drawable/ic_display" />
        </LinearLayout>
    </LinearLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="0.5dp"
        android:layout_below="@+id/loginAccountPwd"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:background="#484747" />

    <RelativeLayout
        android:id="@+id/mLogin"
        android:layout_width="fill_parent"
        android:layout_height="42dp"
        android:layout_below="@+id/loginAccountPwd"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="30dp"
        android:background="#7e3b76ec">

        <TextView
            android:id="@+id/loginBtn"
            android:layout_width="wrap_content"
            android:layout_height="42dp"
            android:layout_centerInParent="true"
            android:background="@null"
            android:gravity="center"
            android:text="登录"
            android:textColor="#fff"
            android:textSize="15sp" />

        <ImageView
            android:visibility="gone"
            android:layout_marginRight="10dp"
            android:id="@+id/mLoginLoad"
            android:layout_toLeftOf="@+id/loginBtn"
            android:layout_centerVertical="true"
            android:src="@drawable/ic_load"
            android:layout_width="20dp"
            android:layout_height="20dp" />


    </RelativeLayout>


</RelativeLayout>

//第二步 Activity逻辑代码:

public class Main9Activity extends AppCompatActivity implements View.OnClickListener {

    private ImageView loginLogoImg;
    private LinearLayout loginLogo;
    private EditText loginAccount;
    private LinearLayout login_account_layout;
    private EditText loginPwd;
    private ImageView loginTextClear;
    private LinearLayout loginShowPwd;
    private LinearLayout loginAccountPwd;
    private TextView loginBtn;
    private RelativeLayout mLogin;
    private RelativeLayout mLoginLayout;
    private ImageView mLoginLoad;
    private Animation animation;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main9);
        initView();//初始化控件
        loginLayoutListener();//父布局监听软键盘,布局上升下降
        editPwdListener();//EditText的监听
    }

    private void initView() {
        loginLogoImg = (ImageView) findViewById(R.id.loginLogoImg);
        loginLogo = (LinearLayout) findViewById(R.id.loginLogo);
        loginAccount = (EditText) findViewById(R.id.loginAccount);
        login_account_layout = (LinearLayout) findViewById(R.id.login_account_layout);
        loginPwd = (EditText) findViewById(R.id.loginPwd);
        loginTextClear = (ImageView) findViewById(R.id.loginTextClear);
        loginShowPwd = (LinearLayout) findViewById(R.id.loginShowPwd);
        loginAccountPwd = (LinearLayout) findViewById(R.id.loginAccountPwd);
        loginBtn = (TextView) findViewById(R.id.loginBtn);
        mLogin = (RelativeLayout) findViewById(R.id.mLogin);
        mLoginLayout = (RelativeLayout) findViewById(R.id.mLoginLayout);
        mLoginLoad = (ImageView) findViewById(R.id.mLoginLoad);
        loginShowPwd.setOnClickListener(this);
        mLogin.setOnClickListener(this);

        //登录按钮动画
        animation = AnimationUtils.loadAnimation(this, R.anim.rotate_anim);
        LinearInterpolator lin = new LinearInterpolator();//设置动画匀速运动
        animation.setInterpolator(lin);
    }

    /*
     * 方法名:loginLayoutListener()
     * 功    能:父布局监听软键盘,布局上升下降
     * 参    数:无
     * 返回值:无
     */
    private void loginLayoutListener() {
        // 注册一个回调函数,当在一个视图树中全局布局发生改变或者视图树中的某个视图的可视状态发生改变时调用这个回调函数。
        mLoginLayout.getViewTreeObserver().addOnGlobalLayoutListener(
                new ViewTreeObserver.OnGlobalLayoutListener() {
                    @Override
                    public void onGlobalLayout() {
                        mLoginLayout.scrollTo(0, 0);//根布局视图恢复,必须先恢复一下,因为有时候账号框和密码框,弹出的键盘高度不一致,会出现BUG
                        Rect rect = new Rect();
                        mLoginLayout.getWindowVisibleDisplayFrame(rect);// 获取根布局在窗体的可视区域
                        int mainInvisibleHeight = mLoginLayout.getRootView().getHeight() - rect.bottom;//当前视图最外层的高度,减去现在所看到的屏幕视图的最底部的y坐标,就是软键盘的高度(如果没有虚拟按键的情况下),如果有虚拟按键就是软键盘的高度+虚拟按键的高度
                        //设置150,因为有虚拟按键的话虚拟按键的高度也会算到mainInvisibleHeight里
                        //如果没有虚拟按键,在软键盘没有弹出的情况下mainInvisibleHeight为0,如果有虚拟按键,mainInvisibleHeight永远都不可能为0,设置150既可避开虚拟按键的高度,又可监听软键盘是否弹出
                        if (mainInvisibleHeight > 150) {//若mainInvisibleHeight高度大于150,则说明当前视图上移了,说明软键盘弹出了
                            int[] location = new int[2];
                            loginBtn.getLocationInWindow(location);//获取loginBtn(登录按钮)的窗体坐标,需要显示的最下方View
                            int srollHeight = (location[1] + loginBtn.getHeight() + 20) - rect.bottom;//location[1]是loginBtn(登录按钮)所在y轴下标,加上登录按钮高度+20,减去此时屏幕可见的底部y轴下标,等于需要根布局滚动的高度
                            mLoginLayout.scrollTo(0, srollHeight);//根布局视图滚动至srollHeight的高度
                        } else {//小于150键盘隐藏的情况下
                            mLoginLayout.scrollTo(0, 0);//根布局视图恢复
                        }
                    }
                });
    }

    /*
     * 方法名:editPwdListener()
     * 功    能:EditText的监听,改变登录按钮的颜色
     * 参    数:无
     * 返回值:无
     */
    private void editPwdListener() {
        if (!loginPwd.getText().toString().isEmpty()) {
            mLogin.setBackgroundColor(Color.parseColor("#3b76ec"));
        } else {
            mLogin.setBackgroundColor(Color.parseColor("#7e3b76ec"));
        }
        //EditText的监听
        loginPwd.addTextChangedListener(watcher);
    }

    /*
     * 方法名:TextWatcher()
     * 功    能:监听输入框软键盘 变颜色
     * 参    数:无
     * 返回值:TextWatcher watcher
     */
    private TextWatcher watcher = new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // TODO Auto-generated method stub

        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                                      int after) {
            // TODO Auto-generated method stub
        }

        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub
            if (s.length() > 0) {
                mLogin.setBackgroundColor(Color.parseColor("#3b76ec"));//深色
            } else {
                mLogin.setBackgroundColor(Color.parseColor("#7e3b76ec"));//浅色
            }
        }
    };

    /*
     * 方法名:runOnUiOpen()
     * 功    能:开启登录动画,UI操作
     * 参    数:无
     * 返回值:无
     */
    private void runOnUiOpen() {
        //动画
        mLoginLoad.setVisibility(View.VISIBLE);
        mLoginLoad.startAnimation(animation);
        mLogin.setBackgroundColor(Color.parseColor("#7e3b76ec"));//浅色
        mLogin.setEnabled(false);
        loginShowPwd.setEnabled(false);
        closeKeyboard();
        mLoginLayout.scrollTo(0, 0);
    }

    /*
     * 方法名:runOnUiClose()
     * 功    能:关闭登录动画,UI操作
     * 参    数:无
     * 返回值:无
     */
    private void runOnUiClose() {
        mLoginLoad.clearAnimation();
        mLogin.setBackgroundColor(Color.parseColor("#3b76ec"));//深色
        mLogin.setEnabled(true);
        loginShowPwd.setEnabled(true);
        mLoginLoad.setVisibility(View.GONE);
    }

    /*
     * 方法名:closeKeyboard()
     * 功    能:关闭软键盘
     * 参    数:无
     * 返回值:无
     */
    private void closeKeyboard() {
        View view = getWindow().peekDecorView();
        if (view != null) {
            InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
        }
    }

    /*
     * 方法名:setShowPwd()
     * 功    能:点击显示号码,光标显示在结尾
     * 参    数:无
     * 返回值:无
     */
    private void setShowPwd() {
        //点击显示号码
        if (!loginPwd.getTransformationMethod().equals(HideReturnsTransformationMethod.getInstance())) {
            loginPwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
        } else {
            loginPwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
        }
        Editable editable = loginPwd.getText();//光标显示在结尾
        Selection.setSelection(editable, editable.length());
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.loginShowPwd:
                setShowPwd();
                break;
            case R.id.mLogin:
                runOnUiOpen();
                handler.sendEmptyMessageDelayed(1, 1000);//延时发送
                break;

        }
    }

    Handler handler = new Handler(Looper.myLooper()) {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    runOnUiClose();
                    break;
            }
        }
    };

    @Override
    protected void onResume() {
        super.onResume();
        closeKeyboard();
        mLoginLayout.scrollTo(0, 0);
    }
}

//rotate_anim.xml动画文件:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <rotate
        android:duration="2000"
        android:fromDegrees="0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:repeatCount="-1"
        android:repeatMode="restart"
        android:toDegrees="360" />

</set>

//ic_display.xml图片:

 

<vector android:height="24dp" android:viewportHeight="48.0"
    android:viewportWidth="62.0" android:width="31dp" xmlns:android="http://schemas.android.com/apk/res/android">
    <path android:fillColor="#C7C7C7" android:pathData="M31.07,41.61c-14.53,0 -26.51,-12.63 -29.87,-16.49c-0.81,-0.95 -0.81,-2.3 0,-3.25C4.56,18.01 16.55,5.38 31.07,5.38c14.53,0 26.51,12.63 29.87,16.49c0.81,0.95 0.81,2.3 0,3.25C57.59,28.98 45.6,41.61 31.07,41.61L31.07,41.61zM31.07,7.58c-13.58,0 -25.02,12.06 -28.21,15.75c-0.1,0.1 -0.1,0.27 0,0.41c3.18,3.66 14.63,15.68 28.21,15.68S56.1,27.35 59.28,23.66c0.1,-0.1 0.1,-0.27 0,-0.41C56.1,19.6 44.65,7.58 31.07,7.58L31.07,7.58zM31.07,7.58"/>
    <path android:fillColor="#C7C7C7" android:pathData="M31.07,32.16c-4.77,0 -8.67,-3.89 -8.67,-8.67s3.89,-8.67 8.67,-8.67c4.77,0 8.67,3.89 8.67,8.67S35.85,32.16 31.07,32.16L31.07,32.16zM31.07,17.02c-3.59,0 -6.47,2.91 -6.47,6.47s2.91,6.47 6.47,6.47c3.56,0 6.47,-2.91 6.47,-6.47S34.66,17.02 31.07,17.02L31.07,17.02zM31.07,17.02"/>
</vector>

//ic_load.xml图片:

 

<vector android:height="24dp" android:viewportHeight="52.0"
    android:viewportWidth="52.0" android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
    <path android:fillColor="#FFFFFF" android:pathData="M25.97,13.26c-1.32,0 -2.39,-1.08 -2.39,-2.39v-7.96c0,-1.31 1.08,-2.39 2.39,-2.39c1.32,0 2.39,1.08 2.39,2.39v7.96C28.36,12.19 27.29,13.26 25.97,13.26L25.97,13.26zM25.97,13.26"/>
    <path android:fillAlpha="0.7" android:fillColor="#FFFFFF"
        android:pathData="M13.22,26c0,1.31 -1.08,2.39 -2.39,2.39H2.86c-1.32,0 -2.39,-1.08 -2.39,-2.39c0,-1.31 1.08,-2.39 2.39,-2.39h7.97C12.15,23.61 13.22,24.69 13.22,26L13.22,26zM13.22,26" android:strokeAlpha="0.7"/>
    <path android:fillAlpha="0.4" android:fillColor="#FFFFFF"
        android:pathData="M25.97,51.47c-1.32,0 -2.39,-1.07 -2.39,-2.39v-7.96c0,-1.31 1.08,-2.39 2.39,-2.39c1.32,0 2.39,1.08 2.39,2.39v7.96C28.36,50.4 27.29,51.47 25.97,51.47L25.97,51.47zM25.97,51.47" android:strokeAlpha="0.4"/>
    <path android:fillAlpha="0.1" android:fillColor="#FFFFFF"
        android:pathData="M51.47,26c0,1.31 -1.08,2.39 -2.39,2.39h-7.97c-1.32,0 -2.39,-1.08 -2.39,-2.39c0,-1.31 1.08,-2.39 2.39,-2.39h7.97C50.4,23.61 51.47,24.69 51.47,26L51.47,26zM51.47,26" android:strokeAlpha="0.1"/>
    <path android:fillAlpha="0.8" android:fillColor="#FFFFFF"
        android:pathData="M14.93,19.63c-0.66,1.14 -2.13,1.53 -3.27,0.88l-6.9,-3.98c-1.14,-0.66 -1.53,-2.12 -0.88,-3.26c0.66,-1.14 2.13,-1.53 3.27,-0.88l6.9,3.98C15.19,17.02 15.59,18.49 14.93,19.63L14.93,19.63zM14.93,19.63" android:strokeAlpha="0.8"/>
    <path android:fillAlpha="0.5" android:fillColor="#FFFFFF"
        android:pathData="M13.22,48.06c-1.14,-0.66 -1.53,-2.12 -0.88,-3.26l3.98,-6.89c0.66,-1.14 2.13,-1.53 3.27,-0.88c1.14,0.66 1.53,2.12 0.88,3.26l-3.98,6.89C15.83,48.32 14.36,48.72 13.22,48.06L13.22,48.06zM13.22,48.06" android:strokeAlpha="0.5"/>
    <path android:fillAlpha="0.2" android:fillColor="#FFFFFF"
        android:pathData="M48.06,38.74c-0.66,1.14 -2.13,1.53 -3.27,0.88l-6.9,-3.98c-1.14,-0.66 -1.53,-2.12 -0.88,-3.26c0.66,-1.14 2.13,-1.53 3.27,-0.88l6.9,3.98C48.32,36.13 48.71,37.6 48.06,38.74L48.06,38.74zM48.06,38.74" android:strokeAlpha="0.2"/>
    <path android:fillAlpha="0.9" android:fillColor="#FFFFFF"
        android:pathData="M19.6,14.97c-1.14,0.66 -2.61,0.26 -3.27,-0.88l-3.98,-6.89c-0.66,-1.14 -0.26,-2.61 0.88,-3.26c1.14,-0.66 2.61,-0.26 3.27,0.88l3.98,6.9C21.13,12.85 20.74,14.31 19.6,14.97L19.6,14.97zM19.6,14.97" android:strokeAlpha="0.9"/>
    <path android:fillAlpha="0.6" android:fillColor="#FFFFFF"
        android:pathData="M14.93,32.37c0.66,1.14 0.26,2.61 -0.88,3.26l-6.9,3.98c-1.14,0.66 -2.61,0.26 -3.27,-0.88c-0.66,-1.14 -0.26,-2.61 0.88,-3.26l6.9,-3.98C12.8,30.84 14.27,31.23 14.93,32.37L14.93,32.37zM14.93,32.37" android:strokeAlpha="0.6"/>
    <path android:fillAlpha="0.3" android:fillColor="#FFFFFF"
        android:pathData="M38.72,48.06c-1.14,0.66 -2.61,0.26 -3.27,-0.88l-3.98,-6.89c-0.66,-1.14 -0.26,-2.61 0.88,-3.26c1.14,-0.66 2.61,-0.26 3.27,0.88l3.98,6.89C40.26,45.94 39.86,47.4 38.72,48.06L38.72,48.06zM38.72,48.06" android:strokeAlpha="0.3"/>
    <path android:fillAlpha="0.05" android:fillColor="#FFFFFF"
        android:pathData="M48.06,13.26c0.66,1.14 0.26,2.61 -0.88,3.26l-6.9,3.98c-1.14,0.66 -2.61,0.26 -3.27,-0.88c-0.66,-1.14 -0.26,-2.61 0.88,-3.26l6.9,-3.98C45.93,11.73 47.4,12.12 48.06,13.26L48.06,13.26zM48.06,13.26" android:strokeAlpha="0.05"/>
</vector>

//————————————————————————–完—————————————————————————–

 

 

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/118295.html

(0)
seven_的头像seven_bm

相关推荐

发表回复

登录后才能评论
极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!