常用属性:
- 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(" ") >= 0 || str.indexOf(" ") >= 0) { 27 //去掉回车符和换行符 28 mThisView.setText(str.replace(" ", "").replace(" ", "")); 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(" ") >= 0 || str.indexOf(" ") >= 0) { 70 //去掉回车符和换行符 71 mThisView.setText(str.replace(" ", "").replace(" ", "")); 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 }