1. 程式人生 > >EditText監聽文字改變和焦點改變

EditText監聽文字改變和焦點改變

實現效果:


需求分析: 

1) 電話號碼數字按照344進行分組,中間加空格

2) 點選刪除按鈕,輸入框中內容為空

3) 監聽輸入框的焦點變化狀況 ,獲取焦點顯示刪除按鈕,失去焦點隱藏輸入按鈕(使用者已經輸入的情況下)

4) 設定游標的位置

5) 當用戶輸入的字元長度為13時,獲取驗證碼背景變紅,並且獲取驗證碼按鈕可以點選,其他時候不能點選。

程式碼實現:

    private void initEditText() {
        et_login_name = (EditText) findViewById(R.id.et_login_name);
        iv_delete_phone_number = (ImageView) findViewById(R.id.iv_delete_phone_number);
        iv_delete_phone_number.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //賦值為空字串
                et_login_name.setText("");
            }
        });
        btn_sms_code = (Button) findViewById(R.id.btn_sms_code);
        et_login_name.addTextChangedListener(new MyWatch());

        //設定檢點改變監聽,設定刪除圖示的顯示與隱藏   (這裡的監聽特別人性化)
        et_login_name.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean has) {
                if (et_login_name.getText().toString().length() > 0) {
                    //使用者有輸入內容
                    if (has) {
                        iv_delete_phone_number.setVisibility(View.VISIBLE);
                    } else {
                        iv_delete_phone_number.setVisibility(View.GONE);
                    }
                }
            }
        });
    }

    private class MyWatch implements TextWatcher {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            //使用者輸入的字串
            String input = charSequence.toString();
            int length = input.length();

            //實現電話號碼數字分組 344
            if (length == 4) {
                if (input.substring(3).equals(new String(" "))) {
                    input = input.substring(0, 3);
                    et_login_name.setText(input);
                    et_login_name.setSelection(input.length());
                } else { // +  130 2
                    input = input.substring(0, 3) + " " + input.substring(3);
                    et_login_name.setText(input);
                    et_login_name.setSelection(input.length());
                }
            } else if (length == 9) {
                if (input.substring(8).equals(new String(" "))) {
                    input = input.substring(0, 8);
                    et_login_name.setText(input);
                    et_login_name.setSelection(input.length());
                } else {// +
                    input = input.substring(0, 8) + " " + input.substring(8);
                    et_login_name.setText(input);
                    et_login_name.setSelection(input.length());
                }
            }

            //刪除圖示的顯示與隱藏
            if (et_login_name.getText().toString().isEmpty()) {
                iv_delete_phone_number.setVisibility(View.GONE);
            } else {
                iv_delete_phone_number.setVisibility(View.VISIBLE);
            }

            //游標位置
            et_login_name.setSelection(et_login_name.getText().toString().length());

            //獲取驗證碼背景變紅
            if (et_login_name.getText().toString().length() == 13) {
                btn_sms_code.setBackgroundColor(getResources().getColor(R.color.red1));
                btn_sms_code.setTextColor(getResources().getColor(R.color.white1));
                //只有為11位電話號碼時,才能點選
                btn_sms_code.setClickable(true);
            } else {
                btn_sms_code.setBackgroundColor(getResources().getColor(R.color.bg_gay_with_radius_8));
                btn_sms_code.setTextColor(getResources().getColor(R.color.font1));
                //不能點選獲取驗證碼按鈕
                btn_sms_code.setClickable(false);
            }
        }

        @Override
        public void afterTextChanged(Editable editable) {
        }
    }
佈局檔案:
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <LinearLayout
            android:id="@+id/phone_frame"
            android:layout_width="match_parent"
            android:layout_height="55dp"
            android:background="#ffffff"
            android:gravity="center_vertical">

            <ImageView
                android:id="@+id/iv_phone"
                android:layout_width="20dp"
                android:layout_height="20dp"
                android:layout_gravity="center"
                android:layout_marginLeft="19dp"
                android:background="@drawable/icon_user" />

            <LinearLayout
                android:layout_width="0px"
                android:layout_height="0px"
                android:focusable="true"
                android:focusableInTouchMode="true" />

            <EditText
                android:id="@+id/et_login_name"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="8dp"
                android:layout_weight="1"
                android:background="@null"
                android:hint="手機號"
                android:inputType="number"
                android:maxLength="13"
                android:textColor="#000"
                android:textSize="18sp" />

            <ImageView
                android:id="@+id/iv_delete_phone_number"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_marginRight="20dp"
                android:contentDescription="@string/app_name"
                android:src="@drawable/ico_del"
                android:visibility="gone" />

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_gravity="center"
                android:layout_marginRight="20dp"
                android:orientation="horizontal">

                <Button
                    android:id="@+id/btn_sms_code"
                    android:layout_width="120dp"
                    android:layout_height="32dp"
                    android:layout_gravity="center"
                    android:background="#d9d9d9"
                    android:clickable="false"
                    android:gravity="center"
                    android:paddingLeft="10dp"
                    android:paddingRight="10dp"
                    android:text="獲取驗證碼"
                    android:textColor="#999999"
                    android:textSize="15sp" />
            </LinearLayout>
        </LinearLayout>