android studio EditText用法

1.自定义文本框

选中状态:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">

    <!--指定形状内部颜色-->
    <solid android:color="#ffffff">
    </solid>
    <!--指定形状轮廓的粗细与颜色-->
    <stroke android:width="1dp"
        android:color="#0000ff">
    </stroke>
    <!--指定圆角半径-->
    <corners android:radius="5dp"/>
    <!--指定上下左右间距-->
    <padding android:bottom="2dp"
        android:left="2dp"
        android:right="2dp"
        android:top="2dp">
    </padding>
</shape>

未选中状态:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <!--指定形状内部颜色-->
    <solid android:color="#ffffff">
    </solid>
    <!--指定形状轮廓的粗细与颜色-->
    <stroke android:width="1dp"
        android:color="#aaaaaa">
    </stroke>
    <!--指定圆角半径-->
    <corners android:radius="5dp"/>
    <!--指定上下左右间距-->
    <padding android:bottom="2dp"
        android:left="2dp"
        android:right="2dp"
        android:top="2dp">
    </padding>

</shape>

2.定义文本框

<EditText
    android:id="@+id/ed1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/et1"
    android:text="请输入名字">
</EditText>
<EditText
    android:id="@+id/ed2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/edittext_selector"
    android:inputType="textPassword"
    android:maxLength="11"
    android:hint="请输密码">
</EditText>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <TextView
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:gravity="end"
        android:text="用户:">
    </TextView>
    <EditText
        android:id="@+id/ed3"
        android:layout_width="0dp"
        android:layout_weight="3"
        android:layout_height="wrap_content"
        android:hint="请输入手机号码"
        android:inputType="number"
        android:maxLength="11">
    </EditText>
</LinearLayout>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <TextView
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:gravity="end"
        android:text="密码:">

    </TextView>
    <EditText
        android:id="@+id/ed4"
        android:layout_width="0dp"
        android:layout_weight="3"
        android:layout_height="wrap_content"
        android:hint="请输入验证码"
        android:maxLength="6"
        android:inputType="numberPassword">
    </EditText>
</LinearLayout>

3.焦点监听

editText3 = findViewById(R.id.ed3);
//焦点监听
editText3.setOnFocusChangeListener(this::onFocusChange);private void onFocusChange(View view, boolean b) {

    if(b){
        String  phone =  editText3.getText().toString();
        Log.d("用户账号数据:", "onFocusChange: "+phone);
        //手机号码不足11位
        if(TextUtils.isEmpty(phone)||phone.length()<11){
            //手机号码编辑框请求焦点,把光标移回手机号码编辑框
            editText3.requestFocus();
            Log.d("用户账号数据:", "手机号码不足11位!!! ");
            Toast.makeText(this, "手机号码不足11位", Toast.LENGTH_SHORT).show();
        }
    }

}

4.当输入达到最大值,关闭键盘

editText3 = findViewById(R.id.ed3);
editText4 = findViewById(R.id.ed4);
//文本变化监听
editText3.addTextChangedListener(new HideTextWatcher (editText3,11));
editText4.addTextChangedListener(new HideTextWatcher (editText4,6));
/**
 * 自定义文本变化监听类
 */
private  class  HideTextWatcher implements TextWatcher{

    private  EditText mView;
    private  int mMaxLenth;

    //定义一个构造方法
    public HideTextWatcher(EditText v, int maxLenth) {
        this.mView = v;
        this.mMaxLenth = maxLenth;
    }

    //改变之前
    @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 str = s.toString();
        //输入文本达到最大数,关闭输入法
        if(str.length()==mMaxLenth){
            ViewUitl.hideOneInputMethod( MainActivity3.this,mView);
        }

    }
}

5.自定义关闭键盘工具类

public class ViewUitl {


    /**
     * 关闭键盘
     * @param act
     * @param v
     */
    public static void hideOneInputMethod(Activity act, View v) {
        //从系统获取输入法
        InputMethodManager imm =(InputMethodManager) act.getSystemService(Context.INPUT_METHOD_SERVICE);
        //关闭键盘
        imm.hideSoftInputFromWindow(v.getWindowToken(),0);

    }


}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值