Java 即使处于有效状态,按钮也未禁用
在我的聊天应用程序中,我添加了来自用户的电话验证。因此,我希望当用户输入号码时,sendVerification按钮应该是Java 即使处于有效状态,按钮也未禁用,java,android,firebase-authentication,Java,Android,Firebase Authentication,在我的聊天应用程序中,我添加了来自用户的电话验证。因此,我希望当用户输入号码时,sendVerification按钮应该是setEnabled(true),我将setEnabled(false)设置为默认值,但当我运行应用程序时,它仍然显示为enabled(已启用),而不输入数字 活动\u电话\u auth.xml <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schem
setEnabled(true)
,我将setEnabled(false)
设置为默认值,但当我运行应用程序时,它仍然显示为enabled(已启用),而不输入数字
活动\u电话\u auth.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".PhoneAuthActivity">
<include
android:id="@+id/PhoneToolbar"
layout="@layout/app_bar">
</include>
<LinearLayout
android:id="@+id/DialLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/PhoneToolbar"
android:orientation="horizontal"
android:weightSum="10">
<ImageView
android:id="@+id/dial"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:padding="10dp"
android:src="@drawable/dial"
android:layout_weight="1"/>
<EditText
android:id="@+id/PhoneNumber"
android:layout_width="270dp"
android:layout_height="wrap_content"
android:hint="Phone Number"
android:layout_weight="8"
android:ems="10"
android:inputType="phone"/>
<ProgressBar
android:id="@+id/PhoneProgress"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:id="@+id/LockLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/DialLayout"
android:orientation="horizontal"
android:weightSum="10">
<ImageView
android:id="@+id/lock"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:padding="10dp"
android:src="@drawable/lock"
android:layout_weight="1"/>
<EditText
android:id="@+id/code"
android:layout_width="270dp"
android:layout_height="wrap_content"
android:hint="Verification Code"
android:layout_weight="8"/>
<ProgressBar
android:id="@+id/CodeProgress"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
<TextView
android:id="@+id/VerificationText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="91dp"
android:text="A verification code will be sent to your phone number" />
<Button
android:id="@+id/sendVerification"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="23dp"
android:backgroundTint="#FF0000"
android:text="Send Verification" />
</RelativeLayout>
替换
number = PhoneNumber.getText().toString();
if (number != null) {
sendVerification.setEnabled(true);
} else {
sendVerification.setEnabled(false);
}
与:
默认情况下,在
onCreate(…)
方法中设置sendVerification.setEnabled(false)
,然后像这样应用TextChangedListener
PhoneNumber.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
if (charSequence.toString().equalsIgnoreCase("")) {
sendVerification.setEnabled(false);
} else {
sendVerification.setEnabled(true);
}
}
@Override
public void afterTextChanged(Editable editable) {
}
});
每当PhoneNumber为空时,它将自动禁用sendVerification按钮。请使用TextWatcher()了解用户何时在编辑文本中输入文本。这样做:-
替换此项:-
number = PhoneNumber.getText().toString();
if (number != null) {
sendVerification.setEnabled(true);
}
else {
sendVerification.setEnabled(false);
}
鉴于此:
PhoneNumber.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
String text = charSequence.toString();
if(!text.isEmpty()){
sendVerification.setEnabled(true);
}
}
@Override
public void afterTextChanged(Editable editable) {
}
});
sendVerification.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(!PhoneNumber.getText().toString().isEmpty()){
number = PhoneNumber.getText().toString();
PhoneNumber.setEnabled(false);
PhoneProgress.setVisibility(View.VISIBLE);
PhoneAuthProvider.getInstance().verifyPhoneNumber(
number,
60,
TimeUnit.SECONDS,
PhoneAuthActivity.this,
mCallbacks
);
}
else {
Toast.makeText(PhoneAuthActivity.this, "Empty Field", Toast.LENGTH_SHORT).show();
}
}
});
number=PhoneNumber.getText().toString();如果(number!=null){sendVerification.setEnabled(true);}否则{sendVerification.setEnabled(false);}=>toString将永远不会返回null,因此这将始终将其设置为true,我想你的意思是!isEmpty(…)我的意思是,若用户并没有输入任何内容,那个么它就不应该允许点击按钮。我该怎么办?您应该使用isEmpty作为检查,而不是==或!=无效的Sotiris的回答基本上就是我的意思,不过我想你会需要更多的验证,比如说!isEmpty()&&isValidNumber()(或类似的东西)它不工作定义“不工作”。你做了什么改变?你有错误吗?调试好了吗?不需要if-else语句,只需将number.isEmpty()作为parameter@Stultuske在TextWatcher之后没有得到OTP我应该在onCreate中做什么?是的。您在OnCreate()中编写的上述代码,只需将其替换为我的代码即可。Get cdrash,logcat error此行是sendVerification.setOnClickListener(new View.OnClickLisI已更新我的答案。请查看@RuchikaSingh.Fetch number from PhoneNumber in afterTextChanged()中的方法。我实现了这个。应用程序现在没有崩溃。但我没有得到OTP在TextWatcher之后没有得到OTP。你从服务器上得到OTP吗?不,firebase方法查看此视频上的所有评论:我想我必须使用SHANope,我必须在firebase中添加Sha 1吗?
number = PhoneNumber.getText().toString();
if (number != null) {
sendVerification.setEnabled(true);
}
else {
sendVerification.setEnabled(false);
}
PhoneNumber.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
String text = charSequence.toString();
if(!text.isEmpty()){
sendVerification.setEnabled(true);
}
}
@Override
public void afterTextChanged(Editable editable) {
}
});
sendVerification.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(!PhoneNumber.getText().toString().isEmpty()){
number = PhoneNumber.getText().toString();
PhoneNumber.setEnabled(false);
PhoneProgress.setVisibility(View.VISIBLE);
PhoneAuthProvider.getInstance().verifyPhoneNumber(
number,
60,
TimeUnit.SECONDS,
PhoneAuthActivity.this,
mCallbacks
);
}
else {
Toast.makeText(PhoneAuthActivity.this, "Empty Field", Toast.LENGTH_SHORT).show();
}
}
});