android多个editext文本改变事件,Android多个EditText输入效果解决方案

原标题:Android多个EditText输入效果解决方案

在开发中,我们常常遇到这种情况

92156642eb5a96ba2205fe74aba1ffc0.png

我们往往需要的是下面这种效果

8b65a07d3a2f2a30e1ba8f3968225b48.gif

但是如果把这些实现的代码写在Activity中会比较麻烦,影响代码美观

于是就有了下面这个辅助类,禁用了按钮的点击事件和按钮的渐变色,可同时添加一个或者多个EditText

/**

* 文本输入辅助类,通过管理多个TextView或者EditText输入是否为空来启用或者禁用按钮的点击事件

*/

publicfinalclassTextInputHelperimplementsTextWatcher{

privateView mMainView; //操作按钮的View

privateList mViewSet; //TextView集合,子类也可以(EditText、TextView、Button)

privatebooleanisAlpha; //是否设置透明度

publicTextInputHelper(View view){

this(view, true);

}

/**

* 构造函数

*

* @paramview 跟随EditText或者TextView输入为空来判断启动或者禁用这个View

* @paramalpha 是否需要设置透明度

*/

publicTextInputHelper(View view, booleanalpha){

if(view == null) thrownewIllegalArgumentException( "The view is empty");

mMainView = view;

isAlpha = alpha;

}

/**

* 添加EditText或者TextView监听

*

* @paramviews 传入单个或者多个EditText或者TextView对象

*/

publicvoidaddViews(TextView... views){

if(views == null) return;

if(mViewSet == null) {

mViewSet = newArrayList<>(views.length - 1);

}

for(TextView view : views) {

view.addTextChangedListener( this);

mViewSet.add(view);

}

afterTextChanged( null);

}

/**

* 移除EditText监听,避免内存泄露

*/

publicvoidremoveViews(){

if(mViewSet == null) return;

for(TextView view : mViewSet) {

view.removeTextChangedListener( this);

}

mViewSet.clear();

mViewSet = null;

}

// TextWatcher

@Override

publicvoidbeforeTextChanged(CharSequence s, intstart, intcount, intafter){}

@Override

publicvoidonTextChanged(CharSequence s, intstart, intbefore, intcount){}

@Override

publicsynchronizedvoidafterTextChanged(Editable s){

if(mViewSet == null) return;

for(TextView view : mViewSet) {

if( "".equals(view.getText().toString())) {

setEnabled( false);

return;

}

}

setEnabled( true);

}

/**

* 设置View的事件

*

* @paramenabled 启用或者禁用View的事件

*/

publicvoidsetEnabled(booleanenabled){

if(enabled == mMainView.isEnabled()) return;

if(enabled) {

//启用View的事件

mMainView.setEnabled( true);

if(isAlpha) {

//设置不透明

mMainView.setAlpha( 1f);

}

} else{

//禁用View的事件

mMainView.setEnabled( false);

if(isAlpha) {

//设置半透明

mMainView.setAlpha( 0.5f);

}

}

}

}

在Activity创建时添加监听

privateTextInputHelper mInputHelper;

@Override

protectedvoidonCreate(Bundle savedInstanceState){

super.onCreate(savedInstanceState);

//创建一个辅助类,传入按钮操作View

mInputHelper = newTextInputHelper(mButton);

mInputHelper.addViews(mEditText1, mEditText2, mEditText3);

}

在Activity销毁时移除监听(避免内存泄露)

@Override

protectedvoidonDestroy(){

super.onDestroy();

//移除引用,避免内存泄露

mInputHelper.removeViews();

}

需要注意的是这里不单单只是可以添加EditText,还可以添加TextView,因为EditText是TextView的子类,最后要是觉得好用记得点个赞。

责任编辑:

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值