(原創)EditText軟鍵盤彈出關閉等使用總結

(原創)EditText軟鍵盤彈出關閉等使用總結

1.關於EditText常用屬性設置:

(1).去除其默認劃線背景方式:設置背景色透明即可

	
android:background="#00000000"

(2).設置其顯示閃爍光標方式:光標可見

android:cursorVisible="true"

(3).設置單行:

android:singleLine="true"

(4).設置提醒文本顏色:

android:textColorHint="#dddddd"

(5).去除光標色:

android:textCursorDrawable="@null"

基本就這些設置了,關於彈出軟鍵盤屬性設置應該設置到清單文件相應的activity裏面,因爲其影響的是整個activity。

2.設置一開始進來不彈出軟鍵盤並且軟鍵盤彈出後不會重新繪製activity裏面的view(避免收縮軟鍵盤時activity底部出現一段空白):

 <activity
            android:name="對應的activity"
            android:windowSoftInputMode="adjustNothing|stateHidden" />//其中adjustNothing設置不影響activity重新繪製view佈局,stateHidden第一次進來隱藏軟鍵盤

3.代碼動態設置彈出軟鍵盤和關閉軟鍵盤方式:

(1).代碼動態彈出軟鍵盤方式:

<span style="white-space:pre">	</span>editText.setCursorVisible(true);//動態代碼設置顯示光標方式
 <span style="white-space:pre">	</span>//代碼動態彈出軟鍵盤
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);

(2).代碼動態關閉彈出的軟鍵盤方式:

 <span style="white-space:pre">	</span>editText.setCursorVisible(false);//動態代碼設置隱藏guangbiao
        InputMethodManager im = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        im.hideSoftInputFromWindow(getCurrentFocus().getApplicationWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);

4.關於EditText一些常用的監聽事件的使用:

(1).關於其輸入text文本的完成或者發生變化的監聽:addTextChangedListener()監聽輸入框變化狀態

editText.addTextChangedListener(new TextWatcher() {  
            @Override  
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {  
        //輸入變化前執行  
            }  
  
            @Override  
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {  
        //輸入文本發生變化執行  
            }  
  
            @Override  
            public void afterTextChanged(Editable editable) {  
        //輸入文本停止後的執行方法  
            }  
        });  

(2).關於其軟鍵盤裏面各個控件操作行爲的監聽:

 editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {  
            @Override  
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {  
                if (actionId == EditorInfo.IME_ACTION_DONE) {//點擊軟鍵盤完成控件時觸發的行爲  
            //關閉光標並且關閉軟鍵盤  
                    editeText.setCursorVisible(false);  
                    InputMethodManager im = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);  
                    im.hideSoftInputFromWindow(getCurrentFocus().getApplicationWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);  
                }  
                return true;//消費掉該行爲  
            }  
        });  

(3).關於其獲取到焦點事件的監聽: 

edtiText.setOnTouchListener(new View.OnTouchListener() {  
            @Override  
            public boolean onTouch(View view, MotionEvent motionEvent) {  
        //獲取到焦點顯示光標  
                editText.setCursorVisible(true);  
                return false;  
            }  
        });  

以上暫時個人開發過程中使用到的一些知識點整理。




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章