常用屬性:
- inputType:(代碼中:setiputtype)設置輸入類型,多種類型中間用“|”
- maxlength:最大長度,無法通過代碼設置
- hint:提示文本內容,(代碼中:setHint)
- textColorHint:提示文本顏色,(代碼中:setHintTextColor)
輸入類型取值說明:
輸入類型 | 說明 |
text | 文本 |
textPassword | 文本密碼,“*”代替 |
number | 整型數 |
number signed | 帶符號的數,開頭帶符號“-” |
number decimal | 帶小數點的數字 |
number password | 數字密碼 |
DATe time | 時間日期,允許輸入:橫線、斜杠、空格、冒號 |
date | 日期格式 |
time | 時間格式 |
在實際開發當中主要有以下四個方面的基本操作:
1、更換編輯框的光標
- cursorVisible:指定光標是否可見,(代碼中:set cursorvisible)
- textcursorDrawable:指定 光標的圖像,無法在代碼里面設置
2、更換編輯框的邊框
- 邊框通過background屬性設置,隱藏邊框的時候,background=@null
1 <?xml version="1.0" encoding="utf-8"?> 2 <selector xmlns:android="http://schemas.android.com/apk/res/android"> 3 <item android:state_focused="true" android:drawable="@drawable/shape_edit_focus"/> 4 <item android:drawable="@drawable/shape_edit_normal"/> 5 </selector>
上面代碼可以看出,當編輯框獲得焦點時,邊框就會顯示對應的圖形shape_edit_focus,否則顯示,默認的圖形
3、 自動隱藏輸入法
- 獲取文本最大長度
由於Edittext沒有提供獲取最大長度方法,需要用到反射
1 public static int getMaxLength(EditText et) { 2 int length = 0; 3 try { 4 InputFilter[] inputFilters = et.getFilters(); 5 for (InputFilter filter : inputFilters) { 6 Class<?> c = filter.getClass(); 7 if (c.getName().equals("android.text.InputFilter$LengthFilter")) { 8 Field[] f = c.getDeclaredFields(); 9 for (Field field : f) { 10 if (field.getName().equals("mMax")) { 11 field.setAccessible(true); 12 length = (Integer) field.get(filter); 13 } 14 } 15 } 16 } 17 } catch (Exception e) { 18 e.printStackTrace(); 19 } 20 return length; 21 }
- 監控當前輸入的文本長度
需要實現TextWatcher接口,
1 private class HideTextWatcher implements TextWatcher { 2 private EditText mView; 3 private int mMaxLength; 4 private CharSequence mStr; 5 6 public HideTextWatcher(EditText v) { 7 super(); 8 mView = v; 9 mMaxLength = ViewUtil.getMaxLength(v); 10 } 11 12 @Override 13 public void beforeTextChanged(CharSequence s, int start, int count, int after) { 14 } 15 16 @Override 17 public void onTextChanged(CharSequence s, int start, int before, int count) { 18 mStr = s; 19 } 20 21 @Override 22 public void afterTextChanged(Editable s) { 23 if (mStr == null || mStr.length() == 0) 24 return; 25 if (mStr.length() == 11 && mMaxLength == 11) { 26 ViewUtil.hideAllInputMethod(EditHideActivity.this); 27 } 28 if (mStr.length() == 6 && mMaxLength == 6) { 29 ViewUtil.hideOneInputMethod(EditHideActivity.this, mView); 30 } 31 } 32 }
- 關閉軟鍵盤
1 public static void hideAllInputMethod(Activity act) { 2 InputMethodManager imm = (InputMethodManager) act.getSystemService(Context.INPUT_METHOD_SERVICE); 3 //軟鍵盤如果已經打開則關閉之 4 if (imm.isActive() == true) { 5 imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS); 6 } 7 } 8 9 public static void hideOneInputMethod(Activity act, View v) { 10 InputMethodManager imm = (InputMethodManager) act.getSystemService(Context.INPUT_METHOD_SERVICE); 11 imm.hideSoftInputFromWindow(v.getWindowToken(), 0); 12 }
隱藏軟鍵盤測試類:
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 android:id="@+id/ll_hide" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" 6 android:padding="5dp" > 7 8 <View 9 android:layout_width="match_parent" 10 android:layout_height="20dp" /> 11 12 <EditText 13 android:id="@+id/et_phone" 14 style="@style/text_normal" 15 android:hint="輸入11位時自動隱藏輸入法" 16 android:inputType="number" 17 android:maxLength="11" 18 android:background="@drawable/editext_selector" /> 19 20 <View 21 android:layout_width="match_parent" 22 android:layout_height="20dp" /> 23 24 <EditText 25 android:id="@+id/et_password" 26 style="@style/text_normal" 27 android:hint="輸入6位時自動隱藏輸入法" 28 android:inputType="numberPassword" 29 android:maxLength="6" 30 android:background="@drawable/editext_selector" /> 31 32 <View 33 android:layout_width="match_parent" 34 android:layout_height="20dp" /> 35 36 <EditText 37 android:id="@+id/et_other" 38 style="@style/text_normal" 39 android:hint="點擊外部空白處隱藏輸入法" 40 android:inputType="text" 41 android:background="@drawable/editext_selector" /> 42 43 </LinearLayout>
1 package com.example.alimjan.hello_world; 2 3 import android.content.Context; 4 import android.content.Intent; 5 import android.os.Bundle; 6 import android.support.v7.app.AppCompatActivity; 7 import android.text.Editable; 8 import android.text.TextWatcher; 9 import android.view.View; 10 import android.widget.EditText; 11 import android.widget.LinearLayout; 12 13 import com.example.alimjan.hello_world.Utils.ViewUtil; 14 15 /** 16 * Created by alimjan on 7/3/2017. 17 */ 18 19 public class class_3_4_1_3 extends AppCompatActivity implements View.OnClickListener { 20 21 private LinearLayout ll_hide; 22 private EditText et_phone; 23 private EditText et_password; 24 private EditText et_other; 25 26 @Override 27 protected void onCreate(Bundle savedInstanceState) { 28 super.onCreate(savedInstanceState); 29 setContentView(R.layout.code_3_4_1_3); 30 31 ll_hide= (LinearLayout) findViewById(R.id.ll_hide); 32 et_phone= (EditText) findViewById(R.id.et_phone); 33 et_password= (EditText) findViewById(R.id.et_password); 34 et_other= (EditText) findViewById(R.id.et_other); 35 36 ll_hide.setOnClickListener(this); 37 et_phone.addTextChangedListener(new HideTextWatcher(et_phone)); 38 et_password.addTextChangedListener(new HideTextWatcher(et_password)); 39 } 40 41 @Override 42 public void onClick(View v) { 43 if (v.getId() == R.id.ll_hide) { 44 //實際上不只是et_other的軟鍵盤會關閉,其它編輯框的軟鍵盤也會關閉 45 //因為方法內部去獲取視圖的WindowToken,這個Token在每個頁面上都是唯一的 46 ViewUtil.hideOneInputMethod(class_3_4_1_3.this, et_other); 47 } 48 } 49 50 private class HideTextWatcher implements TextWatcher { 51 private EditText mView; 52 private int mMaxLength; 53 private CharSequence mStr; 54 55 public HideTextWatcher(EditText v) { 56 super(); 57 mView = v; 58 mMaxLength = ViewUtil.getMaxLength(v); 59 } 60 61 @Override 62 public void beforeTextChanged(CharSequence s, int start, int count, int after) { 63 } 64 65 @Override 66 public void onTextChanged(CharSequence s, int start, int before, int count) { 67 mStr = s; 68 } 69 70 @Override 71 public void afterTextChanged(Editable s) { 72 if (mStr == null || mStr.length() == 0) 73 return; 74 if (mStr.length() == 11 && mMaxLength == 11) { 75 ViewUtil.hideAllInputMethod(class_3_4_1_3.this); 76 } 77 if (mStr.length() == 6 && mMaxLength == 6) { 78 ViewUtil.hideOneInputMethod(class_3_4_1_3.this, mView); 79 } 80 } 81 } 82 83 public static void startHome(Context mContext) { 84 Intent intent = new Intent(mContext, class_3_4_1_3.class); 85 mContext.startActivity(intent); 86 } 87 }
4、輸入回車符自動跳轉
主要是判斷輸入的內容里面有么有換行符/回車符
1 private class JumpTextWatcher implements TextWatcher { 2 3 private EditText mThisView = null; 4 private View mNextView = null; 5 6 public JumpTextWatcher(EditText vThis, View vNext) { 7 super(); 8 mThisView = vThis; 9 if (vNext != null) { 10 mNextView = vNext; 11 } 12 } 13 14 @Override 15 public void beforeTextChanged(CharSequence s, int start, int count, int after) { 16 } 17 18 @Override 19 public void onTextChanged(CharSequence s, int start, int before, int count) { 20 } 21 22 @Override 23 public void afterTextChanged(Editable s) { 24 String str = s.toString(); 25 //發現輸入回車符或換行符 26 if (str.indexOf("\r") >= 0 || str.indexOf("\n") >= 0) { 27 //去掉回車符和換行符 28 mThisView.setText(str.replace("\r", "").replace("\n", "")); 29 if (mNextView != null) { 30 //讓下一個視圖獲得焦點,即將光標移到下個視圖 31 mNextView.requestFocus(); 32 if (mNextView instanceof EditText) { 33 EditText et = (EditText)mNextView; 34 //讓光標自動移到編輯框內部的文本末尾 35 //方式一:直接調用EditText的setSelection方法 36 et.setSelection(et.getText().length()); 37 //方式二:調用Selection類的setSelection方法 38 //Editable edit = et.getText(); 39 //Selection.setSelection(edit, edit.length()); 40 } 41 } 42 } 43 } 44 }
測試:
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 android:layout_width="match_parent" 3 android:layout_height="match_parent" 4 android:orientation="vertical" 5 android:paddingLeft="20dp" 6 android:paddingRight="20dp" > 7 8 <View 9 android:layout_width="match_parent" 10 android:layout_height="20dp" /> 11 12 <EditText 13 android:id="@+id/et_username" 14 style="@style/text_normal" 15 android:hint="請輸入用戶名" 16 android:inputType="text" 17 android:background="@drawable/editext_selector" /> 18 19 <View 20 android:layout_width="match_parent" 21 android:layout_height="20dp" /> 22 23 <EditText 24 android:id="@+id/et_password" 25 style="@style/text_normal" 26 android:hint="請輸入密碼" 27 android:inputType="textPassword" 28 android:background="@drawable/editext_selector" /> 29 30 <View 31 android:layout_width="match_parent" 32 android:layout_height="20dp" /> 33 34 <Button 35 android:id="@+id/btn_login" 36 style="@style/btn_relative" 37 android:layout_width="match_parent" 38 android:text="登錄" /> 39 40 </LinearLayout>
1 package com.example.alimjan.hello_world; 2 3 import android.content.Context; 4 import android.content.Intent; 5 import android.os.Bundle; 6 import android.support.v7.app.AppCompatActivity; 7 import android.text.Editable; 8 import android.text.TextWatcher; 9 import android.view.View; 10 import android.widget.Button; 11 import android.widget.EditText; 12 import android.widget.Toast; 13 14 /** 15 * Created by alimjan on 7/3/2017. 16 */ 17 18 public class class_3_4_1_4 extends AppCompatActivity implements View.OnClickListener { 19 20 private EditText et_username; 21 private EditText et_password; 22 private Button btn_login; 23 24 @Override 25 protected void onCreate(Bundle savedInstanceState) { 26 super.onCreate(savedInstanceState); 27 setContentView(R.layout.code_3_4_1_4); 28 29 et_username= (EditText) findViewById(R.id.et_username); 30 et_password= (EditText) findViewById(R.id.et_password); 31 btn_login = (Button) findViewById(R.id.btn_login); 32 et_username.addTextChangedListener(new JumpTextWatcher(et_username, et_password)); 33 et_password.addTextChangedListener(new JumpTextWatcher(et_password, btn_login)); 34 btn_login.setOnClickListener(this); 35 } 36 37 @Override 38 public void onClick(View v) { 39 if (v.getId() == R.id.btn_login) { 40 Toast.makeText(this, "這個登錄按鈕啥事也沒做", Toast.LENGTH_SHORT).show(); 41 } 42 } 43 44 private class JumpTextWatcher implements TextWatcher { 45 46 private EditText mThisView = null; 47 private View mNextView = null; 48 49 public JumpTextWatcher(EditText vThis, View vNext) { 50 super(); 51 mThisView = vThis; 52 if (vNext != null) { 53 mNextView = vNext; 54 } 55 } 56 57 @Override 58 public void beforeTextChanged(CharSequence s, int start, int count, int after) { 59 } 60 61 @Override 62 public void onTextChanged(CharSequence s, int start, int before, int count) { 63 } 64 65 @Override 66 public void afterTextChanged(Editable s) { 67 String str = s.toString(); 68 //發現輸入回車符或換行符 69 if (str.indexOf("\r") >= 0 || str.indexOf("\n") >= 0) { 70 //去掉回車符和換行符 71 mThisView.setText(str.replace("\r", "").replace("\n", "")); 72 if (mNextView != null) { 73 //讓下一個視圖獲得焦點,即將光標移到下個視圖 74 mNextView.requestFocus(); 75 if (mNextView instanceof EditText) { 76 EditText et = (EditText)mNextView; 77 //讓光標自動移到編輯框內部的文本末尾 78 //方式一:直接調用EditText的setSelection方法 79 et.setSelection(et.getText().length()); 80 //方式二:調用Selection類的setSelection方法 81 //Editable edit = et.getText(); 82 //Selection.setSelection(edit, edit.length()); 83 } 84 } 85 } 86 } 87 } 88 89 public static void startHome(Context mContext) { 90 Intent intent = new Intent(mContext, class_3_4_1_4.class); 91 mContext.startActivity(intent); 92 } 93 94 }