Android EditText懸浮在輸入法之上


Android EditText懸浮在輸入法之上

使用 android:windowSoftInputMode="adjustResize" 會讓界面整體被頂上去,很多時候我們不需要這樣的情況出現,這里給出另一個方案.

**思路:監聽輸入法的狀態,然后動態的滾動 EditText 所在的 ViewGroup 或者View **

1. Android Manifest.xml

<activity
        android:name=".InputActivity"
        android:theme="@style/Theme.AppCompat.Light.NoActionBar"
        android:configChanges="keyboard|keyboardHidden|orientation"
        android:screenOrientation="portrait"
        android:windowSoftInputMode="adjustPan"> //非adjustResize
 </activity>

2. 布局文件

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/root"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/bg_saber_q"
    tools:context="didikee.com.demoapk.InputActivity">
    <LinearLayout
        android:id="@+id/rl_inputdlg_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="#00000000"
        android:orientation="horizontal">

        <EditText
            android:id="@+id/input_message"
            android:layout_width="0dp"
            android:layout_height="@dimen/dp30"
            android:layout_gravity="center|left"
            android:layout_marginLeft="@dimen/dp12"
            android:paddingLeft="@dimen/dp2"
            android:paddingRight="@dimen/dp2"
            android:layout_weight="1"
            android:background="@drawable/shape_cricle_gray_solid_white"
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:gravity="left"
            android:imeOptions="actionSend"
            android:maxLength="32"
            android:singleLine="true"
            android:text=""
            android:textColor="@color/dark_text"
            android:textSize="20sp"/>

        <TextView
            android:id="@+id/confrim_btn"
            android:layout_width="@dimen/dp50"
            android:layout_height="@dimen/dp30"
            android:layout_gravity="center|right"
            android:layout_marginLeft="@dimen/dp11"
            android:layout_marginRight="@dimen/dp12"
            android:background="@drawable/shape_cricle_solid_orange"
            android:gravity="center"
            android:text="發送"
            android:textColor="@color/white"/>
    </LinearLayout>

</RelativeLayout>

3. Activity里設置監聽,滾動 input 視圖

public class InputActivity extends AppCompatActivity {

    private RelativeLayout rLayout;
    private View mInputLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_input);
        mInputLayout = findViewById(R.id.rl_inputdlg_view);

        rLayout = ((RelativeLayout) findViewById(R.id.root));
        //輸入法到底部的間距(按需求設置)
        final int paddingBottom = DisplayUtil.dp2px(this, 5);

        rLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

            @Override
            public void onGlobalLayout() {
                Rect r = new Rect();
                rLayout.getWindowVisibleDisplayFrame(r);
                //r.top 是狀態欄高度
                int screenHeight = rLayout.getRootView().getHeight();
                int softHeight = screenHeight - r.bottom ;
                Log.e("test","screenHeight:"+screenHeight);
                Log.e("test","top:"+r.top);
                Log.e("test","bottom:"+r.bottom);
                Log.e("test", "Size: " + softHeight);
                if (softHeight>100){//當輸入法高度大於100判定為輸入法打開了
                    rLayout.scrollTo(0, softHeight+paddingBottom);
                }else {//否則判斷為輸入法隱藏了
                    rLayout.scrollTo(0, paddingBottom);
                }
            }
        });
    }
}

效果圖:

1. 不做處理前:

2. 處理后的效果:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM