欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android編輯框EditText與焦點(diǎn)變更監(jiān)視器及文本變化監(jiān)視器實(shí)現(xiàn)流程詳解

 更新時(shí)間:2022年09月27日 14:20:11   作者:Shewyoo  
這篇文章主要介紹了Android編輯框EditText與焦點(diǎn)變更監(jiān)視器及文本變化監(jiān)視器實(shí)現(xiàn)流程,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧

一、編輯框EditText

編輯框用于接收鍵盤輸入的文字,由文本視圖派生而來(lái),除了TextView已有的各種屬性和方法,EditText還支持下列XML屬性:

  • inputType:指定輸入的文本類型,輸入類型的取值說(shuō)明如下表,若同時(shí)使用多種文本類型,則可使用豎線“|”把多種文本類型拼接起來(lái)。
  • maxLength:指定文本允許輸入的最大長(zhǎng)度。
  • hint:指定提示文本的內(nèi)容。
  • textColorHint:指定提示文本的顏色。
輸入類型說(shuō)明
text文本
textPassword文本密碼,顯示時(shí)用圓點(diǎn)代替
number整型數(shù)
numberSigned帶符號(hào)的數(shù)字,允許在開(kāi)頭帶符號(hào)”-“
numberPassword數(shù)字密碼,顯示時(shí)用圓點(diǎn)代替
datetime時(shí)間日期格式,除了數(shù)字外,還允許輸入橫線、斜杠、空格、冒號(hào)
date日期格式,除了數(shù)字外,還允許輸入橫線”-“和斜杠”/“
time時(shí)間格式,除了數(shù)字外,還允許輸入冒號(hào)”:“
numberDecimal帶小數(shù)點(diǎn)的數(shù)字

例1:輸入用戶名和密碼

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="text"
        android:maxLength="10"
        android:hint="請(qǐng)輸入用戶名"
        android:textColor="@color/black"
        android:textSize="17sp"/>
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="numberPassword"
        android:maxLength="10"
        android:hint="請(qǐng)輸入密碼"
        android:textColor="@color/black"
        android:textSize="17sp"/>

例2:自定義輸入框樣式:無(wú)邊框和圓角邊框

drawable下新建兩個(gè)xml文件用于配置樣式

第一個(gè):選中時(shí)的樣式

<shape xmlns:android="http://schemas.android.com/apk/res/android">
<!--指定形狀內(nèi)的填充顏色-->
    <solid android:color="#ffffff"/>
<!--    指定形狀輪廓的粗細(xì)與顏色-->
    <stroke
        android:width="1dp"
        android:color="#0000ff"/>
<!--    指定形狀四個(gè)圓角的半徑-->
    <corners android:radius="5dp"/>
<!--    指定形狀四個(gè)方向的間距-->
    <padding
        android:bottom="2dp"
        android:left="2dp"
        android:right="2dp"
        android:top="2dp"/>
</shape>

第二個(gè):正常樣式

<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <!--指定形狀內(nèi)的填充顏色-->
    <solid android:color="#ffffff"/>
    <!--    指定形狀輪廓的粗細(xì)與顏色-->
    <stroke
        android:width="1dp"
        android:color="#aaaaaa"/>
    <!--    指定形狀四個(gè)圓角的半徑-->
    <corners android:radius="5dp"/>
    <!--    指定形狀四個(gè)方向的間距-->
    <padding
        android:bottom="2dp"
        android:left="2dp"
        android:right="2dp"
        android:top="2dp"/>
</shape>

再在drawable文件下新建一個(gè)xml文件用于配置選中時(shí)和未選中時(shí)的樣式。

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/edit_focus"
        android:state_focused="true"/>
    <item android:drawable="@drawable/edit_normal"/>
</selector>

Activity的XML文件

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@null"
        android:layout_marginTop="10dp"
        android:hint="無(wú)邊框"
        android:inputType="text"/>
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/edit_text"
        android:layout_marginTop="10dp"
        android:hint="圓角邊框"
        android:inputType="text"/>

二、焦點(diǎn)變更監(jiān)視器

調(diào)用編輯框?qū)ο蟮膕etOnFocusChangeListener方法,即可在光標(biāo)切換時(shí)觸發(fā)焦點(diǎn)變更事件。

使用場(chǎng)景如:手機(jī)號(hào)碼未輸滿11位,就點(diǎn)擊密碼框,此時(shí)校驗(yàn)不通過(guò),一邊彈出提示文字,一邊把焦點(diǎn)拉回手機(jī)框。

例:當(dāng)手機(jī)號(hào)碼不足11位時(shí)點(diǎn)擊密碼框會(huì)出現(xiàn)提示。

注意:不可采取這樣的方式:為密碼框綁定點(diǎn)擊事件,當(dāng)點(diǎn)擊密碼框時(shí)檢測(cè)是否通過(guò)。

原因:編輯框點(diǎn)擊兩次后才會(huì)觸發(fā)點(diǎn)擊事件,第一次點(diǎn)擊只觸發(fā)焦點(diǎn)變更事件,第二次點(diǎn)擊才觸發(fā)點(diǎn)擊事件。

XML文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <EditText
        android:id="@+id/et_phone"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/edit_text"
        android:layout_marginTop="10dp"
        android:hint="請(qǐng)輸入11位手機(jī)號(hào)碼"
        android:inputType="number"
        android:maxLength="11"/>
    <EditText
        android:id="@+id/et_password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/edit_text"
        android:layout_marginTop="10dp"
        android:hint="請(qǐng)輸入密碼"
        android:inputType="numberPassword"
        android:maxLength="11"/>
    <Button
        android:id="@+id/btn_login"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="登錄"/>
</LinearLayout>

java類

public class EditFocusActivity extends AppCompatActivity implements View.OnFocusChangeListener {
    private EditText et_phone;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_focus);
        et_phone = findViewById(R.id.et_phone);
        EditText et_password = findViewById(R.id.et_password);
        et_password.setOnFocusChangeListener(this);
    }
    @Override
    public void onFocusChange(View view, boolean hasFocus) {
        if(hasFocus){
            String phone = et_phone.getText().toString();
            //如果手機(jī)號(hào)碼不足11位或?yàn)榭?
            if(TextUtils.isEmpty(phone)||phone.length()<11){
                //手機(jī)號(hào)碼編輯框請(qǐng)求焦點(diǎn),把光標(biāo)移回手機(jī)號(hào)碼編輯框
                et_phone.requestFocus();
                Toast.makeText(this,"請(qǐng)輸入11位的!",Toast.LENGTH_SHORT).show();
            }
        }
    }
}

三、文本變化監(jiān)聽(tīng)器

調(diào)用編輯框?qū)ο蟮腶ddTextChangedListener方法即可注冊(cè)文本監(jiān)聽(tīng)器。

文本監(jiān)聽(tīng)器的接口名稱為TextWatcher,該接口提供了3個(gè)監(jiān)控方法,具體說(shuō)明:

  • beforeTextChanged:在文本改變之前觸發(fā)。
  • onTextChanged:在文本改變過(guò)程種觸發(fā)。
  • afterTextChanged:在文本改變后觸發(fā)。
         mEtPassword.addTextChangedListener(new MyWatcher());
class MyWatcher implements TextWatcher {
         public void beforeTextChanged(CharSequence s, int start, int count,int after) {
         }
         public void onTextChanged(CharSequence s, int start, int before,int count) {
         }
         public void afterTextChanged(Editable edit) {
         }
     }

到此這篇關(guān)于Android編輯框EditText中的焦點(diǎn)變更與文本變化監(jiān)視功能實(shí)現(xiàn)流程詳解的文章就介紹到這了,更多相關(guān)Android編輯框EditText內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Android應(yīng)用圖標(biāo)在狀態(tài)欄上顯示實(shí)現(xiàn)原理

    Android應(yīng)用圖標(biāo)在狀態(tài)欄上顯示實(shí)現(xiàn)原理

    Android應(yīng)用圖標(biāo)在狀態(tài)欄上顯示,以及顯示不同的圖標(biāo),其實(shí)很研究完后,才發(fā)現(xiàn),很簡(jiǎn)單,具體實(shí)現(xiàn)如下,感興趣的朋友可以參考下哈
    2013-06-06
  • Android  Wifi的forget()操作實(shí)例詳解

    Android Wifi的forget()操作實(shí)例詳解

    這篇文章主要介紹了Android Wifi的forget()操作實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下
    2017-02-02
  • 解析android中ProgressBar的用法

    解析android中ProgressBar的用法

    本篇文章是對(duì)android中ProgressBar的使用進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-06-06
  • Android中RecyclerView的item寬高問(wèn)題詳解

    Android中RecyclerView的item寬高問(wèn)題詳解

    RecyclerView出現(xiàn)已經(jīng)有一段時(shí)間了,相信大家肯定不陌生了,下面這篇文章主要給大家介紹了關(guān)于Android中RecyclerView的item寬高問(wèn)題的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面來(lái)一起看看吧。
    2017-08-08
  • Android自定義字母選擇側(cè)邊欄

    Android自定義字母選擇側(cè)邊欄

    這篇文章主要為大家詳細(xì)介紹了Android自定義字母選擇側(cè)邊欄,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • Android Studio 打包生成APK文件方法

    Android Studio 打包生成APK文件方法

    Android Studio是谷歌推出一個(gè)Android集成開(kāi)發(fā)工具,基于IntelliJ IDEA。這篇文章主要介紹了Android Studio 打包生成APK文件方法,需要的朋友可以參考下
    2018-07-07
  • Android 軟鍵盤狀態(tài)并隱藏輸入法的實(shí)例

    Android 軟鍵盤狀態(tài)并隱藏輸入法的實(shí)例

    這篇文章主要介紹了Android 軟鍵盤狀態(tài)并隱藏輸入法的實(shí)例的相關(guān)資料,這里提供實(shí)例實(shí)現(xiàn)軟鍵盤切換并隱藏輸入法的鍵盤,需要的朋友可以參考下
    2017-09-09
  • android之SeekBar控件用法詳解

    android之SeekBar控件用法詳解

    下面小編就為大家?guī)?lái)一篇android之SeekBar控件用法詳解。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2016-09-09
  • Android布局控件DrawerLayout實(shí)現(xiàn)完美側(cè)滑效果

    Android布局控件DrawerLayout實(shí)現(xiàn)完美側(cè)滑效果

    這篇文章主要為大家詳細(xì)介紹了Android布局控件DrawerLayout實(shí)現(xiàn)完美側(cè)滑效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-08-08
  • Android的webview支持HTML5的離線應(yīng)用功能詳細(xì)配置

    Android的webview支持HTML5的離線應(yīng)用功能詳細(xì)配置

    HTML5的離線應(yīng)用功能可以使得WebApp即使在網(wǎng)絡(luò)斷開(kāi)的情況下仍能正常使用這是個(gè)非常有用的功能,但如何使Webivew支持HTML5離線應(yīng)用功能呢,需要的朋友可以參考下
    2012-12-12

最新評(píng)論