仿微信软键盘弹出与隐藏
仿微信软键盘弹出与隐藏,效果图如下:
实现输入框弹出,软键盘弹出,获取焦点,否则失去焦点。
首先在 AndroidManifest 文件的对应 Activity 中加入下面代码:
android:windowSoftInputMode="adjustResize"
下面是 MainActivity 中的代码:
public class MainActivity extends AppCompatActivity {
private SoftKeyBoardListener softKeyBoardListener;
private EditText editText;
private TextView tv_context, tv_send;
private RelativeLayout rl_sendMessage;
private View shape_bg;//灰色背景
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = findViewById(R.id.editText);
tv_context = findViewById(R.id.tv_context);
tv_send = findViewById(R.id.tv_send);
rl_sendMessage = findViewById(R.id.rl_sendMessage);
shape_bg = findViewById(R.id.shape_bg);
setListener();
setSoftKeyBoardListener();
}
/**
* setListener()
* 设置点击事件
*/
private void setListener() {
//上方点击输入的点击事件
tv_context.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
setSoftKeyBoard();
}
});
//灰色背景的点击事件
shape_bg.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//强制隐藏键盘
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
});
//发送按钮点击事件
tv_send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (!editText.getText().toString().equals("")) {
setSoftKeyBoard();
tv_context.setText(editText.getText().toString());
} else {
Toast.makeText(MainActivity.this, "请输入发送内容", Toast.LENGTH_SHORT).show();
}
}
});
}
/**
* setSoftKeyBoard()
* 设置软键盘显示/隐藏
*/
private void setSoftKeyBoard() {
//自动设置软键盘显示或隐藏
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
}
/**
* setSoftKeyBoardListener()
* 监听软键盘显示/隐藏
*/
private void setSoftKeyBoardListener() {
//软键盘显示/隐藏监听
softKeyBoardListener = new SoftKeyBoardListener(this);
softKeyBoardListener.setListener(new SoftKeyBoardListener.OnSoftKeyBoardChangeListener() {
@Override
public void keyBoardShow(int height) {
//此处的height为软键盘的高度
//软键盘显示,底部发送消息和灰色背景显示,EditText获取焦点
rl_sendMessage.setVisibility(View.VISIBLE);
shape_bg.setVisibility(View.VISIBLE);
editText.setFocusable(true);
editText.setFocusableInTouchMode(true);
editText.setCursorVisible(true);
editText.requestFocus();
}
@Override
public void keyBoardHide(int height) {
//软键盘隐藏,底部发送消息和灰色背景隐藏,EditText失去焦点
rl_sendMessage.setVisibility(View.GONE);
shape_bg.setVisibility(View.GONE);
editText.setFocusable(false);
editText.setFocusableInTouchMode(false);
editText.setCursorVisible(false);
}
});
}
}
上面注释很清楚了,主要是监听软键盘显示与隐藏,做出不同的处理,下面是对应 xml 文件的代码:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tv_context"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#FFFFFF"
android:gravity="center_vertical"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:text="点击输入"
android:textSize="13sp" />
<View
android:id="@+id/shape_bg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#89000000"
android:visibility="gone" />
<RelativeLayout
android:id="@+id/rl_sendMessage"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_alignParentBottom="true"
android:visibility="gone">
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_toLeftOf="@+id/tv_send"
android:background="#FFFFFF"
android:hint="请输入"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:textSize="13sp" />
<TextView
android:id="@+id/tv_send"
android:layout_width="60dp"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:background="@color/colorPrimaryDark"
android:gravity="center"
android:text="发送"
android:textColor="#FFFFFF" />
</RelativeLayout>
</RelativeLayout>
最后是监听软键盘显示与隐藏的工具类 SoftKeyBoardListener 中的代码:
/**
* author: wu
* date: on 2019/1/21.
* describe:
*/
public class SoftKeyBoardListener {
private View rootView;//activity的根视图
int rootViewVisibleHeight;//纪录根视图的显示高度
private OnSoftKeyBoardChangeListener onSoftKeyBoardChangeListener;
public SoftKeyBoardListener(Activity activity) {
//获取activity的根视图
rootView = activity.getWindow().getDecorView();
//监听视图树中全局布局发生改变或者视图树中的某个视图的可视状态发生改变
rootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
//获取当前根视图在屏幕上显示的大小
Rect r = new Rect();
rootView.getWindowVisibleDisplayFrame(r);
int visibleHeight = r.height();
if (rootViewVisibleHeight == 0) {
rootViewVisibleHeight = visibleHeight;
return;
}
//根视图显示高度没有变化,可以看作软键盘显示/隐藏状态没有改变
if (rootViewVisibleHeight == visibleHeight) {
return;
}
//根视图显示高度变小超过200,可以看作软键盘显示了
if (rootViewVisibleHeight - visibleHeight > 200) {
if (onSoftKeyBoardChangeListener != null) {
onSoftKeyBoardChangeListener.keyBoardShow(rootViewVisibleHeight - visibleHeight);
}
rootViewVisibleHeight = visibleHeight;
return;
}
//根视图显示高度变大超过200,可以看作软键盘隐藏了
if (visibleHeight - rootViewVisibleHeight > 200) {
if (onSoftKeyBoardChangeListener != null) {
onSoftKeyBoardChangeListener.keyBoardHide(visibleHeight - rootViewVisibleHeight);
}
rootViewVisibleHeight = visibleHeight;
return;
}
}
});
}
private void setOnSoftKeyBoardChangeListener(OnSoftKeyBoardChangeListener onSoftKeyBoardChangeListener) {
this.onSoftKeyBoardChangeListener = onSoftKeyBoardChangeListener;
}
public interface OnSoftKeyBoardChangeListener {
void keyBoardShow(int height);
void keyBoardHide(int height);
}
public void setListener(OnSoftKeyBoardChangeListener onSoftKeyBoardChangeListener) {
setOnSoftKeyBoardChangeListener(onSoftKeyBoardChangeListener);
}
}
这样便可以实现上面的效果了。
上面工具类使用中已经获取到软键盘的高度,可以根据此高度设置如微信、QQ中的表情高度。