简体   繁体   English

按钮即使处于有效状态也不会禁用

[英]Button is not disabling even it is in valid condition

In my chat app, I'm adding Phone verification from user. 在我的聊天应用中,我要添加用户的电话验证。 So I want that when user will enter the number, after that sendVerification button should be setEnabled(true) and I set setEnabled(false) as default, but when I run the app, it is still showing as enabled without entering the digits. 所以我希望当用户输入数字时,在sendVerification按钮应该设置为setEnabled(true)并将setEnabled(false)设置为默认值之后,但是当我运行该应用程序时,它仍显示为启用状态,而无需输入数字。

activity_phone_auth.xml activity_phone_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>

PhoneAuthActivity.java PhoneAuthActivity.java

package com.jimmytrivedi.lapitchat;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ProgressBar;

import com.google.firebase.FirebaseException;
import com.google.firebase.auth.PhoneAuthCredential;
import com.google.firebase.auth.PhoneAuthProvider;

import java.util.concurrent.TimeUnit;

public class PhoneAuthActivity extends AppCompatActivity {

    private LinearLayout DialLayout, LockLayout;
    private EditText PhoneNumber, code;
    private ProgressBar PhoneProgress, CodeProgress;
    private Button sendVerification;
    private Toolbar PhoneToolbar;
    private PhoneAuthProvider.OnVerificationStateChangedCallbacks mCallbacks;
    private String number;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_phone_auth);

        DialLayout = findViewById(R.id.DialLayout);
        LockLayout = findViewById(R.id.LockLayout);
        PhoneNumber = findViewById(R.id.PhoneNumber);
        code = findViewById(R.id.code);
        PhoneProgress = findViewById(R.id.PhoneProgress);
        CodeProgress = findViewById(R.id.CodeProgress);
        sendVerification = findViewById(R.id.sendVerification);
        PhoneToolbar = findViewById(R.id.PhoneToolbar);

        PhoneProgress.setVisibility(View.INVISIBLE);
        CodeProgress.setVisibility(View.INVISIBLE);
        sendVerification.setEnabled(false);

        setSupportActionBar(PhoneToolbar);
        getSupportActionBar().setTitle("Welcome to Phone Verification");
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        number = PhoneNumber.getText().toString();
        if (number != null) {
            sendVerification.setEnabled(true);
        } else {
            sendVerification.setEnabled(false);
        }

        sendVerification.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                PhoneNumber.setEnabled(false);
                PhoneProgress.setVisibility(View.VISIBLE);


                PhoneAuthProvider.getInstance().verifyPhoneNumber(
                        number,
                        60,
                        TimeUnit.SECONDS,
                        PhoneAuthActivity.this,
                        mCallbacks
                );
            }
        });


        mCallbacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
            @Override
            public void onVerificationCompleted(PhoneAuthCredential phoneAuthCredential) {

            }

            @Override
            public void onVerificationFailed(FirebaseException e) {

            }
        };
    }
}

Replace 更换

number = PhoneNumber.getText().toString();
        if (number != null) {
            sendVerification.setEnabled(true);
        } else {
            sendVerification.setEnabled(false);
        }

with: 与:

number = PhoneNumber.getText().toString();
        if (number.isEmpty()) {
            sendVerification.setEnabled(false);
        } else {
            sendVerification.setEnabled(true);
        }

Set sendVerification.setEnabled(false) by default in onCreate(...) method and then apply TextChangedListener like this 默认在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) {

            }
        });

It will automatically disable sendVerification Button whenever PhoneNumber is blank 每当PhoneNumber为空时,它将自动禁用sendVerification按钮

Use a TextWatcher() to know when the user enters the text in edit text. 使用TextWatcher()可以知道用户何时在编辑文本中输入文本。 Do like this:- 这样做:

Replace this:- 替换为:-

number = PhoneNumber.getText().toString();
if (number != null) {
    sendVerification.setEnabled(true);
}
else {
    sendVerification.setEnabled(false);
}

with this:- 有了这个:-

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();

        }
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM