思路:
放一个布局在页面底部,隐藏起来。当输入法弹起来的时候,检测到布局的变化,设置插件布局的显示即可
- 在
AndroidManifest.xml
里相应的Activity里加上android:windowSoftInputMode="stateHidden|adjustResize"
至于为什么是adjustResize而不是adjustSpan,看图
平时是这样
adjustResize是这样
adjustPan是这样
区别:
EditText本来在背景的L处
adjustResize会把EditText顶到G处
adjustPan,EditText还是在L处,输入法把所有布局都顶起来了。
当然这俩还有一些别的区别,这不是本文讨论的重点。
上代码
布局:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:focusable="true" android:focusableInTouchMode="true"> <EditText android:id="@+id/et" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:hint="XXXXXXXXXXXX"/>
<TextView android:id="@+id/tv" android:layout_alignParentBottom="true" android:layout_width="match_parent" android:layout_height="wrap_content" android:textColor="@android:color/white" android:textSize="20sp" android:background="@android:color/holo_green_light" android:text="bbbbbbbbbbbbbbbbbbbbbb" android:visibility="invisible"/>
<TextView android:layout_width="match_parent" android:layout_height="match_parent" android:text="A\nB\nC\nD\nE\nD\nF\nG\nH\nI\nJ\nK\nL\nM\nN" android:textSize="60sp"/> </RelativeLayout>
|
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| public class MainActivity extends AppCompatActivity { private TextView mTv; private EditText mEt;
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mTv = (TextView) findViewById(R.id.tv); mEt = (EditText) findViewById(R.id.et); getWindow().getDecorView().getViewTreeObserver() .addOnGlobalLayoutListener(mLayoutChangeListener); }
ViewTreeObserver.OnGlobalLayoutListener mLayoutChangeListener = new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { Rect r = new Rect(); getWindow().getDecorView().getWindowVisibleDisplayFrame(r); int mScreenHeight = ScreenUtils.getScreenHeight(); int heightDifference = mScreenHeight - (r.bottom - r.top); boolean isKeyboardShowing = heightDifference > mScreenHeight / 3; if(isKeyboardShowing){ mTv.setVisibility(View.VISIBLE); }else{ mTv.setVisibility(View.INVISIBLE); } } }; }
|
为复用方便,拎了一个工具类出来
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
| public class KeyBoardPluginUtils {
public static void addKeyBoardPluginGlobal(Activity activity,LinearLayout llPlugin) { activity.getWindow().getDecorView().getViewTreeObserver() .addOnGlobalLayoutListener(new GlobalLayoutListener(activity,llPlugin)); }
public static void addKeyBoardPlugin(EditText et, final LinearLayout llPlugin) { et.setOnFocusChangeListener(new View.OnFocusChangeListener() { @Override public void onFocusChange(View v, boolean hasFocus) { if (hasFocus) { llPlugin.setVisibility(View.VISIBLE); }else { llPlugin.setVisibility(View.INVISIBLE); } } });
}
private static class GlobalLayoutListener implements ViewTreeObserver.OnGlobalLayoutListener { private Activity mActivity; private LinearLayout mLlPlugin; private GlobalLayoutListener(Activity activity, LinearLayout llPlugin) { mActivity = activity; mLlPlugin = llPlugin; }
@Override public void onGlobalLayout() { Rect r = new Rect(); mActivity.getWindow().getDecorView().getWindowVisibleDisplayFrame(r); int mScreenHeight = ScreenUtils.getScreenHeight(); int heightDifference = mScreenHeight - (r.bottom - r.top); boolean isKeyboardShowing = heightDifference > mScreenHeight / 3; if(isKeyboardShowing){ mLlPlugin.setVisibility(View.VISIBLE); }else{ mLlPlugin.setVisibility(View.INVISIBLE); } } } }
|