1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > Android 软键盘覆盖并抬起输入框

Android 软键盘覆盖并抬起输入框

时间:2022-11-21 04:01:14

相关推荐

Android 软键盘覆盖并抬起输入框

页面有EditText,获取焦点时,默认情况下,软键盘会把整个页面布局给顶上去

假设我们想要软键盘覆盖在原页面上,只是把EditText抬起到软键盘之上,

或是想监测软键盘显示或隐藏时,进行一些操作

效果图

思路是用屏幕高度减键盘抬起时,页面可视区域的高度,得到软键盘的高度

关键代码:

Rect r = new Rect();((Activity)(mContext)).getWindow().getDecorView().getWindowVisibleDisplayFrame(r);

Demo代码如下

布局文件

<?xml version="1.0" encoding="utf-8"?><com.example.softkeyboardtest.SoftKeyboardSizeWatchLayoutxmlns:android="/apk/res/android"xmlns:tools="/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:focusable="true"android:focusableInTouchMode="true"android:id="@+id/keyboard_layout"tools:context="com.example.softkeyboardtest.MainActivity"><ImageViewandroid:layout_width="match_parent"android:layout_height="match_parent"android:scaleType="fitXY"android:src="@drawable/img" /><TextViewandroid:id="@+id/tv_show"android:layout_width="match_parent"android:layout_height="wrap_content"android:minHeight="50dp"android:textColor="#ff0000"android:text="Hello World!" /><Viewandroid:id="@+id/v_divider"android:layout_width="match_parent"android:layout_height="1dp"android:layout_above="@+id/ll_input"android:background="#808080" /><LinearLayoutandroid:id="@+id/ll_input"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_alignParentBottom="true"><EditTextandroid:id="@+id/et_input"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"android:maxLines="1"android:textColor="#0055ff"/><Buttonandroid:id="@+id/btn_send"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="确定" /></LinearLayout></com.example.softkeyboardtest.SoftKeyboardSizeWatchLayout>

package com.example.softkeyboardtest;import android.content.Context;import android.os.Handler;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.text.TextUtils;import android.view.MotionEvent;import android.view.View;import android.view.inputmethod.InputMethodManager;import android.widget.Button;import android.widget.EditText;import android.widget.LinearLayout;import android.widget.TextView;public class MainActivity extends AppCompatActivity {EditText mEtInput;Button mBtnSend;TextView mTvShow;View mDivider;LinearLayout mLlInputPanel;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);mEtInput = (EditText)findViewById(R.id.et_input);mBtnSend = (Button)findViewById(R.id.btn_send);mTvShow = (TextView)findViewById(R.id.tv_show);mDivider = findViewById(R.id.v_divider);mLlInputPanel = (LinearLayout)findViewById(R.id.ll_input);mBtnSend.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {if(!TextUtils.isEmpty(mEtInput.getText().toString())) {mTvShow.setText(mEtInput.getText().toString());mEtInput.setText("");}}});((SoftKeyboardSizeWatchLayout)findViewById(R.id.keyboard_layout)).addResizeListener(new SoftKeyboardSizeWatchLayout.OnResizeListener() {@Overridepublic void OnSoftPop(int height) {mLlInputPanel.setTranslationY(-height);mDivider.setTranslationY(-height);}@Overridepublic void OnSoftClose() {mLlInputPanel.setTranslationY(0);mDivider.setTranslationY(0);}});}public void showKeyboard() {final InputMethodManager imm = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);//EditText未绘制完成时,抬起软键盘会失败。所以延时执行new Handler().postDelayed(new Runnable(){public void run() {if(imm != null){if(! imm.showSoftInput(mEtInput, 0)){imm.toggleSoftInput(0,0);}}}}, 100);}public void hideKeyboard() {mEtInput.clearFocus();InputMethodManager imm = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);if(imm != null){imm.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), 0);}}@Overridepublic boolean dispatchTouchEvent(MotionEvent ev) {if (ev.getAction() == MotionEvent.ACTION_DOWN) {if (isShouldHideInput(mEtInput, ev)) {hideKeyboard();}return super.dispatchTouchEvent(ev);}if (getWindow().superDispatchTouchEvent(ev)) {return true;}return onTouchEvent(ev);}/*** 判断点击event 坐标是否位于View v之内*/public boolean isShouldHideInput(View v, MotionEvent event) {if (v != null && (v instanceof EditText)) {int[] leftTop = {0, 0};//获取输入框当前的location位置v.getLocationInWindow(leftTop);int left = leftTop[0];int top = leftTop[1];int bottom = top + v.getHeight();int right = left + v.getWidth();if (event.getX() > left && event.getX() < right&& event.getY() > top && event.getY() < bottom) {return false;} else {v.setFocusable(false);v.setFocusableInTouchMode(true);return true;}}return false;}}

package com.example.softkeyboardtest;import android.app.Activity;import android.content.Context;import android.graphics.Rect;import android.util.AttributeSet;import android.view.ViewTreeObserver;import android.widget.RelativeLayout;import java.util.ArrayList;import java.util.List;/*** Created by Administrator on /10/19.*/public class SoftKeyboardSizeWatchLayout extends RelativeLayout {private Context mContext;private boolean mIsSoftKeyboardPop = false;private int mScreenHeight = 0;private int mOldh = -1;private int mNowh = -1;public SoftKeyboardSizeWatchLayout(Context context, AttributeSet attrs) {super(context, attrs);mContext = context;getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener(){@Overridepublic void onGlobalLayout() {Rect r = new Rect();((Activity)(mContext)).getWindow().getDecorView().getWindowVisibleDisplayFrame(r);if(mScreenHeight == 0){mScreenHeight = r.bottom;}mNowh = mScreenHeight - r.bottom;if(mOldh != -1 && mNowh != mOldh) {if (mNowh > 0) {mIsSoftKeyboardPop = true;if (mListenerList != null) {for (OnResizeListener l : mListenerList) {l.OnSoftPop(mNowh);}}} else {mIsSoftKeyboardPop = false;if (mListenerList != null) {for (OnResizeListener l : mListenerList) {l.OnSoftClose();}}}}mOldh = mNowh;}});}public boolean ismIsSoftKeyboardPop() {return mIsSoftKeyboardPop;}public List<OnResizeListener> mListenerList;public void addResizeListener(OnResizeListener listener){if(null == mListenerList){mListenerList = new ArrayList<OnResizeListener>();}mListenerList.add(listener);}public interface OnResizeListener {/*** 软键盘弹起*/void OnSoftPop(int height);/*** 软键盘关闭*/void OnSoftClose();}}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。