DownEditTextView【自定义Edittext对Android 软键盘向下的监听】
版权声明:本文为HaiyuKing原创文章,转载请注明出处!
前言
记录自定义EditText控件实现监听软键盘隐藏事件的功能。基本上和参考资料相同。
效果图
代码分析
自定义EditText子类,然后重写onKeyPreIme方法
@Override public boolean onKeyPreIme(int keyCode, 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复制到项目中
package com.why.project.androidcnblogsdemo.views.customedittext; import android.annotation.SuppressLint; import android.content.Context; import android.util.AttributeSet; import android.util.Log; import android.view.KeyEvent; import android.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 extends EditText { public DownEditTextView(Context context) { super(context); } public DownEditTextView(Context context, AttributeSet attrs) { super(context, attrs); } public DownEditTextView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override public boolean onKeyPreIme(int keyCode, 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); } /** *键盘监听接口 */ public OnKeyBoardHideListener onKeyBoardHideListener; public void setOnKeyBoardHideListener(OnKeyBoardHideListener onKeyBoardHideListener) { this.onKeyBoardHideListener = onKeyBoardHideListener; } public interface OnKeyBoardHideListener{ void onKeyHide(int keyCode, KeyEvent event); } }
输入框背景图片资源input_box_send.9.png
三、使用方法
在布局文件中引用这个自定义控件【注意,实际项目中需要更改DownEditTextView的完整路径】
<?xml version="1.0" encoding="utf-8"?>
<!-- 自定义EditText的演示(监听软键盘隐藏) -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.why.project.androidcnblogsdemo.views.customedittext.DownEditTextView
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"
/>
</LinearLayout>
Activity中使用
package com.why.project.androidcnblogsdemo.activity; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.KeyEvent; import android.widget.Toast; import com.why.project.androidcnblogsdemo.R; import com.why.project.androidcnblogsdemo.views.customedittext.DownEditTextView; /** * Created by HaiyuKing * Used 自定义EditText的演示(监听软键盘隐藏) */ public class DownEditTextActivity extends AppCompatActivity { private DownEditTextView mDownEditTextView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_downedittext); initViews(); initEvents(); } private void initViews() { mDownEditTextView = (DownEditTextView) findViewById(R.id.edt_down); } private void initEvents() { mDownEditTextView.setOnKeyBoardHideListener(new DownEditTextView.OnKeyBoardHideListener() { @Override public void onKeyHide(int keyCode, KeyEvent event) { Toast.makeText(DownEditTextActivity.this,"软键盘隐藏了",Toast.LENGTH_SHORT).show(); } }); } }
混淆配置
无
参考资料
项目demo下载地址
上面已提供相关代码。