設(shè)置單行,多行,自動換行,文字間距參考TextView
在EditText設(shè)置單行,由于android:single屬性顯示過時,提示用使用android:maxLines=”1”代替,但是設(shè)置之后沒有任何效果。這是由于android:inputType屬性默認(rèn)值為none的緣故.只要將android:inputType屬性設(shè)置為其他就可以了,TextView應(yīng)該也是一樣吧
設(shè)置默認(rèn)提示文本
默認(rèn)提示文本的兩個屬性如下:
android:hint="默認(rèn)提示文本"
android:textColorHint="#95A1AA"
前者設(shè)置提示的文本內(nèi)容,后者設(shè)置提示文本的顏色!
設(shè)置光標(biāo)顏色
在drawable文件夾下寫個edit_cursor_color.xml文件
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<size android:width="2dp" />
<solid android:color="#ff7200" />
</shape>
然后在xml中設(shè)置
android:textCursorDrawable=”@drawable/edit_cursor_color”
全局設(shè)置
在AndroidManifest.xml中找到引用的主題
<application
android:allowBackup="true"
android:icon="@mipmap/liyin_logo"
android:label="@string/app_name"
android:name=".appllication.MyAppBinder"
android:supportsRtl="true"
android:theme="@style/AppTheme">
在該主題下添加
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="windowNoTitle">true</item>
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:textCursorDrawable">@drawable/basic_edit_text_cursor</item><!--設(shè)置光標(biāo)樣式-->
</style>
設(shè)置顯示和隱藏密碼
EditText 密碼顯示隱藏三種方法
//顯示密碼
//1、
EditText.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
//2、
EditText.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
//3、
EditText.setInputType(0x90);
//隱藏密碼
//1、
EditText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
//2、
EditText.setTransformationMethod(PasswordTransformationMethod.getInstance());
//3、
EditText.setInputType(0x81);
第一種方法存在設(shè)置完一次之后,再次設(shè)置無效果的情況,推薦使用第二種方法
光標(biāo)位置的控制
有時可能需要我們控制EditText中的光標(biāo)移動到指定位置或者選中某些文本!EditText為我們提供了setSelection()的方法,方法有兩種形式:
一個參數(shù)的是設(shè)置光標(biāo)位置的,兩個參數(shù)的是設(shè)置起始位置與結(jié)束位置的中間括的部分,即部分選中!
當(dāng)然我們也可以調(diào)用setSelectAllOnFocus(true);讓EditText獲得焦點(diǎn)時選中全部文本!
另外我們還可以調(diào)用setCursorVisible(false);設(shè)置光標(biāo)不顯示
還可以調(diào)用getSelectionStart()和getSelectionEnd獲得當(dāng)前光標(biāo)的前后位置
login_user_pwd_edt.setSelection(login_user_pwd_edt.getText().toString().length());//設(shè)置光標(biāo)在最后
EditText獲取焦點(diǎn) 失去焦點(diǎn)監(jiān)聽,獲取焦點(diǎn)操作
實(shí)現(xiàn)方法也很簡單、那就是綁定OnFocusChangeListener事件、實(shí)現(xiàn)onFocusChange(View v, boolean hasFocus) 方法、第二個參數(shù)就是判斷得到焦點(diǎn)或失去焦點(diǎn)、從而實(shí)現(xiàn)我得想要的效果、代碼如下
EditText searchView = (EditText) findViewById(R.id.search_text);
searchView.setOnFocusChangeListener(new android.view.View.
OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
// 此處為得到焦點(diǎn)時的處理內(nèi)容
} else {
// 此處為失去焦點(diǎn)時的處理內(nèi)容
}
}
});
文本框重新獲取焦點(diǎn)方法:
searchView.setFocusable(true);
searchView.setFocusableInTouchMode(true);
searchView.requestFocus();
searchView.clearFocus();//失去焦點(diǎn)
searchView.requestFocus();//獲取焦點(diǎn)
【Android】EditText獲得焦點(diǎn)以及失去焦點(diǎn)
Android 設(shè)置Edittext獲取焦點(diǎn)并彈出軟鍵盤
獲得焦點(diǎn)后全選組件內(nèi)所有文本內(nèi)容
當(dāng)我們想在點(diǎn)擊輸入框獲得焦點(diǎn)后,不是將光標(biāo)移動到文本的開始或者結(jié)尾;而是 獲取到輸入框中所有的文本內(nèi)容的話!這個時候我們可以使用selectAllOnFocus屬性
android:selectAllOnFocus="true"
設(shè)置英文字母大寫類型
EditText還為我們提供了設(shè)置英文字母大寫類型的屬性:
android:capitalize 默認(rèn)none,提供了三個可選值:
- sentences:僅第一個字母大寫
- words:每一個單詞首字母大小,用空格區(qū)分單詞
- characters:每一個英文字母都大寫
EditText輸入錯誤時該怎樣提示用戶
- 使用Toast提示
Toast. makeText ( this, "郵箱格式不正確", Toast. LENGTH_LONG ). show ( ) ;
- 使用EditText的setError提示
EditText et = (EditText ) findViewById (R. id. etTest ) ;
et. setError ( "有錯誤提示" ) ;
PS:在EditText的右邊使用TextView來提示,類似于網(wǎng)頁中注冊頁面。需要注意的是EditText必須獲得焦點(diǎn)才會彈出錯誤提示框。
- 自定義圖標(biāo)的setError提示
Drawable d = getResources ( ). getDrawable (R. drawable. ic_launcher ) ;
d. setBounds ( 0, 0, 30, 30 ) ; //必須設(shè)置大小,否則不顯示
et. setError ( "有錯誤提示", d ) ;
限制輸入類型和軟鍵盤彈出類型
限制輸入類型
Android之設(shè)置EditText輸入類型(setInputType()方法和android:inputType屬性)
EditText中輸入信息的限制的方法
代碼 :et_lxnr.setInputType(InputType.TYPE_TEXT_VARIATION_LONG_MESSAGE);
xml :
android:inputType="number" //設(shè)置輸入為數(shù)字,彈出數(shù)字鍵盤
android:inputType="textPassword" //設(shè)置輸入為密碼,不顯示明文
限制輸入長度(如限制輸入最大長度10)
代碼:et_lxnr.setFilters(new InputFilter[]{new InputFilter.LengthFilter(10)});
xml:android:maxLength="10"
限制輸入固定的某些字符(如123456xyz)
代碼:et_lxnr.setKeyListener(DigitsKeyListener.getInstance(“123456xyz”);
xml:android:digits="@string/input_num_character"
示例
//設(shè)置只能輸入字母數(shù)字
login_user_pwd_edt.setKeyListener(DigitsKeyListener.getInstance(getString(R.string.filter_vcode_pwd)));
<string name="filter_vcode_pwd">0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ</string>
TextWatcher(監(jiān)聽輸入變化)
addTextChangedListener(文本監(jiān)聽)參數(shù)解釋及實(shí)現(xiàn)EditText字?jǐn)?shù)監(jiān)聽
Android TextWatcher三個回調(diào)詳解,監(jiān)聽EditText的輸入
Android 文本監(jiān)聽接口TextWatcher詳解
EditText限制最大字符數(shù)(中文,字母,數(shù)字)
自定義TextWatcher,實(shí)現(xiàn)輸入超出最大長度時提示的功能。
public class LengthTextWatcher implements TextWatcher {
private EditText editText = null;
private ImageView imageView = null;
private int editStart;
private int editEnd;
private int maxLen;//最大長度
private String toast;//超出最大長度時的提示語
/**
* @param editText
* @param maxLen
* @param toast
*/
public LengthTextWatcher(EditText editText, int maxLen, String toast) {
this.editText = editText;
this.maxLen = maxLen;
this.toast = toast;
}
/**
* @param editText
* @param imageView 清空圖標(biāo)
* @param maxLen
* @param toast
*/
public LengthTextWatcher(EditText editText, ImageView imageView, int maxLen, String toast) {
this.editText = editText;
this.imageView = imageView;
this.maxLen = maxLen;
this.toast = toast;
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
Logger.e("s=>>>>>>>>>>>" + s);
if (calculateLength(s.toString()) > maxLen && toast.length() > 0)
RxToast.normal(toast);
}
@Override
public void afterTextChanged(Editable s) {
editStart = editText.getSelectionStart();//得到光標(biāo)的前后位置
editEnd = editText.getSelectionEnd();
// 先去掉監(jiān)聽器,否則會出現(xiàn)棧溢出
editText.removeTextChangedListener(this);
if (!TextUtils.isEmpty(editText.getText())) {
//刪除超出的部分
while (calculateLength(s.toString()) > maxLen) {
s.delete(editStart - 1, editEnd);
editStart--;
editEnd--;
}
}
editText.setText(s);
editText.setSelection(editStart);//設(shè)置光標(biāo)在句尾
// 恢復(fù)監(jiān)聽器
editText.addTextChangedListener(this);
if (imageView != null) {
if (TextUtils.isEmpty(s)) {
imageView.setVisibility(View.GONE);
} else {
imageView.setVisibility(View.VISIBLE);
}
}
}
/**
* 計算字符數(shù),中文算2個,英文1個
*
* @param etstring
* @return
*/
private int calculateLength(String etstring) {
char[] ch = etstring.toCharArray();
int varlength = 0;
for (int i = 0; i < ch.length; i++) {
// changed by zyf 0825 , bug 6918,加入中文標(biāo)點(diǎn)范圍 , TODO 標(biāo)點(diǎn)范圍有待具體化
if ((ch[i] >= 0x2E80 && ch[i] <= 0xFE4F) || (ch[i] >= 0xA13F && ch[i] <= 0xAA40) || ch[i] >= 0x80) { // 中文字符范圍0x4e00 0x9fbb
varlength = varlength + 2;
} else {
varlength++;
}
}
// 這里也可以使用getBytes,更準(zhǔn)確嘛
// varlength = etstring.getBytes(CharSet.forName("GBK")).lenght;// 編碼根據(jù)自己的需求,注意u8中文占3個字節(jié)...
return varlength;
}
}
調(diào)用
et_name.addTextChangedListener(new LengthTextWatcher(et_name, iv_clear_name, 16, ""));
InputFilter過濾器
1、Android--?InputFilter詳解(用例:只能輸入中文, 只能輸入數(shù)字, 只能輸入字母, 最大長度限制等)
https://blog.csdn.net/angcyo/article/details/81566066
2、Android開發(fā)之EditText字符串過濾器InputFilter
https://blog.csdn.net/talentclass_ctt/article/details/54773340
3、InputFilter詳解、TextWatcher詳解
https://blog.csdn.net/u014606081/article/details/53101629
示例:
public class CharInputFilter implements InputFilter {
//允許中文輸入
public boolean IS_INPUT_CHINESE = false;
//允許輸入大小寫字母
public boolean IS_INPUT_LETTER = false;
//允許輸入數(shù)字
public boolean IS_INPUT_NUMBER = false;
//允許輸入Ascii碼表的[33-126]的字符,包含數(shù)字字母和特殊符號
public boolean IS_INPUT_ASCII_CHAR = false;
//限制輸入的最大字符數(shù), 小于0不限制
private int maxInputLength = -1;
public CharInputFilter() {
}
public void setIS_INPUT_CHINESE(boolean IS_INPUT_CHINESE) {
this.IS_INPUT_CHINESE = IS_INPUT_CHINESE;
}
public void setIS_INPUT_LETTER(boolean IS_INPUT_LETTER) {
this.IS_INPUT_LETTER = IS_INPUT_LETTER;
}
public void setIS_INPUT_NUMBER(boolean IS_INPUT_NUMBER) {
this.IS_INPUT_NUMBER = IS_INPUT_NUMBER;
}
public void setIS_INPUT_ASCII_CHAR(boolean IS_INPUT_ASCII_CHAR) {
this.IS_INPUT_ASCII_CHAR = IS_INPUT_ASCII_CHAR;
}
public void setMaxInputLength(int maxInputLength) {
this.maxInputLength = maxInputLength;
}
/**
* 是否是中文
*/
public static boolean isChinese(char c) {
Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
|| ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
|| ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
|| ub == Character.UnicodeBlock.GENERAL_PUNCTUATION
|| ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
|| ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) {
return true;
}
return false;
}
/**
* 是否是大小寫字母
*/
public static boolean isCharLetter(char c) {
// Allow [a-zA-Z]
if ('a' <= c && c <= 'z')
return true;
if ('A' <= c && c <= 'Z')
return true;
return false;
}
public static boolean isNumber(char c) {
return ('0' <= c && c <= '9');
}
public static boolean isAsciiChar(char c) {
return (33 <= c && c <= 126);
}
/**
* @param source 本次需要更新的字符串, (可以理解為輸入法輸入的字符,比如:我是文本)
* @param start 取 source 字符串的開始位置,通常是0
* @param end 取 source 字符串的結(jié)束位置,通常是source.length()
* @param dest 原始字符串
* @param dstart 原始字符串開始的位置
* @param dend 原始字符串結(jié)束的位置, 這種情況會在你已經(jīng)選中了很多個字符, 然后用輸入法輸入字符的情況下
* @return
*/
@Override
public CharSequence filter(CharSequence source, int start, int end,
Spanned dest, int dstart, int dend) {
//此次操作后, 原來的字符數(shù)量
int length = dest.length() - (dend - dstart);
if (maxInputLength > 0) {
if (length == maxInputLength) {
return "";
}
}
SpannableStringBuilder modification = new SpannableStringBuilder();
for (int i = start; i < end; i++) {
char c = source.charAt(i);
boolean append = false;
if (IS_INPUT_CHINESE) {
append = isChinese(c) || append;
}
if (IS_INPUT_LETTER) {
append = isCharLetter(c) || append;
}
if (IS_INPUT_NUMBER) {
append = isNumber(c) || append;
}
if (IS_INPUT_ASCII_CHAR) {
append = isAsciiChar(c) || append;
}
if (append) {
modification.append(c);
}
}
if (maxInputLength > 0) {
int newLength = length + modification.length();
if (newLength > maxInputLength) {
//越界
modification.delete(maxInputLength - length, modification.length());
}
}
return modification;//返回修改后, 允許輸入的字符串. 返回null, 由系統(tǒng)處理.
}
}
遇到的問題
用InputFilter限制輸入長度時,在一些手機(jī)(如LG)的英文輸入法輸入時會莫名多出一些字符等問題:
關(guān)于android 中EditText 特殊字符過濾和字符長度限制的最優(yōu)方法