Android:隱藏IME軟鍵盤

來源:互聯網
上載者:User

標籤:http   dma   ted   ati   ber   str   undle   declared   命名空間   

1.概述

1) 給LinearLayout 註冊click 事件,點擊後隱藏IME軟鍵盤。為什麼要叫軟鍵盤,很奇怪呢。通過Activity 的getSystem(Context.INPUT_METHOD_SERVICE) 擷取到IME的對象,接著把它隱藏掉。

2) EditText 輸入的字元達到最大值時隱藏IME軟鍵盤。註冊EditText 的TextWatcher 事件,通過判斷輸入的字元數達到最大長度時隱藏掉IME軟鍵盤。

2.Activity 後台代碼

public class EditHideActivity extends AppCompatActivity implements View.OnClickListener {    private LinearLayout ll_hide;    private EditText et_phone;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_edit_hide);        ll_hide = (LinearLayout) findViewById(R.id.ll_hide);        et_phone = (EditText) findViewById(R.id.et_phone);        //給LinearLayout 註冊click 監聽,點擊後隱藏IME        ll_hide.setOnClickListener(this);        //給EditText 註冊TextChanged 監聽,文本的變化會觸發監聽事件        et_phone.addTextChangedListener(new HideTextWatcher(et_phone));    }    //實現OnClickListener 的onClick 事件    @Override    public void onClick(View view) {        if (view.getId() == R.id.ll_hide) {            hideOneInputMethod(EditHideActivity.this, et_phone);        }    }    private void hideOneInputMethod(Activity act, View v) {        //實際上不只是et_other的軟鍵盤會關閉,其它編輯框的軟鍵盤也會關閉        //因為方法內部去擷取視圖的WindowToken,這個Token在每個頁面上都是唯一的        InputMethodManager imm = (InputMethodManager) act.getSystemService(Context.INPUT_METHOD_SERVICE);        imm.hideSoftInputFromWindow(v.getWindowToken(), 0);    }    private void hideAllInputMethod(Activity act) {        InputMethodManager imm = (InputMethodManager) act.getSystemService(Context.INPUT_METHOD_SERVICE);        if (imm.isActive() == true) {            imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);        }    }    //擷取EditText 最大長度    private int getMaxLength(EditText et) {        int length = 0;        try {            InputFilter[] inputFilters = et.getFilters();            //遍曆所有過濾器            for (InputFilter filter : inputFilters) {                Class<?> c = filter.getClass();                //是否有InputFilter$LengthFilter 過濾器                if (c.getName().equals("android.text.InputFilter$LengthFilter")) {                    //Field 的命名空間為: java.lang.reflect.Field                    Field[] f = c.getDeclaredFields();                    //遍曆過濾器的所有欄位                    for (Field field : f) {                        if (field.getName().equals("mMax")) {                            field.setAccessible(true);                            length = (Integer) field.get(filter);                        }                    }                }            }        } catch (Exception ex) {            ex.printStackTrace();        }        return length;    }    private class HideTextWatcher implements TextWatcher {        private CharSequence mStr;        private EditText mView;        private int mMaxLength;        //通過建構函式注入要監聽的EditText,持有EditText 便於事件裡的操作        public HideTextWatcher(EditText v) {            super();            mView = v;            //擷取EditText 的最大長度            mMaxLength = getMaxLength(v);        }        //CharSequence charSequence:文本改變之前的內容        //int i:文本開始改變時的起點位置,從0開始計算        //int i1:要被改變的文本字數,即將要被替代的選中文本字數        //int i2:改變後添加的文本字數,即替代選中文本後的文本字數        @Override        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {        }        //CharSequence charSequence:文本改變之後的內容        //int i:文本開始改變時的起點位置,從0開始計算        //int i1:要被改變的文本字數,即已經被替代的選中文本字數        //int i2:改變後添加的文本字數,即替代選中文本後的文本字數        @Override        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {            mStr = charSequence;        }        //Editable editable: 改變後的最終文本        @Override        public void afterTextChanged(Editable editable) {            if (mStr == null || mStr.length() == 0) {                return;            } else if (mStr.length() == mMaxLength) {                //輸入的字元達到最大時,隱藏IME                hideAllInputMethod(EditHideActivity.this);            }        }    }}

  

3.layout 布局檔案代碼

<?xml version="1.0" encoding="utf-8"?><android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="com.example.administrator.middledemo.EditHideActivity">    <LinearLayout        android:id="@+id/ll_hide"        android:layout_width="368dp"        android:layout_height="495dp"        android:orientation="vertical"        tools:layout_editor_absoluteY="8dp"        tools:layout_editor_absoluteX="8dp">        <View            android:layout_width="match_parent"            android:layout_height="20dp" />        <EditText            android:id="@+id/et_phone"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:ems="10"            android:maxLength="11"            android:inputType="number"            style="@style/text_normal"/>    </LinearLayout></android.support.constraint.ConstraintLayout>

  

 

Android:隱藏IME軟鍵盤

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.