版權聲明:本文爲HaiyuKing原創文章,轉載請註明出處!android
記錄自定義EditText控件實現監聽軟鍵盤隱藏事件的功能。基本上和參考資料相同。app
自定義EditText子類,而後重寫onKeyPreIme方法ide
@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); }
注意事項:佈局
一、 導入類文件後須要change包名以及從新import R文件路徑this
二、 Values目錄下的文件(strings.xml、dimens.xml、colors.xml等),若是項目中存在,則複製裏面的內容,不要整個覆蓋spa
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); } }
<?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>
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(); } }); } }
無.net
上面已提供相關代碼。blog