常用属性:
- 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
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:drawable="@drawable/shape_edit_focus"/>
<item android:drawable="@drawable/shape_edit_normal"/>
</selector>
上面代码可以看出,当编辑框获得焦点时,边框就会显示对应的图形shape_edit_focus,否则显示,默认的图形
3、 自动隐藏输入法
- 获取文本最大长度
由于Edittext没有提供获取最大长度方法,需要用到反射
public static int getMaxLength(EditText et) {
int length = 0;
try {
InputFilter[] inputFilters = et.getFilters();
for (InputFilter filter : inputFilters) {
Class<?> c = filter.getClass();
if (c.getName().equals("android.text.InputFilter$LengthFilter")) {
Field[] f = c.getDeclaredFields();
for (Field field : f) {
if (field.getName().equals("mMax")) {
field.setAccessible(true);
length = (Integer) field.get(filter);
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return length;
}
- 监控当前输入的文本长度
需要实现TextWatcher接口,
private class HideTextWatcher implements TextWatcher {
private EditText mView;
private int mMaxLength;
private CharSequence mStr; public HideTextWatcher(EditText v) {
super();
mView = v;
mMaxLength = ViewUtil.getMaxLength(v);
} @Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
} @Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
mStr = s;
} @Override
public void afterTextChanged(Editable s) {
if (mStr == null || mStr.length() == 0)
return;
if (mStr.length() == 11 && mMaxLength == 11) {
ViewUtil.hideAllInputMethod(EditHideActivity.this);
}
if (mStr.length() == 6 && mMaxLength == 6) {
ViewUtil.hideOneInputMethod(EditHideActivity.this, mView);
}
}
}
- 关闭软键盘
public static void hideAllInputMethod(Activity act) {
InputMethodManager imm = (InputMethodManager) act.getSystemService(Context.INPUT_METHOD_SERVICE);
//软键盘如果已经打开则关闭之
if (imm.isActive() == true) {
imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
}
} public static void hideOneInputMethod(Activity act, View v) {
InputMethodManager imm = (InputMethodManager) act.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
隐藏软键盘测试类:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ll_hide"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="5dp" > <View
android:layout_width="match_parent"
android:layout_height="20dp" /> <EditText
android:id="@+id/et_phone"
style="@style/text_normal"
android:hint="输入11位时自动隐藏输入法"
android:inputType="number"
android:maxLength="11"
android:background="@drawable/editext_selector" /> <View
android:layout_width="match_parent"
android:layout_height="20dp" /> <EditText
android:id="@+id/et_password"
style="@style/text_normal"
android:hint="输入6位时自动隐藏输入法"
android:inputType="numberPassword"
android:maxLength="6"
android:background="@drawable/editext_selector" /> <View
android:layout_width="match_parent"
android:layout_height="20dp" /> <EditText
android:id="@+id/et_other"
style="@style/text_normal"
android:hint="点击外部空白处隐藏输入法"
android:inputType="text"
android:background="@drawable/editext_selector" /> </LinearLayout>
package com.example.alimjan.hello_world; import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout; import com.example.alimjan.hello_world.Utils.ViewUtil; /**
* Created by alimjan on 7/3/2017.
*/ public class class_3_4_1_3 extends AppCompatActivity implements View.OnClickListener { private LinearLayout ll_hide;
private EditText et_phone;
private EditText et_password;
private EditText et_other; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.code_3_4_1_3); ll_hide= (LinearLayout) findViewById(R.id.ll_hide);
et_phone= (EditText) findViewById(R.id.et_phone);
et_password= (EditText) findViewById(R.id.et_password);
et_other= (EditText) findViewById(R.id.et_other); ll_hide.setOnClickListener(this);
et_phone.addTextChangedListener(new HideTextWatcher(et_phone));
et_password.addTextChangedListener(new HideTextWatcher(et_password));
} @Override
public void onClick(View v) {
if (v.getId() == R.id.ll_hide) {
//实际上不只是et_other的软键盘会关闭,其它编辑框的软键盘也会关闭
//因为方法内部去获取视图的WindowToken,这个Token在每个页面上都是唯一的
ViewUtil.hideOneInputMethod(class_3_4_1_3.this, et_other);
}
} private class HideTextWatcher implements TextWatcher {
private EditText mView;
private int mMaxLength;
private CharSequence mStr; public HideTextWatcher(EditText v) {
super();
mView = v;
mMaxLength = ViewUtil.getMaxLength(v);
} @Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
} @Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
mStr = s;
} @Override
public void afterTextChanged(Editable s) {
if (mStr == null || mStr.length() == 0)
return;
if (mStr.length() == 11 && mMaxLength == 11) {
ViewUtil.hideAllInputMethod(class_3_4_1_3.this);
}
if (mStr.length() == 6 && mMaxLength == 6) {
ViewUtil.hideOneInputMethod(class_3_4_1_3.this, mView);
}
}
} public static void startHome(Context mContext) {
Intent intent = new Intent(mContext, class_3_4_1_3.class);
mContext.startActivity(intent);
}
}
4、输入回车符自动跳转
主要是判断输入的内容里面有么有换行符/回车符
private class JumpTextWatcher implements TextWatcher { private EditText mThisView = null;
private View mNextView = null; public JumpTextWatcher(EditText vThis, View vNext) {
super();
mThisView = vThis;
if (vNext != null) {
mNextView = vNext;
}
} @Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
} @Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
} @Override
public void afterTextChanged(Editable s) {
String str = s.toString();
//发现输入回车符或换行符
if (str.indexOf("\r") >= 0 || str.indexOf("\n") >= 0) {
//去掉回车符和换行符
mThisView.setText(str.replace("\r", "").replace("\n", ""));
if (mNextView != null) {
//让下一个视图获得焦点,即将光标移到下个视图
mNextView.requestFocus();
if (mNextView instanceof EditText) {
EditText et = (EditText)mNextView;
//让光标自动移到编辑框内部的文本末尾
//方式一:直接调用EditText的setSelection方法
et.setSelection(et.getText().length());
//方式二:调用Selection类的setSelection方法
//Editable edit = et.getText();
//Selection.setSelection(edit, edit.length());
}
}
}
}
}
测试:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingLeft="20dp"
android:paddingRight="20dp" > <View
android:layout_width="match_parent"
android:layout_height="20dp" /> <EditText
android:id="@+id/et_username"
style="@style/text_normal"
android:hint="请输入用户名"
android:inputType="text"
android:background="@drawable/editext_selector" /> <View
android:layout_width="match_parent"
android:layout_height="20dp" /> <EditText
android:id="@+id/et_password"
style="@style/text_normal"
android:hint="请输入密码"
android:inputType="textPassword"
android:background="@drawable/editext_selector" /> <View
android:layout_width="match_parent"
android:layout_height="20dp" /> <Button
android:id="@+id/btn_login"
style="@style/btn_relative"
android:layout_width="match_parent"
android:text="登录" /> </LinearLayout>
package com.example.alimjan.hello_world; import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast; /**
* Created by alimjan on 7/3/2017.
*/ public class class_3_4_1_4 extends AppCompatActivity implements View.OnClickListener { private EditText et_username;
private EditText et_password;
private Button btn_login; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.code_3_4_1_4); et_username= (EditText) findViewById(R.id.et_username);
et_password= (EditText) findViewById(R.id.et_password);
btn_login = (Button) findViewById(R.id.btn_login);
et_username.addTextChangedListener(new JumpTextWatcher(et_username, et_password));
et_password.addTextChangedListener(new JumpTextWatcher(et_password, btn_login));
btn_login.setOnClickListener(this);
} @Override
public void onClick(View v) {
if (v.getId() == R.id.btn_login) {
Toast.makeText(this, "这个登录按钮啥事也没做", Toast.LENGTH_SHORT).show();
}
} private class JumpTextWatcher implements TextWatcher { private EditText mThisView = null;
private View mNextView = null; public JumpTextWatcher(EditText vThis, View vNext) {
super();
mThisView = vThis;
if (vNext != null) {
mNextView = vNext;
}
} @Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
} @Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
} @Override
public void afterTextChanged(Editable s) {
String str = s.toString();
//发现输入回车符或换行符
if (str.indexOf("\r") >= 0 || str.indexOf("\n") >= 0) {
//去掉回车符和换行符
mThisView.setText(str.replace("\r", "").replace("\n", ""));
if (mNextView != null) {
//让下一个视图获得焦点,即将光标移到下个视图
mNextView.requestFocus();
if (mNextView instanceof EditText) {
EditText et = (EditText)mNextView;
//让光标自动移到编辑框内部的文本末尾
//方式一:直接调用EditText的setSelection方法
et.setSelection(et.getText().length());
//方式二:调用Selection类的setSelection方法
//Editable edit = et.getText();
//Selection.setSelection(edit, edit.length());
}
}
}
}
} public static void startHome(Context mContext) {
Intent intent = new Intent(mContext, class_3_4_1_4.class);
mContext.startActivity(intent);
} }