我想从下拉菜单中添加国家代码

时间:2018-07-21 13:04:54

标签: java android

我该怎么做,我在StackOverflow上进行了搜索,但是解决方案适用于iPhone,而不是Android。 这是我的聊天应用程序:https://play.google.com/store/apps/details?id=com.jimmytrivedi.lapitchat

package com.jimmytrivedi.lapitchat;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.FirebaseException;
import com.google.firebase.FirebaseTooManyRequestsException;
import com.google.firebase.analytics.FirebaseAnalytics;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseAuthInvalidCredentialsException;
import com.google.firebase.auth.FirebaseUser;
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;
    private FirebaseAnalytics mFirebaseAnalytics;
    private String mVerificationId;
    private PhoneAuthProvider.ForceResendingToken mResendToken;
    private FirebaseAuth mAuth;
    private TextView ErrorView;
    private int Verify = 0;

    @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);
        ErrorView = findViewById(R.id.ErrorView);

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

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

        mFirebaseAnalytics = FirebaseAnalytics.getInstance(this);
        mAuth = FirebaseAuth.getInstance();

        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(Verify == 0) {

                    if (!PhoneNumber.getText().toString().isEmpty()) {
                        number = PhoneNumber.getText().toString();
                        PhoneNumber.setEnabled(false);
                        sendVerification.setEnabled(false);
                        PhoneProgress.setVisibility(View.VISIBLE);


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

                    }
                } else {
                    String VerificationCode = code.getText().toString();
                    sendVerification.setEnabled(false);
                    CodeProgress.setVisibility(View.VISIBLE);
                    PhoneAuthCredential credential = PhoneAuthProvider.getCredential(
                            mVerificationId,VerificationCode);
                    signInWithPhoneAuthCredential(credential);
                    }
            }
        });

        mCallbacks = new PhoneAuthProvider.OnVerificationStateChangedCallbacks() {
            @Override
            public void onVerificationCompleted(PhoneAuthCredential phoneAuthCredential) {
                PhoneProgress.setVisibility(View.INVISIBLE);
                signInWithPhoneAuthCredential(phoneAuthCredential);
            }

            @Override
            public void onVerificationFailed(FirebaseException e) {

                ErrorView.setText("There was some error in verification");
                ErrorView.setVisibility(View.VISIBLE);

                if (e instanceof FirebaseAuthInvalidCredentialsException) {
                    Log.d("wihddiewd", "FirebaseAuthInvalidCredentialsException: " + e);

                } else if (e instanceof FirebaseTooManyRequestsException) {
                    Log.d("wihddiewd", "FirebaseTooManyRequestsException: " + e);
                }

            }

            @Override
            public void onCodeSent(String verificationId,
                                   PhoneAuthProvider.ForceResendingToken token) {

                Log.d("wihddiewd", "onCodeSent:" + verificationId);


                mVerificationId = verificationId;
                mResendToken = token;
                Verify=1;
                PhoneProgress.setVisibility(View.INVISIBLE);
                LockLayout.setVisibility(View.VISIBLE);
                sendVerification.setEnabled(true);
                sendVerification.setText("Verify code");

            }
        };
    }

    private void signInWithPhoneAuthCredential(PhoneAuthCredential credential) {
        mAuth.signInWithCredential(credential)
                .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if (task.isSuccessful()) {
                            FirebaseUser user = task.getResult().getUser();
                            startActivity(new Intent(PhoneAuthActivity.this, MainActivity.class));
                            finish();
                        } else {
                            ErrorView.setText("There was some error in logging in");
                            ErrorView.setVisibility(View.VISIBLE);
                            Log.w("wihddiewd", "signInWithCredential:failure", task.getException());
                            if (task.getException() instanceof FirebaseAuthInvalidCredentialsException) {
                            }
                        }
                    }
                });
    }

}

我不知道什么类型的代码是必需的,我应该在哪里添加?

1 个答案:

答案 0 :(得分:1)

试试这个库: CountryCodePicker

 dependencies {
      implementation 'com.hbb20:ccp:2.2.2'
    }

在XML中

<com.hbb20.CountryCodePicker
                    android:id="@+id/country_picker"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true"
                    android:gravity="center|center_vertical"
                    android:includeFontPadding="false"
                    android:textColor="@color/text_black"
                    app:ccp_showNameCode="false"
                    app:ccp_textSize="@dimen/small_text_size" />

活动中

CountryCodePicker country_picker = findViewById(R.id.country_picker);
String country_code = country_picker.getSelectedCountryCode();