本文提供APP开发当中常用的edittext效果
- 需求,在当前页面底部显示一个布局,布局内包含一个输入框,右侧有一个按钮。在键盘显示时布局上移,在输入文字比较多事输入框高度自动增加,并且显示行数不操过3行。下面是显示效果
因为在布局上面直接显示布局,所以不需要任何设置系统默认就是这个效果。我们只需要实现相应布局即可
布局文件如下,大家看看属性就明白了。这里注意一下如果不在webview上面显示,要在webview下面显示大家可以试试修改会发送什么现象
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimary">
<WebView
android:id="@+id/web_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<LinearLayout
android:id="@+id/ll_comment"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="1dip"
android:background="#c9c5c1" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#FbF9F7"
android:maxHeight="90dp"
android:orientation="horizontal"
android:paddingBottom="5dip"
android:paddingLeft="10dip"
android:paddingRight="10dip"
android:paddingTop="5dip">
<EditText
android:id="@+id/et_comment_input"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_marginRight="10dp"
android:layout_weight="1.0"
android:hint="请输入评论内容"
android:imeOptions="actionDone"
android:maxHeight="60dp"
android:maxLength="150"
android:padding"5dp"
android:textSize="14sp" />
<Button
android:id="@+id/btn_comment"
android:layout_width="60dip"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="发表"
android:textSize="14sp" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
2 需求:edittext所在布局有多个控件,edittext在和其它布局有相对关系。要求edittext根据键盘是否显示动态调整大小,整个布局调整后的大小正好铺面整个屏幕。
这里找到一个别人的总结,大家可以先看看能不能解决自己的问题
类似需求
git地址
上面的是修改整体布局,键盘弹出是调整rootview的大小达到所有控件不被遮挡效果
这里的需求是调整edittext大小,所以要做一下调整。基本思路和上面一样,监听到布局变化以后动态调整edittext的高度 看下面的效果图
因项目代码不能提供全部代码,主要代码如下
<com.xxxx.KeyboardLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_ll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ScrollView
android:id="@+id/login_ll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
//你自己的布局
/>
</LinearLayout>
</ScrollView>
</com.xxxx.KeyboardLayout>
在默认显示无论是否显示屏幕这题要调整到正好满屏效果,如果因为各种布局无法设置准确的edittext的高度可以在布局加载完毕后动态调整
defaultEdittextHeight= screenHeight - title_bar_height - statusBarHeight - 你的各种布局的高度;
// ViewGroup.LayoutParams lp = postEditext.getLayoutParams();
// lp.height = defaultEdittextHeight;
// postEditext.setLayoutParams(lp);
这里推荐使用,在xml设置@dimen/yourHeight,在代码中获取这个值系统会根据分别率动态计算给你
getResources().getDimensionPixelSize(R.dimen.yourHeight)
下面是对类似需求简化版本
public void addLayoutListener() {
postGroupView.setKeyboardListener(new KeyboardLayout.KeyboardLayoutListener() {
@Override
public void onKeyboardStateChanged(boolean isActive, final int keyboardHeight) {
ViewGroup.LayoutParams lp = postEditext.getLayoutParams();
lp.height = defaultEdittextHeight - postGroupView.getKeyboardHeight();
postEditext.setLayoutParams(lp);
});
}
这里我们直接调整edittext的高度,和上面的链接相比 我们不需要scrollview滚动就可以做到整屏幕显示,而且不会遮挡屏幕
这里有一个坑注意一下
如果postEditext.heitgh = defaultEdittextHeight,显示效果不会有任何变化。但是如果系统因需要重新调整view的大小,那么你设置的值就会有效。有兴趣的可以在addLayoutListener试一下,设置的高度和显示效果正好相反
修改的KeyboardLayout源码,支持touch事件拦截(如果下层还有view情况不拦截,下面的view会同时触发touch事件),事件监听处理一下键盘改变时候只会发一次事件
public class KeyboardLayout extends FrameLayout {
private KeyboardLayoutListener mListener;
private boolean mIsKeyboardActive = false; //输入法是否激活
private int mKeyboardHeight = 0; // 输入法高度
public KeyboardLayout(Context context) {
this(context, null, 0);
}
public KeyboardLayout(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public KeyboardLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
// 监听布局变化
getViewTreeObserver().addOnGlobalLayoutListener(new KeyboardOnGlobalChangeListener());
}
// @Override
// public boolean dispatchTouchEvent(MotionEvent ev)
// {
// super.dispatchTouchEvent(ev);
// return false;
// }
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
if (isHoldTouch) {
return false;
}else{
return super.onInterceptTouchEvent(ev);
}
}
public boolean onTouchEvent(MotionEvent ev){
if (isHoldTouch) {
return true;
}else{
return super.onTouchEvent(ev);
}
}
public void setKeyboardListener(KeyboardLayoutListener listener) {
mListener = listener;
}
boolean isHoldTouch;
public void setHoldTouchEvent(){
isHoldTouch = true;
}
public KeyboardLayoutListener getKeyboardListener() {
return mListener;
}
public boolean isKeyboardActive() {
return mIsKeyboardActive;
}
/**
* 获取输入法高度
*
* @return
*/
public int getKeyboardHeight() {
return mKeyboardHeight;
}
public interface KeyboardLayoutListener {
/**
* @param isActive 输入法是否激活
* @param keyboardHeight 输入法面板高度
*/
void onKeyboardStateChanged(boolean isActive, int keyboardHeight);
}
private class KeyboardOnGlobalChangeListener implements ViewTreeObserver.OnGlobalLayoutListener {
int mScreenHeight = 0;
private int getScreenHeight() {
if (mScreenHeight > 0) {
return mScreenHeight;
}
mScreenHeight = ((WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE))
.getDefaultDisplay().getHeight();
return mScreenHeight;
}
@Override
public void onGlobalLayout() {
Rect rect = new Rect();
// 获取当前页面窗口的显示范围
((Activity) getContext()).getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
int screenHeight = getScreenHeight();
int keyboardHeight = screenHeight - rect.bottom; // 输入法的高度
if (mKeyboardHeight == keyboardHeight) return;
boolean isActive = false;
if (Math.abs(keyboardHeight) > screenHeight / 5) {
isActive = true; // 超过屏幕五分之一则表示弹出了输入法
mKeyboardHeight = keyboardHeight;
}else{
mKeyboardHeight = 0;
}
if (mIsKeyboardActive == isActive) return;
mIsKeyboardActive = isActive;
if (mListener != null) {
mListener.onKeyboardStateChanged(isActive, keyboardHeight);
}
}
}
}
在mainfest需要给对应activity添加 a**djustResize**属性,如果用其他属性在oppo手机上会出现view上移问题,造成显示异常