1. 程式人生 > >關於Android軟鍵盤遮擋的問題

關於Android軟鍵盤遮擋的問題

在開發登入介面時,點選某個EditText準備輸入,彈出的軟鍵盤遮擋了按鈕或者下面的輸入框,在完成這個文字框的輸入後,想要繼續下面的操作,需要先隱藏軟鍵盤。這會影響使用者操作的流暢感,所以需要解決。

問題如下:

3012329-a172a3f4cd4055e8.png

image.png

解決方法

監聽佈局變化,當軟鍵盤彈出時,滾動佈局使顯示出來。下面直接放程式碼。

activity_login.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/ll_root"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:focusableInTouchMode="true"
    android:orientation="vertical">

    <include layout="@layout/toolbar_layout" />


    <ScrollView
        android:id="@+id/scrollView"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">


            <ImageView
                android:id="@+id/imageView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginTop="80dp"
                app:srcCompat="@mipmap/ic_launcher_round" />

            <android.support.design.widget.TextInputLayout
                android:id="@+id/textInputLayout1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginEnd="42dp"
                android:layout_marginStart="42dp"
                android:layout_marginTop="60dp">


                <com.sunshuai.ramanspectrometer.widgets.DropEditText
                    android:id="@+id/edit_username"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="@string/username_hint"
                    android:singleLine="true" />

            </android.support.design.widget.TextInputLayout>


            <android.support.design.widget.TextInputLayout
                android:id="@+id/textInputLayout2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginEnd="42dp"
                android:layout_marginStart="42dp"
                android:layout_marginTop="3dp">

                <EditText
                    android:id="@+id/edit_password"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:ems="10"
                    android:hint="@string/password_hint"
                    android:inputType="textPassword"
                    android:singleLine="true" />
            </android.support.design.widget.TextInputLayout>


            <android.support.v7.widget.AppCompatButton
                android:id="@+id/btn_login"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginEnd="42dp"
                android:layout_marginStart="42dp"
                android:layout_marginTop="30dp"
                android:text="@string/login">

            </android.support.v7.widget.AppCompatButton>
        </LinearLayout>
    </ScrollView>
</LinearLayout>

LoginActivity.java

package com.sunshuai.ramanspectrometer.login;

import android.graphics.Rect;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.Toast;

import com.sunshuai.ramanspectrometer.R;
import com.sunshuai.ramanspectrometer.base.BaseActivity;
import com.sunshuai.ramanspectrometer.device.MainActivity;
import com.sunshuai.ramanspectrometer.widgets.DropEditText;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;


/**
 * Created by sunshuai on 2018/1/16
 */

public class LoginActivity extends BaseActivity implements LoginContract.View {

    private LoginContract.Presenter mPresenter;
    private int scrollToPosition = 0;

    @BindView(R.id.edit_username)
    DropEditText editUserName;
    @BindView(R.id.edit_password)
    EditText editPassword;
    @BindView(R.id.btn_login)
    Button btnLogin;
    @BindView(R.id.ll_root)
    LinearLayout ll_root;
    @BindView(R.id.scrollView)
    ScrollView scrollView;

    @OnClick(R.id.btn_login)
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_login:
                if (TextUtils.isEmpty(editUserName.getText()) || TextUtils.isEmpty(editPassword.getText())) {
                    Toast.makeText(this, R.string.username_or_password_is_empty, Toast.LENGTH_SHORT).show();
                } else {
                    mPresenter.login();
                }
                break;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mPresenter = new LoginPresenter(this);
        mPresenter.start();
    }

    @Override
    protected void initView() {
        super.initView();
        autoScrollView(ll_root);
    }

    private void autoScrollView(final View rootView) {
        rootView.getViewTreeObserver().addOnGlobalLayoutListener(
                new ViewTreeObserver.OnGlobalLayoutListener() {
                    @Override
                    public void onGlobalLayout() {
                        Rect rect = new Rect();
                        //獲取root在窗體的可視區域
                        rootView.getWindowVisibleDisplayFrame(rect);
                        //獲取root在窗體的不可視區域高度(被遮擋的高度)
                        int rootInvisibleHeight = rootView.getRootView().getHeight() - rect.bottom;
                        //若不可視區域高度大於150(鍵盤顯示時)
                        if (rootInvisibleHeight > 150) {
                            scrollView.fullScroll(ScrollView.FOCUS_DOWN);
                        } else {
                            //鍵盤隱藏
                            scrollToPosition = 0;
                        }
                        rootView.scrollTo(0, scrollToPosition);

                    }
                });
    }

    protected boolean isShowBacking() {
        return false;
    }


    @Override
    protected int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    public void loadUserList(ArrayList<String> arrayList) {
        UserAdapter userAdapter = new UserAdapter(this, arrayList);
        editUserName.setAdapter(userAdapter);
    }

    @Override
    public String getUserName() {
        return editUserName.getText().toString();
    }

    @Override
    public String getPassword() {
        return editPassword.getText().toString();
    }

    @Override
    public void loginSuccessManager() {
        btnLogin.setText(R.string.login);
        Toast.makeText(this, R.string.login_success_manager, Toast.LENGTH_SHORT).show();
        jumpToActivity(MainActivity.class);
        finish();
    }

    @Override
    public void loginSuccessOthers() {
        btnLogin.setText(R.string.login);
        Toast.makeText(this, R.string.login_success_others, Toast.LENGTH_SHORT).show();
        jumpToActivity(MainActivity.class);
        finish();
    }

    @Override
    public void loginFail() {
        btnLogin.setText(R.string.login);
        Toast.makeText(this, R.string.login_failed, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showLogining() {
        btnLogin.setText(R.string.logining);
    }

    @Override
    public void setPresent(LoginContract.Presenter present) {
        mPresenter = present;
    }
}