《Android学习笔记》实现带字数的文本输入框

先看效果图:
在这里插入图片描述
布局文件:activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    
	<LinearLayout
		android:layout_width="match_parent"
		android:layout_height="wrap_content"
		android:layout_marginStart="16dp"
		android:layout_marginEnd="16dp"
		android:orientation="horizontal">

		<TextView
			android:layout_width="0dp"
        	android:layout_height="wrap_content"
        	android:layout_weight="1"
        	android:text="问题和意见"
        	android:textColor="#000000"
        	android:textSize="14sp" />

		<TextView
            android:id="@+id/tv_number"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="0/500"
            android:textColor="#000000"
            android:textSize="14sp" />

	</LinearLayout>

	<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="@dimen/dp_16"
        android:layout_marginTop="@dimen/dp_8"
        android:layout_marginEnd="@dimen/dp_16"
        android:layout_marginBottom="@dimen/dp_47"
        android:background="@drawable/bg_r16_ffffff"
        android:orientation="vertical">

        <EditText
            android:id="@+id/et_questions_comments"
            android:layout_width="wrap_content"
            android:layout_height="@dimen/dp_150"
            android:background="@null"
            android:gravity="top"
            android:hint="@string/str_questions_and_comments_hint"
            android:padding="@dimen/dp_16"
            android:textColor="@color/black"
            android:textColorHint="@color/hint_text_color"
            android:textSize="@dimen/sp_14"
            tools:ignore="Autofill" />

    </LinearLayout>
    
</LinearLayout>

核心代码:

private TextView mTvNumber;
private Edittext mEtComments;

@Override
protected void onCreate(){
	super.onCreate();
	initView();
	initData();
}

private void initView(){
	mTvNumber = findViewById(R.id.tv_number);
	mEtComments = findViewById(R.id.et_comments);
}

private void initData(){
	mEtComments.addTextChangeListener(new TextWatcher(){
		//输入的文本
		private CharSequence inputText;
		//输入文本个数
		private int inputTextNumber;

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count, int after){
			
		}

		@Override
		public void onTextChanged(CharSequence s, int start, int before, int count){
			inputText= s;
		}

		@Override
		public void afterTextChanged(Editable s){
			inputTextNumber= 500 - s.length();
			mTvNumber.setText((500 - inputTextNumber) + "/500");
			mTvNumber.setTextColor(Color.BLACK);
			mEtComments.setTextColor(Color.BLACK);
			
			if(inputText.length() > 500){
				mTvNumber.setTextColor(Color.RED);
				mEtComments.setTextColor(Color.RED);
				//设置光标的位置
				mEtComments.setSelection(mEtComments.getSelectionEnd());
			}
		}
	});
}

如果有更好的意见,麻烦不吝赐教。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值