1. 程式人生 > >EditText文字改變與軟鍵盤的監聽事件

EditText文字改變與軟鍵盤的監聽事件

    對於EditText對於從事android開發的人來說非常的熟悉,但是能熟練的應用這個控制元件也需要自己多花點心才能熟於心.我將寫寫關於自己在實際開發中所遇到的坑和問題及解決方案.

 使用場景:

  1.對EditText上輸入的文字時時的去監聽:

    使用程式碼如下:
mEditText.addTextChangedListener(new TextWatcher() {
    //文字改變之前的監聽
    @Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        
    }
   //文字改變的監聽
@Overridepublic void onTextChanged(CharSequence s, int start, int before, int count) { }
   //文字改變之後的監聽
@Overridepublic void afterTextChanged(Editable s) { //注意這裡的s就是EditText上的輸入的文字,只需要toString()即可 }});
2.對軟鍵盤的監聽:
mEditText.setOnEditorActionListener(new TextView.OnEditorActionListener
() {
    @Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
   //在這裡可以進行對軟鍵盤Enter鍵判斷,對應的就是EditText在xml設定的
      android:imeOptions="actionDone" 屬性對應                             
     if (actionId == EditorInfo.IME_ACTION_DONE) { 
      //在這個方法中處理你需要處理的事件
       return true
; }
return false; }});
  3.在開發中使用ListView和ScollView等具有滑動功能的控制元件的時候,當滑動的時候在介面的上面
會出現陰影解決方案:
   只需在style中配置如下屬性:紅色部分
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="android:overScrollMode">never</item>
</style>
  總結:在開發的道路上我們可能時不時的會踩到坑,這篇文章是我實際開發中所遇到的問題和坑,希望這篇文章對你有所幫助,
謝謝!!!!!!!!!!!