DownEditTextView【自定義Edittext對Android 軟鍵盤向下的監聽】

版權聲明:本文爲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);
    }

使用步驟

1、項目組織結構圖

注意事項:佈局

一、  導入類文件後須要change包名以及從新import R文件路徑this

二、  Values目錄下的文件(strings.xml、dimens.xml、colors.xml等),若是項目中存在,則複製裏面的內容,不要整個覆蓋spa

2、導入步驟

將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

3、使用方法

在佈局文件中引用這個自定義控件【注意,實際項目中須要更改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(); } });
    }
}

混淆配置

.net

參考資料

對Android 軟鍵盤向下的監聽code

Android軟鍵盤彈出和收回監聽xml

項目demo下載地址

上面已提供相關代碼。blog

相關文章
相關標籤/搜索