Java 与简单条件混淆
我不知道为什么我会与简单的if/else条件混淆?在我的应用程序,手机验证部分,默认情况下,我设置sendVerification按钮禁用,当我输入文本时,它应该启用。但它不是使能的吗?我的情况怎么了?即使我尝试其他部分,同样的问题 XMLJava 与简单条件混淆,java,android,firebase-realtime-database,Java,Android,Firebase Realtime Database,我不知道为什么我会与简单的if/else条件混淆?在我的应用程序,手机验证部分,默认情况下,我设置sendVerification按钮禁用,当我输入文本时,它应该启用。但它不是使能的吗?我的情况怎么了?即使我尝试其他部分,同样的问题 XML <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xm
<?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>
在您有机会开始键入之前,这些都是在onCreate中执行的。一旦开始键入,此代码将不再执行。它仅在创建活动时执行一次
解决方案:在电话号码editText上设置TextWatcher,并在TextWatcher#afterTextChanged方法中启用/禁用sendVerification。有关如何执行此操作的说明,请参阅。您需要一个TextWatcher来执行您希望执行的操作,因此请向EditText/TextView添加一个TextWatcher,如下所示:
PhoneNumber.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) {
if (s.length()>0) {
sendVerification.setEnabled(true);
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
代码无法工作的原因是,在开始键入后不会调用onCreate()方法,这意味着启用视图的条件只检查一次,因此不会再次检查代码中的任何更改。
number = PhoneNumber.getText().toString();
if (!number.isEmpty()) {
sendVerification.setEnabled(true);
}
一次仅执行一次。您必须添加一个文本更改侦听器,并相应地启用/禁用该按钮
像
}
}); 请给我发exac代码,我不知道TextWatcher让你自己知道。我不会用勺子给你密码的。只需跟随链接,我不会在应用TextWatcher后获得OTP。@PalakSingh这是另一个问题。针对当前正在创建时将属性“enabled”设置为false的问题提出一个新问题。我认为您在输入中缺少一个事件侦听器,您可以在其中键入数据(电话号码),这将根据您的情况启用您的按钮。我在applied TextWatcher之后没有得到OTP。我在applied TextWatcher之后没有得到OTP。您的意思是什么?您能给我们提供更多详细信息吗?它将进入:public void onVerificationFailed(FirebaseException e){未获得OTP您可以将您的行号=PhoneNumber.getText().toString();移动到SendVerificationOnClickListener吗?我在应用TextWatcher后没有获得OTP。
PhoneNumber.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) {
if (s.length()>0) {
sendVerification.setEnabled(true);
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
number = PhoneNumber.getText().toString();
if (!number.isEmpty()) {
sendVerification.setEnabled(true);
}
PhoneNumber.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {}
@Override
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start,
int before, int count) {
if(s.length() > 0)
{
sendVerification.setEnabled(true);
}