编辑文本隐藏在键盘焦点上。一旦我点击编辑文本,键盘就会隐藏它!!在我的清单中添加了"调整大小",仍然没有成功。
这是我的XML文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="dev.sixhops.app.activities.ChatActivity"
android:background="#FFF3E0"
>
<include
android:id="@+id/app_bar"
layout="@layout/app_bar"></include>
<android.support.v7.widget.RecyclerView
android:id="@+id/chatList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/app_bar"
android:layout_marginBottom="60dp"
android:background="#FFF3E0"
/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginRight="16dp"
android:layout_marginLeft="16dp"
android:layout_marginBottom="8dp"
android:gravity="bottom"
android:background="#FFF3E0">
<ImageView
android:id="@+id/message_send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:src="@drawable/ic_action_send_now"
android:background="@drawable/circle_orange"
android:layout_marginTop="4dp"
android:layout_marginLeft="4dp"/>
<EditText
android:id="@+id/message_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_toLeftOf="@id/message_send"
android:minEms="15"
android:hint="message here!!"
android:textSize="@dimen/font_size3"
android:background="@drawable/chat_bg_white"
android:textColor="@color/black"
android:maxLines="2"
android:padding="10dp"
android:cursorVisible="true"
android:textCursorDrawable="@null"
android:singleLine="true"/>
</RelativeLayout>
我错过了什么吗?
在您的活动onCreate()
方法中尝试此操作:
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
它将在打开时调整键盘上方的窗口。
必须根据显示/隐藏的键盘调整我的视图大小!!
class KeyboardUtil {
private View decorView;
private View contentView;
private float initialDpDiff = -1;
public KeyboardUtil(Activity act, View contentView) {
this.decorView = act.getWindow().getDecorView();
this.contentView = contentView;
decorView.getViewTreeObserver().addOnGlobalLayoutListener(onGlobalLayoutListener);
}
//a small helper to allow showing the editText focus
ViewTreeObserver.OnGlobalLayoutListener onGlobalLayoutListener = new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
Rect r = new Rect();
//r will be populated with the coordinates of your view that area still visible.
decorView.getWindowVisibleDisplayFrame(r);
//get the height diff as dp
float heightDiffDp = convertPixelsToDp(decorView.getRootView().getHeight() - (r.bottom - r.top), decorView.getContext());
//set the initialDpDiff at the beginning. (on my phone this was 73dp)
if (initialDpDiff == -1) {
initialDpDiff = heightDiffDp;
}
//if it could be a keyboard add the padding to the view
if (heightDiffDp - initialDpDiff > 100) { // if more than 100 pixels, its probably a keyboard...
contentView.setPadding(0, 0, 0, (int) convertDpToPixel((heightDiffDp - initialDpDiff), decorView.getContext()));
if (mChatAdapter.getItemCount() > 0)
mRecyclerChat.smoothScrollToPosition(mChatAdapter.getItemCount() - 1);
} else {
contentView.setPadding(0, 0, 0, 0);
if (mChatAdapter.getItemCount() > 0)
mRecyclerChat.smoothScrollToPosition(mChatAdapter.getItemCount() - 1);
}
}
};