Android使用EditText小技巧匯總
1、隱藏android中EditText自帶的的下劃線
android:background="@null" 或android:background="@/drawable/bg_edittext_norma.xml"
bg_edittext_norma.xml
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<!--商品描述的可編輯框-->
<solid android:color="#FFFFFF" />
<corners android:radius="10dip"/>
<stroke
android:width="1dip"
android:color="#BDC7D8" />
</shape>
<EditText
style="?android:attr/textViewStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@null"
android:hint="輸入用戶名"
android:paddingBottom="5dip"
android:paddingTop="5dip" />
2、讓軟鍵盤出現搜索按鈕


- 核心代碼塊1:
這倆個一定要設置,要不然軟鍵盤不會出現搜索
android:imeOptions="actionSearch"
android:singleLine="true"
- 核心代碼塊2:
Activity或者Fragment 要實現TextView.OnEditorActionListener接口
public class DrugCatalogueInquiryFragment extends GeneralSocialFragment implements TextView.OnEditorActionListener {
private ClearEditText etDrugName;
etDrugName = xFindViewById(R.id.et_drug_name);
etDrugName.setOnEditorActionListener(this);
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
doWhichOperation(actionId);
return true;
}
private void doWhichOperation(int actionId) {
switch (actionId) {
case EditorInfo.IME_ACTION_SEARCH:
//隱藏項目中彈框
hideSoftInputMethod();
//項目中個性化操作
getEditTextValue();
pageno = 1;
getMedicineListInfoForApp(name,firstWord,type,level,pageno);
break;
default:
break;
}
}
}
3、多行EditText的時候會出現光標在中間的問題:
關鍵代碼
android:gravity="left"
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minLines="5"
android:background="#ffffff"
android:paddingLeft="5dp"
android:gravity="left" />
像這種。這是什么原因造成的呢?用來EdittText默認是gravity是center.就是從中間對齊。我們把他改成left啊top啊就OK了。
4、修改EditText的光標顏色
在使用EditText的XML 文件中加入一個屬性:
android:textCursorDrawable="@null" //或者 android:textCursorDrawable = "#fff000"
這個屬性是用來控制光標顏色的,"@null" 是作用是讓光標顏色和text color一樣,當然也可以修改成你自己的顏色。
5、通過監(jiān)聽OnFocusChangeListener判斷EditText的焦點與否
private void initListener(){
etDrugName.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean b) {
if (b){
TypeUtils.getInstance( getActivity() ).hideKeyboardView();
}
}
});
etDrugNameOfInitial.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean b) {
if (b){
TypeUtils.getInstance( getActivity() ).hideKeyboardView();
}
}
});
}
6、通過屬性android:ellipsize來對文本內容的呈現做說明
android:ellipsize="end"

7、通過屬性android:digits來規(guī)定只能輸入的值
android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
8、規(guī)定只能輸入中文
/**
* 通過使用Android源碼中的InputFilter接口
*/
InputFilter filter = new InputFilter() {
public CharSequence filter(CharSequence source, int start, int end,
Spanned dest, int dstart, int dend) {
for (int i = start; i < end; i++) {
if (!isChinese(source.charAt(i))) {
return "";
}
}
return null;
}
};
/**
* 判定輸入漢字
*
* @param c
* @return
*/
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;
}
到這里就結束啦。
以上就是Android使用EditText小技巧匯總的詳細內容,更多關于Android使用EditText的資料請關注腳本之家其它相關文章!
- Android開發(fā)EditText實現密碼顯示隱藏
- Android開發(fā)EditText禁止輸入監(jiān)聽及InputFilter字符過濾
- Android中EditText光標的顯示與隱藏方法
- Android TextWatcher三個回調以及監(jiān)聽EditText的輸入案例詳解
- Android EditText輸入框實現下拉且保存最近5個歷史記錄思路詳解
- Android EditText隨輸入法一起移動并懸浮在輸入法之上的示例代碼
- Android EditText每4位自動添加空格效果
- Android編輯框EditText與焦點變更監(jiān)視器及文本變化監(jiān)視器實現流程詳解
相關文章
Android入門之在SharedPreference中使用加密
這篇文章主要為大家詳細介紹了Android如何使在SharedPreference中使用加密,文中的示例代碼講解詳細,對我們學習Android有一定的幫助,需要的可以參考一下2022-12-12
Android開發(fā)Flutter?桌面應用窗口化實戰(zhàn)示例
這篇文章主要為大家介紹了Android開發(fā)Flutter?桌面應用窗口化實戰(zhàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-09-09

