当先锋百科网

首页 1 2 3 4 5 6 7

实现效果:


需求分析: 

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>