先上图:
具体实现:
1.先看我们的工具类:
//根据EditText判断按钮是否可点击
public class EditIsCanUseBtnUtils {
private List<EditText> editTextList = new ArrayList<>();
private Button btn;
private Context context;
public static EditIsCanUseBtnUtils getInstance(){
return new EditIsCanUseBtnUtils();
}
public EditIsCanUseBtnUtils setBtn(Button btn){
this.btn = btn;
btn.setEnabled(false);
return this;
}
public EditIsCanUseBtnUtils addEdittext(EditText editText){
editTextList.add(editText);
return this;
}
public EditIsCanUseBtnUtils addText(String text){
btn.setText(text);
return this;
}
public EditIsCanUseBtnUtils addContext(Context context){
this.context = context;
return this;
}
public void build(){
setWatcher();
}
private void setWatcher() {
for (int i = 0; i < editTextList.size(); i++) {
editTextList.get(i).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) {
if (s.length() == 0){
setBtnUnavailable();
return;
}
boolean tag = false;
for (int i1 = 0; i1 < editTextList.size(); i1++) {
if (editTextList.get(i1).getText().length()!=0){
tag = true;
}else {
tag = false;
break;
}
}
if (tag){
setBtnAvailable();
}else {
setBtnUnavailable();
}
}
});
}
}
private void setBtnAvailable(){
btn.setEnabled(true);
}
private void setBtnUnavailable(){
btn.setEnabled(false);
}
}
代码并不难,我们将EditText对象传递进来,根据afterTextChanged和判断EditText中内容的长度设置Tag值,根据Tag判断要执行的方法,这里的方法为设置Button可否被点击。
2.在使用上:先获取工具类的实例,使用addEdittext传入 EditText,使用setBtn传入Button,这里的addContext在这里目前没有用上,可以不传,最后 .build,需要写在我们的onCreate生命周期中。
//获取验证码
EditIsCanUseBtnUtils.getInstance().addEdittext(etRegsterPhone)
.addEdittext(etRegisterPassWore)
.setBtn(btnRegisterNum)
.addContext(this)
.build();
3.我们还需要写选择器和shape:
在drawable下:
selector:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/shapebutton_null" android:state_enabled="false"/>
<item android:drawable="@drawable/shapebutton" android:state_enabled="true"/>
</selector>
shape:
shapebutton_null:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<!-- 填充的颜色 -->
<solid android:color="@color/color_null"/>
<!-- 设置按钮的四个角为弧形 -->
<!-- android:radius 弧形的半径 -->
<corners android:radius="7dip" />
<!--边框的宽度及颜色-->
<stroke android:width="2px" android:color="@color/color_null" />
</shape>
shapebutton:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<!-- 填充的颜色 -->
<solid android:color="@color/color_button"/>
<!-- 设置按钮的四个角为弧形 -->
<!-- android:radius 弧形的半径 -->
<corners android:radius="7dip" />
<!--边框的宽度及颜色-->
<stroke android:width="2px" android:color="@color/color_button" />
</shape>
我们的Button:
<Button
android:enabled="false"
android:background="@drawable/button_register_background"
android:layout_width="wrap_content"
android:layout_height="40dp"
/>
上面是实现这种效果的第一种方式,有的小伙伴觉得,这样使用有点繁琐,那么,下面介绍一种改进的方法:
还是我们的工具类,我们做一些改动:
//根据EditText判断按钮是否可点击(Null代表第一中状态,Full代表第二种状态)
public class EditIsCanUseBtnUtils {
private List<EditText> editTextList = new ArrayList<>();
private Button btn;
private Context context;
private String textNull,textFull;
private Integer colorNull,colorFull;
private Integer backgroundResourceNull,backgroundResourceFull;
//实例化
public static EditIsCanUseBtnUtils getInstance(){
return new EditIsCanUseBtnUtils();
}
//Null时候的颜色
public EditIsCanUseBtnUtils setNullbackgroundResource(Integer backgroundResourceNull){
this.backgroundResourceNull = backgroundResourceNull;
return this;
}
//Full时候的颜色
public EditIsCanUseBtnUtils setFullbackgroundResource(Integer backgroundResourceFull){
this.backgroundResourceFull = backgroundResourceFull;
return this;
}
//Null时候的颜色
public EditIsCanUseBtnUtils setNullColor(Integer colorNull){
this.colorNull = colorNull;
return this;
}
//Full时候的颜色
public EditIsCanUseBtnUtils setFullColor(Integer colorFull){
this.colorFull = colorFull;
return this;
}
//绑定的Button
public EditIsCanUseBtnUtils addButton(Button btn){
this.btn = btn;
btn.setEnabled(false);
return this;
}
//添加EditText
public EditIsCanUseBtnUtils addEditext(EditText editText){
editTextList.add(editText);
return this;
}
//给Button设置文字
public EditIsCanUseBtnUtils setText(String text){
btn.setText(text);
return this;
}
public EditIsCanUseBtnUtils setTextNull(String textNull){
this.textNull = textNull;
return this;
}
public EditIsCanUseBtnUtils setTextFull(String textFull){
this.textFull = textFull;
return this;
}
//添加Context对象
public EditIsCanUseBtnUtils setContext(Context context){
this.context = context;
return this;
}
//创建
public void build(){
setWatcher();
}
private void setWatcher() {
for (int i = 0; i < editTextList.size(); i++) {
editTextList.get(i).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) {
if (s.length() == 0){
setBtnUnavailable();
return;
}
boolean tag = false;
for (int i1 = 0; i1 < editTextList.size(); i1++) {
if (editTextList.get(i1).getText().length()!=0){
tag = true;
}else {
tag = false;
break;
}
}
if (tag){
setBtnAvailable();
}else {
setBtnUnavailable();
}
}
});
}
}
//Full时
private void setBtnAvailable(){
//颜色
if (colorFull!= null){
btn.setBackgroundColor(colorFull);
}
//文字
if (!TextUtils.isEmpty(textFull)){
btn.setText(textFull);
}
//背景
if (backgroundResourceFull!= null){
btn.setBackgroundResource(backgroundResourceFull);
}
btn.setEnabled(true);
}
//Null时
private void setBtnUnavailable(){
if (colorNull!= null){
btn.setBackgroundColor(colorNull);
}
if (!TextUtils.isEmpty(textNull)){
btn.setText(textNull);
}
if (backgroundResourceNull!= null){
btn.setBackgroundResource(backgroundResourceNull);
}
btn.setEnabled(false);
}
}
先看我们的:
private List<EditText> editTextList = new ArrayList<>();
private Button btn;
private Context context;
private String textNull,textFull;
private Integer colorNull,colorFull;
private Integer backgroundResourceNull,backgroundResourceFull;
editTextList 和 button 和上面介绍的是一样的。
textNull : 进入时,默认的文字。
textFull : 每个Edittext都有内容时的文字。
colorNull : 未填满时候的按钮颜色。
colorFull : 填满时候的按钮的颜色。
backgroundResourceNull:未填满时候的 shape
backgroundResourceFull :填满时候的 shape其他内部逻辑并无却别,只不过是添加了这些方法,方便使用:
在使用的时候:
EditIsCanUseBtnUtils.getInstance().addEditext(etAccount)
.addEditext(etPassWord)
.addButton(btnLogin)
.setText("默认字段")
.setTextNull("未填全时候的字段")
.setTextFull("填写全时候 的字段")
// .setNullColor(getResources().getColor(R.color.colorAccent))
// .setFullColor(getResources().getColor(R.color.colorPrimary))
.setNullbackgroundResource(R.drawable.shapebutton)
.setFullbackgroundResource(R.drawable.shapebutton_null)
.build();
这样,我们就不用写选择器了,直接调用对应的方法,就可以实现我们想要的功能了。