自定義Button類
public class EditTextButtonextends androidx.appcompat.widget.AppCompatButton {
//繼承Button實現(xiàn)的3個構(gòu)造方法
? ? public EditTextButton(Context context) {
super(context);
}
public EditTextButton(Context context, AttributeSet attrs) {
super(context, attrs);
}
public EditTextButton(Context context, AttributeSet attrs,int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public void bindEditText(EditText editText) {
/*只要條件滿足就設(shè)置啟用*/
? ? ? ? setEnabled(editText.getText().toString().length() >0);
/*添加文本更改監(jiān)聽器*/
? ? ? ? editText.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) {
/*文本之后改變了*/
? ? ? ? ? ? ? ? /*設(shè)置啟用*/
? ? ? ? ? ? ? ? setEnabled(editText.getText().toString().length() >0);
}
});
}
}
<!--設(shè)置點(diǎn)擊登錄的樣式-->
<style name="Jiandao" parent="AppTheme"/>
? ? <style name="Jiandao.Button_style1">
? ? ? ? <item name="android:layout_width">0dp</item>
? ? ? ? <item name="android:layout_height">wrap_content</item>
? ? ? ? <item name="android:background">@drawable/ic_button_style1_background_selector</item>
? ? ? ? <item name="android:textColor">@color/white</item>
讓我們設(shè)置登錄把
<com.m.k.seetaoism.widgets.EditTextButton
? ? android:id="@+id/auth_password_login_btn_login"
? ? style="@style/Jiandao.Button_style1"
? ? android:layout_marginTop="50dp"
? ? android:text="@string/text_login"
? ? app:layout_constraintEnd_toEndOf="@+id/auth_password_login_edt_password"
? ? app:layout_constraintStart_toStartOf="@+id/auth_password_login_edt_password"
? ? app:layout_constraintTop_toBottomOf="@+id/imageView3" />
?選擇器
<selector xmlns:android="http://schemas.android.com/apk/res/android">
? ? <!--剛啟用的狀態(tài)-->
? ? <item android:state_enabled="false">
? ? ? ? <!--形狀“長方形”-->
? ? ? ? <shape android:shape="rectangle">
? ? ? ? ? ? <!--設(shè)置他的角度=半徑是+-->
? ? ? ? ? ? <corners android:radius="22dp" />
? ? ? ? ? ? <!--實線的顏色-->
? ? ? ? ? ? <solid android:color="@color/gray_3" />
? ? ? ? </shape>
? ? </item>
? ? <!--按下去的狀態(tài)的狀態(tài)-->
? ? <item android:state_pressed="true">
? ? ? ? <!--形狀“長方形”-->
? ? ? ? <shape android:shape="rectangle">
? ? ? ? ? ? <!--設(shè)置他的角度=半徑是+-->
? ? ? ? ? ? <corners android:radius="22dp" />
? ? ? ? ? ? <!--實線的顏色-->
? ? ? ? ? ? <solid android:color="@color/gray_3" />
? ? ? ? </shape>
? ? </item>
? ? <item>
? ? ? ? <!--當(dāng)某種條件觸發(fā)的狀態(tài)-->
? ? ? ? <shape android:shape="rectangle">
? ? ? ? ? ? <corners android:radius="22dp" />
? ? ? ? ? ? <solid android:color="@android:color/black" />
? ? ? ? </shape>
? ? </item>
</selector>
介紹:這個那就是設(shè)置了button的三種狀態(tài):點(diǎn)擊宛渐,未點(diǎn)擊狠轻,不讓點(diǎn)擊