Android EditText文本框错误提示语句

https://blog.csdn.net/shenggaofei/article/details/100172304中写到了TextInputLayout,下面看看EditText简单的设置显示错误信息的弹框提示:

一.基本属性:

hint 输入框显示的提示文本 
textColorHint 输入框显示的提示文本的颜色 
inputType限制用户的输入类型
capitalize英文大写设置
minLines 最小行数
maxLines 最大行数
SingleLine 单行不换行
  • android:inputType=none:普通字符。
  • android:inputType=text:普通字符。
  • android:inputType=textCapCharacters:字母大写。
  • android:inputType=textCapWords:首字母大写。
  • android:inputType=textCapSentences:仅第一个字母大写。
  • android:inputType=textAutoCorrect:自动完成。
  • android:inputType=textAutoComplete:自动完成。
  • android:inputType=textMultiLine:多行输入。
  • android:inputType=textImeMultiLine:输入法多行(如果支持)。
  • android:inputType=textNoSuggestions:不提示。
  • android:inputType=textUri:网址。
  • android:inputType=textEmailAddress:电子邮件地址。
  • android:inputType=textEmailSubject:邮件主题。
  • android:inputType=textShortMessage:短讯。
  • android:inputType=textLongMessage:长信息。
  • android:inputType=textPersonName:人名。
  • android:inputType=textPostalAddress:地址。
  • android:inputType=textPassword:密码。
  • android:inputType=textVisiblePassword:可见密码。
  • android:inputType=textWebEditText:作为网页表单的文本。
  • android:inputType=textFilter:文本筛选过滤。
  • android:inputType=textPhonetic:拼音输入。
  • android:inputType=number:数字。
  • android:inputType=numberSigned:带符号数字格式。
  • android:inputType=numberDecimal:带小数点的浮点格式。
  • android:inputType=phone:拨号键盘。
  • android:inputType=datetime:时间日期。
  • android:inputType=date:日期键盘。
  • android:inputType=time:时间键盘。

text普通字符
textCapCharacters普通字符
none普通字符
textCapSentences字符串中的第一个字母大写
textCapWords字符串中的每个单词的首字母大写
textMultiLine多行输入
textImeMultiLine输入法多行
textUri格式为:URI
textShortMessage格式为:短消息
textShortMessage格式为:长消息
textEmailAddress格式为:电子邮件地址
textEmailSubject格式为:邮件主题
textPostalAddress格式为:邮政
textPersonName格式为:姓名
textPassword格式为:不可见密码
textVisiblePassword格式为:可见密码
textFilter格式为:文本筛选
textWebEditText格式为:作为网页表单的文本
number格式为:数字
numberSigned格式为:有符号数字
numberDecimal格式为:浮点数
textPhonetic格式为:拼音输入
phone键盘为:拨号
date或者datetime键盘为:日期
time键盘为:时间
textAutoCorrect前两个自动完成
textAutoComplete前两个自动完成
textNoSuggestions不进行提示

EditText还派生了如下两个子类。

  • AutoCompleteTextView:带有自动完成功能的EditText。由于该类通常需要与 Adapter结合使用,因此将会在下一章进行学习。
  • ExtractEditText:并不是UI组件,而是EditText组件的底层服务类,负责提供全屏输入法支持。

二.效果代码:

1.主函数:

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.example.qd.douyinwu.R;
import com.example.qd.douyinwu.utils.TelNumMatch;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * https://blog.csdn.net/shenggaofei/article/details/100172304
 */
public class TextInputLayoutActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {

    private EditText et_one,et_two,et_three,et_four,et_type_one_b,et_type_one_a,et_type_two_a,et_type_two_b;
    private Button btn_registered,btn_login,bt_send,btn_cancel,btn_save;
    private CheckBox cb_select,cb_two,cb_one;
    private RadioGroup rgSex;
    private String sex;// 性别

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_textinputlayout);

        et_one = findViewById(R.id.et_one);
        et_two = findViewById(R.id.et_two);
        et_three = findViewById(R.id.et_three);
        et_four = findViewById(R.id.et_four);
        btn_registered = findViewById(R.id.btn_registered);
        btn_login = findViewById(R.id.btn_login);
        bt_send = findViewById(R.id.bt_send);
        btn_save = findViewById(R.id.btn_save);
        btn_cancel = findViewById(R.id.btn_cancel);
        cb_select = findViewById(R.id.cb_select);
        rgSex = findViewById(R.id.rgSex);

        // 绑定事件
        rgSex.setOnCheckedChangeListener(this);
        final Drawable dr = getResources().getDrawable(R.drawable.ic_launcher_background);
        dr.setBounds(0, 0, 10, 10); //必须设置大小,否则不显示


        cb_one = (CheckBox) findViewById(R.id.cb_one);
        cb_two = (CheckBox) findViewById(R.id.cb_two);
        et_type_one_a =  findViewById(R.id.et_type_one_a);
        et_type_one_b =  findViewById(R.id.et_type_one_b);
        et_type_two_a =  findViewById(R.id.et_type_two_a);
        et_type_two_b =  findViewById(R.id.et_type_two_b);
        et_type_one_a.setEnabled(false);
        et_type_one_b.setEnabled(false);
        et_type_two_a.setEnabled(false);
        et_type_two_b.setEnabled(false);

        //监听选中取消事件
        cb_one.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked){
                    et_type_one_a.setEnabled(true);
                    et_type_one_b.setEnabled(true);
                    et_type_two_a.setEnabled(false);
                    et_type_two_b.setEnabled(false);
                    et_type_one_a.setText("");
                    et_type_one_b.setText("");
                    et_type_two_a.setText("");
                    et_type_two_b.setText("");
                    cb_one.setChecked(true);
                    cb_two.setChecked(false);
                }else {
                    cb_one.setChecked(false);
                    et_type_one_a.setEnabled(false);
                    et_type_one_b.setEnabled(false);
                    et_type_two_a.setEnabled(true);
                    et_type_two_b.setEnabled(true);
                    et_type_one_a.setText("");
                    et_type_one_b.setText("");
                    et_type_two_a.setText("");
                    et_type_two_b.setText("");
                }
            }
        });
        cb_two.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked){
                    et_type_one_a.setEnabled(false);
                    et_type_one_b.setEnabled(false);
                    et_type_two_a.setEnabled(true);
                    et_type_two_b.setEnabled(true);
                    et_type_one_a.setText("");
                    et_type_one_b.setText("");
                    et_type_two_a.setText("");
                    et_type_two_b.setText("");
                    cb_two.setChecked(true);
                    cb_one.setChecked(false);
                }else {
                    et_type_one_a.setEnabled(true);
                    et_type_one_b.setEnabled(true);
                    et_type_two_a.setEnabled(false);
                    et_type_two_b.setEnabled(false);
                    et_type_one_a.setText("");
                    et_type_one_b.setText("");
                    et_type_two_a.setText("");
                    et_type_two_b.setText("");
                    cb_two.setChecked(false);
                }
            }
        });
        cb_select.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                // TODO Auto-generated method stub
                if(isChecked){
                    //如果选中,显示密码
                    et_two.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    if(!TextUtils.isEmpty(et_two.getText().toString())){
                        et_two.setSelection(et_two.length());
                    }

                }else{
                    //否则隐藏密码
                    et_two.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    if(!TextUtils.isEmpty(et_two.getText().toString())){
                        et_two.setSelection(et_two.length());
                    }
                }

            }
        });
        btn_registered.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(TextInputLayoutActivity.this,"Registered",Toast.LENGTH_SHORT).show();
            }
        });
        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(TextInputLayoutActivity.this,"Registered",Toast.LENGTH_SHORT).show();
            }
        });
        bt_send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(TextInputLayoutActivity.this,"Send",Toast.LENGTH_SHORT).show();
            }
        });
        //注意该方法需要通过敲击回车键才会有所监听返回
        et_one.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                Toast.makeText(TextInputLayoutActivity.this,"你点击了回车键了?",Toast.LENGTH_SHORT).show();

                return false;

            }
        });

        et_four.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String money = et_four.getText().toString();
                if(TextUtils.isEmpty(money)&&"".equals(money)){
                    et_four.setError("请输入验证码");
                }else {
                    if(money.length()==6){

                    }else {
                        et_four.setError("请输入6位数数字验证码");
                    }
                }
            }
        });
        et_three.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String money = et_three.getText().toString();
                if(TextUtils.isEmpty(money)&&"".equals(money)){
                    et_three.setError("请输入内容");
                }else {
                    int intMoney = Integer.parseInt(money);
                    if(intMoney>=0&&intMoney<=256){

                    }else {
                        et_three.setError("请输入0`256的整数值");
                    }
                }
            }
        });
        et_two.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String money = et_two.getText().toString();
                if(TextUtils.isEmpty(money)&&"".equals(money)){
                    et_two.setError("请输入密码");
                }else {
                    if(money.length()>=6&&money.length()<=12){

                    }else {
                        et_two.setError("请输入6-12位数的英文字母与数字的组合");
//                    Toast.makeText(TextInputLayoutActivity.this,"请输入0`256的整数值",Toast.LENGTH_SHORT).show();

                    }
                }
            }
        });
        et_one.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String money = et_one.getText().toString();
//                Pattern p = Pattern.compile("[0-9]*");
//                Matcher m = p.matcher(money);
//                if (m.matches()) {
//
//                } else {
//                    Toast.makeText(TextInputLayoutActivity.this,"请输入正确的缴费金额",Toast.LENGTH_SHORT).show();
//                }
                if(TextUtils.isEmpty(money)&&"".equals(money)){
                    et_one.setError("请输入账号");
                }else {
//                    int intMoney = Integer.parseInt(money);
                    //判断正确手机1
//                    if(isMobileNO(money)){
                    //判断正确手机2
//                    if(TelNumMatch.isValidPhoneNumber(money)){
                    //判断正确手机3
                    if(TelNumMatch.isChinaPhoneLegal(money)){

                    }else {
                        //电话号码格式不正确
                        et_one.setError("请输入11位数的正确手机号");
//                    Toast.makeText(TextInputLayoutActivity.this,"请输入0`256的整数值",Toast.LENGTH_SHORT).show();

                    }
                    //设置必须是1开头的数字
                    if (!s.toString().startsWith("1")) {
                        et_one.setText("1");
                        et_one.setSelection(1);
                    }
                }

//                if (!et_one.getText().toString().matches("[a-zA-Z0-9._-]+@[a-z]+\\.+[a-z]+")) {
//                    et_one.setError("请输入正确的邮箱地址", dr);
//                }
            }
        });
        btn_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(TextInputLayoutActivity.this,"已经取消",Toast.LENGTH_SHORT).show();

            }
        });
        btn_save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(cb_one.isChecked()){
                    Toast.makeText(TextInputLayoutActivity.this,"选择了类型1"+et_type_one_a.getText().toString()+"--"+et_type_one_b.getText().toString(),Toast.LENGTH_SHORT).show();

                }else if (cb_two.isChecked()){
                    Toast.makeText(TextInputLayoutActivity.this,"选择了类型2"+et_type_two_a.getText().toString()+"--"+et_type_two_b.getText().toString(),Toast.LENGTH_SHORT).show();

                }else {
                    Toast.makeText(TextInputLayoutActivity.this,"请选择类型",Toast.LENGTH_SHORT).show();

                }

            }
        });
    }


    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int id) {
        // TODO Auto-generated method stub
        switch (id) {
            // 选择男
            case R.id.rbMale:
                sex = "男";
                break;

            // 选择女
            case R.id.rbFemale:
                sex = "女";
                break;
            default:
                break;
        }
        Toast.makeText(TextInputLayoutActivity.this, sex, Toast.LENGTH_SHORT).show();
    }

    private boolean isMobileNO(String mobiles) {
        String telRegex = "^((1[3,5,7,8][0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$";
        if (TextUtils.isEmpty(mobiles)) {
            return false;
        } else {
            return mobiles.matches(telRegex);
        }
    }
}

2.布局代码及相关背景设置:

主函数布局:

<?xml version="1.0" encoding="utf-8"?><!-- xmlns:app="http://schemas.android.com/apk/res-auto" 记得设置命名空间-->
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <!--</android.support.design.widget.TextInputLayout>-->

        <EditText
            android:id="@+id/et_one"
            android:layout_width="match_parent"
            android:layout_height="45dp"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="15dp"
            android:layout_marginTop="15dp"
            android:background="@drawable/shape_blue_sgf"
            android:hint="请输入账号"
            android:inputType="phone"
            android:maxLength="11"
            android:paddingLeft="15dp"
            android:paddingRight="15dp"
            android:textColor="@color/orange_500"
            android:textColorHint="@color/colorPrimary" />

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="15dp"
            android:layout_marginTop="15dp"
            android:background="@drawable/shape_blue_sgf">

            <EditText
                android:id="@+id/et_two"
                android:layout_width="match_parent"
                android:layout_height="45dp"
                android:background="@drawable/shape_blue_sgf"
                android:hint="请输入密码"
                android:inputType="textPassword"
                android:maxLength="12"
                android:paddingLeft="15dp"
                android:paddingRight="15dp"
                android:textColor="@color/orange_500"
                android:textColorHint="@color/colorPrimary" />

            <CheckBox
                android:id="@+id/cb_select"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_centerVertical="true"
                android:layout_marginRight="35dp"
                android:button="@drawable/checkbox_bg" />
        </RelativeLayout>

        <EditText
            android:id="@+id/et_three"
            android:layout_width="match_parent"
            android:layout_height="45dp"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="15dp"
            android:layout_marginTop="15dp"
            android:background="@drawable/shape_blue_sgf"
            android:hint="请输入内容"
            android:inputType="number"
            android:maxLength="5"
            android:paddingLeft="15dp"
            android:paddingRight="15dp"
            android:textColor="@color/orange_500"
            android:textColorHint="@color/colorPrimary" />

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="15dp"
            android:layout_marginTop="15dp"
            android:background="@drawable/shape_blue_sgf">

            <EditText
                android:id="@+id/et_four"
                android:layout_width="match_parent"
                android:layout_height="45dp"
                android:layout_centerVertical="true"
                android:layout_marginRight="130dp"
                android:background="@null"
                android:hint="请输入验证码"
                android:inputType="number"
                android:maxLength="6"
                android:paddingLeft="15dp"
                android:paddingRight="15dp"
                android:textColor="@color/orange_500"
                android:textColorHint="@color/colorPrimary" />

            <Button
                android:id="@+id/bt_send"
                android:layout_width="90dp"
                android:layout_height="30dp"
                android:layout_alignParentRight="true"
                android:layout_centerVertical="true"
                android:layout_marginRight="15dp"
                android:layout_marginTop="10dp"
                android:background="@color/colorPrimary"
                android:text="发送"
                android:textColor="@color/white" />
        </RelativeLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:layout_marginTop="@dimen/login_margin_top"
            android:gravity="center">

            <Button
                android:id="@+id/btn_login"
                android:layout_width="150dp"
                android:layout_height="40dp"

                android:layout_gravity="center"
                android:background="@drawable/btn_iv_drawable_select"
                android:text="login"
                android:textAllCaps="false"
                android:textColor="@color/colorPrimary"
                android:textSize="@dimen/login_text_size" />

            <Button
                android:id="@+id/btn_registered"
                android:layout_width="150dp"
                android:layout_height="40dp"
                android:layout_gravity="center"
                android:layout_marginLeft="26dp"
                android:background="@drawable/shape_blue_sgf"
                android:text="registered"
                android:textAllCaps="false"
                android:textColor="@color/colorPrimary"
                android:textSize="@dimen/login_text_size" />
        </LinearLayout>

        //

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="@color/colorPrimary"></View>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="60dp"
                android:gravity="center">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:text="类型选择:"
                    android:textAllCaps="false"
                    android:textColor="@color/colorPrimary"
                    android:textSize="@dimen/login_text_size" />

                <CheckBox
                    android:id="@+id/cb_one"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:background="@null"
                    android:button="@null"
                    android:drawableLeft="@drawable/radiobutton_icon"
                    android:drawablePadding="8dp"
                    android:text="类型1"
                    android:textAllCaps="false" />

                <EditText
                    android:id="@+id/et_type_one_a"
                    android:layout_width="90dp"
                    android:layout_height="30dp"
                    android:layout_gravity="center"
                    android:layout_marginLeft="5dp"
                    android:layout_marginRight="5dp"
                    android:background="@drawable/shape_blue_sgf"
                    android:hint="请输入内容"
                    android:paddingLeft="5dp"
                    android:paddingRight="5dp"
                    android:singleLine="true"
                    android:textColor="@color/orange_500"
                    android:textColorHint="@color/colorPrimary" />

                <EditText
                    android:id="@+id/et_type_one_b"
                    android:layout_width="90dp"
                    android:layout_height="30dp"
                    android:layout_gravity="center"
                    android:layout_marginLeft="5dp"
                    android:layout_marginRight="5dp"
                    android:background="@drawable/shape_blue_sgf"
                    android:hint="请输入内容"
                    android:paddingLeft="5dp"
                    android:paddingRight="5dp"
                    android:singleLine="true"
                    android:textColor="@color/orange_500"
                    android:textColorHint="@color/colorPrimary" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="60dp"
                android:gravity="center">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:text="类型选择:"
                    android:textAllCaps="false"
                    android:textColor="@color/colorPrimary"
                    android:textSize="@dimen/login_text_size"
                    android:visibility="invisible" />

                <CheckBox
                    android:id="@+id/cb_two"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:background="@null"
                    android:button="@null"
                    android:drawableLeft="@drawable/radiobutton_icon"
                    android:drawablePadding="8dp"
                    android:text="类型2"
                    android:textAllCaps="false" />

                <EditText
                    android:id="@+id/et_type_two_a"
                    android:layout_width="90dp"
                    android:layout_height="30dp"
                    android:layout_gravity="center"
                    android:layout_marginLeft="5dp"
                    android:layout_marginRight="5dp"
                    android:background="@drawable/shape_blue_sgf"
                    android:hint="请输入内容"
                    android:paddingLeft="5dp"
                    android:paddingRight="5dp"
                    android:singleLine="true"
                    android:textColor="@color/orange_500"
                    android:textColorHint="@color/colorPrimary" />

                <EditText
                    android:id="@+id/et_type_two_b"
                    android:layout_width="90dp"
                    android:layout_height="30dp"
                    android:layout_gravity="center"
                    android:layout_marginLeft="5dp"
                    android:layout_marginRight="5dp"
                    android:background="@drawable/shape_blue_sgf"
                    android:hint="请输入内容"
                    android:paddingLeft="5dp"
                    android:paddingRight="5dp"
                    android:singleLine="true"
                    android:textColor="@color/orange_500"
                    android:textColorHint="@color/colorPrimary" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="60dp"
                android:layout_marginTop="@dimen/login_margin_top"
                android:gravity="center">

                <Button
                    android:id="@+id/btn_save"
                    android:layout_width="150dp"
                    android:layout_height="40dp"
                    android:layout_gravity="center"
                    android:background="@drawable/shape_blue_sgf"
                    android:text="save"
                    android:textAllCaps="false"
                    android:textColor="@color/colorPrimary"
                    android:textSize="@dimen/login_text_size" />

                <Button
                    android:id="@+id/btn_cancel"
                    android:layout_width="150dp"
                    android:layout_height="40dp"
                    android:layout_gravity="center"
                    android:layout_marginLeft="26dp"
                    android:background="@drawable/shape_blue_sgf"
                    android:text="cancel"
                    android:textAllCaps="false"
                    android:textColor="@color/colorPrimary"
                    android:textSize="@dimen/login_text_size" />
            </LinearLayout>
        </LinearLayout>

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="@color/colorPrimary" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="35dp"
            android:layout_gravity="center">

            <RadioGroup
                android:id="@+id/rgSex"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:orientation="horizontal">

                <RadioButton
                    android:id="@+id/rbMale"
                    android:layout_marginLeft="15dp"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@null"
                    android:button="@null"
                    android:checked="true"
                    android:drawableLeft="@drawable/radiobutton_icon"
                    android:drawablePadding="5dp"
                    android:text="男" />

                <RadioButton
                    android:id="@+id/rbFemale"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="15dp"
                    android:background="@null"
                    android:button="@null"
                    android:drawableLeft="@drawable/radiobutton_icon"
                    android:drawablePadding="5dp"
                    android:text="女" />
            </RadioGroup>
        </LinearLayout>
    </LinearLayout>
</ScrollView>

背景设置:

shape_blue_sgf.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <!--无圆角边框-->
    <solid android:color="@android:color/white" />
    <!--填充的颜色-->
    <!--描边-->
    <stroke
        android:width="1dp"
        android:color="#0000FF" />
    <!--设置外层边线的圆角度数-->
    <corners android:radius="8dp"></corners>
</shape>

密码设置显示隐藏按钮 checkbox_bg.xml:

<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- 点击之后的图标 -->
    <item
        android:state_checked="true"
        android:drawable="@drawable/pw_show"
        />
    <!-- 点击之前的图标 -->
    <item
        android:state_checked="false"
        android:drawable="@drawable/pw_hide"
        />
</selector>

自定义CheckBox、RadioButton背景选择  radiobutton_icon.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/icon_no" android:state_checked="false" />  
       <item android:drawable="@drawable/icon_yes" android:state_checked="true" />  
</selector>

自定义登录按钮背景选择   btn_iv_drawable_select.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/shape_blue_sgf" android:state_pressed="false" />
    <item android:drawable="@drawable/shape_yellow_sgf" android:state_pressed="true"/>
</selector>

shape_yellow_sgf.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <!--无圆角边框-->
    <solid android:color="@android:color/white" />
    <!--填充的颜色-->
    <!--描边-->
    <stroke
        android:width="1dp"
        android:color="#FF0" />
    <!--设置外层边线的圆角度数-->
    <corners android:radius="8dp"></corners>
</shape>

 

  • 2
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值