android通过TextWatcher监测输入框中输入的字符个数

大家或许会遇到这样的需求,监听文本框中输入的字符个数,这在一些应用的提交意见与建议这里可能会用到。 今天我们就用android提供的工具来实现这个功能,那就是TextWatcher,它提供了3个回调方法,分别对应为文本改变前,文本改变和文本改变之后,完美的满足了我们的需求。

首先,是布局文件activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#0066FF"
        android:gravity="center_horizontal"
        android:layout_weight="0"
        android:padding="10dp" >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="测试测试"
            android:textColor="#FFFFFF"
            android:textSize="20sp" />
    </LinearLayout>
    <ScrollView
        android:id="@+id/scrollview"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_margin="10.0dp"
        android:layout_marginTop="48dp"
        android:layout_weight="1"
        android:background="@drawable/settings_bg"
        android:fillViewport="true" >
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >
            <EditText
                android:id="@+id/edit"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@null"
                android:gravity="left|top"
                android:hint="亲,给点建议吧!么么哒!"
                android:inputType="textMultiLine"
                android:layout_weight="1"
                android:paddingBottom="4.0dp"
                android:paddingLeft="4.0dp"
                android:paddingRight="4.0dp"
                android:paddingTop="4.0dp"
                android:textSize="16.0sp" />
            <TextView
                android:id="@+id/tips"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginRight="5dp"
                android:gravity="center|right"
                android:layout_weight="0"
                android:padding="5dp"
                android:text="(160字以内)"
                android:textSize="16sp" />
        </LinearLayout>
    </ScrollView>
    <Button
        android:id="@+id/btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0"
        android:layout_gravity="center"
        android:layout_marginBottom="10.0dp"
        android:layout_marginLeft="10.0dp"
        android:layout_marginRight="10.0dp"
        android:background="@drawable/btn_bg"
        android:text="提  交"
        android:textSize="18sp" />
</LinearLayout>
接下来就是功能的实现了。

package com.tony.edittextwatcher;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.app.Activity;

public class MainActivity extends Activity {
	private EditText edit;
	private TextView tips;
	private Button btn;	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
//		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_main);
		edit = (EditText) findViewById(R.id.edit);
		tips = (TextView) findViewById(R.id.tips);
		btn = (Button) findViewById(R.id.btn);
		edit.addTextChangedListener(new TextWatcher() {			
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				if (s.length() == 0) {
					tips.setText("(160字以内)");
				} else if (s.length() > 160) {
					Toast.makeText(MainActivity.this, "亲,写得太多啦", Toast.LENGTH_SHORT).show();
				} else {
					tips.setText("(" + String.valueOf(0 + s.length())
							+ "/160字)");
				}
			}			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {				
			}			
			@Override
			public void afterTextChanged(Editable s) {				
			}
		});
		btn.setOnClickListener(new OnClickListener() {			
			@Override
			public void onClick(View v) {
				String content=edit.getText().toString();
				if(!TextUtils.isEmpty(content)){
					Toast.makeText(MainActivity.this, "提交成功!!!", Toast.LENGTH_SHORT).show();
				}else{
					Toast.makeText(MainActivity.this, "亲,你还没写哦(づ ̄3 ̄)づ╭❤~", Toast.LENGTH_SHORT).show();
				}				
			}
		});
	}
}
从代码中我们可以看到,只要给EditText添加一个文本监听,即addTextChangedListener,再在onTextChanged()这个方法中编写代码就可以了。至于要输入多少字符,这个可以自己设定。

下载完整代码请戳这里


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

代码的灵魂是bug!

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值