版权声明:本文为HaiyuKing原创文章,转载请注明出处!
前言
记录自定义EditText控件实现监听软键盘隐藏事件的功能。基本上和参考资料相同。
效果图
代码分析
自定义EditText子类,然后重写onKeyPreIme方法
@Overridepublic boolean onKeyPreIme(intkeyCode, KeyEvent event) {if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == 1) {
Log.i("DownEditTextView", "键盘向下 ");super.onKeyPreIme(keyCode, event);if(onKeyBoardHideListener != null){
onKeyBoardHideListener.onKeyHide(keyCode, event);//回调
}return false;
}return super.onKeyPreIme(keyCode, event);
}
使用步骤
一、项目组织结构图
注意事项:
1、 导入类文件后需要change包名以及重新import R文件路径
2、 Values目录下的文件(strings.xml、dimens.xml、colors.xml等),如果项目中存在,则复制里面的内容,不要整个覆盖
二、导入步骤
将DownEditTextView复制到项目中
packagecom.why.project.androidcnblogsdemo.views.customedittext;importandroid.annotation.SuppressLint;importandroid.content.Context;importandroid.util.AttributeSet;importandroid.util.Log;importandroid.view.KeyEvent;importandroid.widget.EditText;/*** Created by HaiyuKing
* Used 对Android 软键盘向下的监听:https://blog.csdn.net/yxhuang2008/article/details/53822072* Android软键盘弹出和收回监听:https://blog.csdn.net/wangkai1101/article/details/79066046
*/@SuppressLint("AppCompatCustomView")public class DownEditTextView extendsEditText {publicDownEditTextView(Context context) {super(context);
}publicDownEditTextView(Context context, AttributeSet attrs) {super(context, attrs);
}public DownEditTextView(Context context, AttributeSet attrs, intdefStyleAttr) {super(context, attrs, defStyleAttr);
}
@Overridepublic boolean onKeyPreIme(intkeyCode, KeyEvent event) {if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == 1) {
Log.i("DownEditTextView", "键盘向下 ");super.onKeyPreIme(keyCode, event);if(onKeyBoardHideListener != null){
onKeyBoardHideListener.onKeyHide(keyCode, event);//回调
}return false;
}return super.onKeyPreIme(keyCode, event);
}/***键盘监听接口*/
publicOnKeyBoardHideListener onKeyBoardHideListener;public voidsetOnKeyBoardHideListener(OnKeyBoardHideListener onKeyBoardHideListener) {this.onKeyBoardHideListener =onKeyBoardHideListener;
}public interfaceOnKeyBoardHideListener{void onKeyHide(intkeyCode, KeyEvent event);
}
}
输入框背景图片资源input_box_send.9.png
三、使用方法
在布局文件中引用这个自定义控件【注意,实际项目中需要更改DownEditTextView的完整路径】
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:id="@+id/edt_down"
android:layout_width="match_parent"
android:layout_height="48dp"
android:inputType="text"
android:hint="演示监听软键盘隐藏功能"
android:lines="1"
android:background="@drawable/input_box_send"
android:layout_margin="8dp"
/>
Activity中使用
packagecom.why.project.androidcnblogsdemo.activity;importandroid.os.Bundle;importandroid.support.v7.app.AppCompatActivity;importandroid.view.KeyEvent;importandroid.widget.Toast;importcom.why.project.androidcnblogsdemo.R;importcom.why.project.androidcnblogsdemo.views.customedittext.DownEditTextView;/*** Created by HaiyuKing
* Used 自定义EditText的演示(监听软键盘隐藏)*/
public class DownEditTextActivity extendsAppCompatActivity {privateDownEditTextView mDownEditTextView;
@Overrideprotected voidonCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);
setContentView(R.layout.activity_downedittext);
initViews();
initEvents();
}private voidinitViews() {
mDownEditTextView=(DownEditTextView) findViewById(R.id.edt_down);
}private voidinitEvents() {
mDownEditTextView.setOnKeyBoardHideListener(newDownEditTextView.OnKeyBoardHideListener() {
@Overridepublic void onKeyHide(intkeyCode, KeyEvent event) {
Toast.makeText(DownEditTextActivity.this,"软键盘隐藏了",Toast.LENGTH_SHORT).show();
}
});
}
}
混淆配置
无
参考资料
项目demo下载地址
上面已提供相关代码。