溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

EditText怎么在Android中使用

發(fā)布時(shí)間:2021-05-06 16:27:24 來源:億速云 閱讀:177 作者:Leah 欄目:開發(fā)技術(shù)

本篇文章給大家分享的是有關(guān)EditText怎么在Android中使用,小編覺得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。

Android是什么

Android是一種基于Linux內(nèi)核的自由及開放源代碼的操作系統(tǒng),主要使用于移動(dòng)設(shè)備,如智能手機(jī)和平板電腦,由美國(guó)Google公司和開放手機(jī)聯(lián)盟領(lǐng)導(dǎo)及開發(fā)。

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
       
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:background="@null"
       android:hint="輸入用戶名"
       android:paddingBottom="5dip"
       android:paddingTop="5dip" />

2、讓軟鍵盤出現(xiàn)搜索按鈕

EditText怎么在Android中使用

EditText怎么在Android中使用

  • 核心代碼塊1:

這倆個(gè)一定要設(shè)置,要不然軟鍵盤不會(huì)出現(xiàn)搜索

android:imeOptions="actionSearch"
                android:singleLine="true"
  • 核心代碼塊2:

Activity或者Fragment 要實(shí)現(xiàn)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:
                //隱藏項(xiàng)目中彈框
                hideSoftInputMethod();

                //項(xiàng)目中個(gè)性化操作
                getEditTextValue();
                pageno = 1;
                getMedicineListInfoForApp(name,firstWord,type,level,pageno);
                break;
            default:
                break;
        }
    }

}

3、多行EditText的時(shí)候會(huì)出現(xiàn)光標(biāo)在中間的問題:

關(guān)鍵代碼

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默認(rèn)是gravity是center.就是從中間對(duì)齊。我們把他改成left啊top啊就OK了。

4、修改EditText的光標(biāo)顏色

在使用EditText的XML 文件中加入一個(gè)屬性:

android:textCursorDrawable="@null"
//或者
android:textCursorDrawable = "#fff000"

這個(gè)屬性是用來控制光標(biāo)顏色的,"@null" 是作用是讓光標(biāo)顏色和text color一樣,當(dāng)然也可以修改成你自己的顏色。

5、通過監(jiān)聽OnFocusChangeListener判斷EditText的焦點(diǎn)與否

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來對(duì)文本內(nèi)容的呈現(xiàn)做說明

 android:ellipsize="end"

EditText怎么在Android中使用

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;
    }

以上就是EditText怎么在Android中使用,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見到或用到的。希望你能通過這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI