Android仿真器设计与XML设计不同

zsohkypk  于 2022-10-07  发布在  Android
关注(0)|答案(1)|浏览(133)

在使用IntelliJ Idea 2022.2.2构建Android应用程序时,我发现XML设计和仿真器设计是不同的。我已将模拟器从Pixel_2_API_24更改为Nexus_5X_API_32,但问题仍然存在。我已经附上了问题的图像、我的Java文件和XML文件。我希望有人能帮上忙。

问题

Java文件

public class LoginActivity extends AppCompatActivity {

    private EditText email, pass;
    private Button loginB;
    private TextView forgotpassB, signupB;
    private FirebaseAuth mAuth;
    private Dialog progressDialog;
    private TextView dialogText;
    private RelativeLayout gSignB;
    private GoogleSignInClient mGoogleSignInClient;
    private int RC_SIGN_IN = 104;

    @SuppressLint("SourceLockedOrientationActivity")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        email = findViewById(R.id.email);
        pass = findViewById(R.id.password);
        loginB = findViewById(R.id.LoginB);
        forgotpassB = findViewById(R.id.forgot_pass);
        signupB = findViewById(R.id.signupB);
        gSignB = findViewById(R.id.g_signB);

        progressDialog = new Dialog(LoginActivity.this);
        progressDialog.setContentView(R.layout.dialog_layout);
        progressDialog.setCancelable(false);
        progressDialog.getWindow().setLayout(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);

        dialogText = progressDialog.findViewById(R.id.dialog_text);
        dialogText.setText("Signing in...");

        mAuth = FirebaseAuth.getInstance();

        // Configure Google Sign In
        GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestIdToken(getString(R.string.default_web_client_id))
                .requestEmail()
                .build();

        forgotpassB.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(LoginActivity.this, ForgotPassword.class);
                startActivity(intent);
            }
        });

        mGoogleSignInClient = GoogleSignIn.getClient(this,gso);

        loginB.setOnClickListener((view) -> {

            if(validateData())
            {
                login();
            }

        });

        signupB.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent (LoginActivity.this, SignUpActivity.class);
                startActivity(intent);
            }
        });

        gSignB.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                googleSignIn();
            }
        });

    }

    private boolean validateData()
    {

        if (email.getText().toString().isEmpty()) {
            email.setError("Enter E-Mail Id");
            return false;
        }

        if (pass.getText().toString().isEmpty()) {
            pass.setError("Enter Password");
            return false;
        }

        return true;

    }

        private void login()
    {
        progressDialog.show();

        mAuth.signInWithEmailAndPassword(email.getText().toString().trim(), pass.getText().toString().trim())
                .addOnCompleteListener(this, task -> {
                    if (task.isSuccessful()) {
                            Toast.makeText(LoginActivity.this,"Sign in Successful", Toast.LENGTH_SHORT).show();

                            DbQuery.loadData(new MyCompleteListener() {
                                @Override
                                public void onSuccess() {
                                    progressDialog.dismiss();

                                    Intent intent = new Intent(LoginActivity.this,MainActivity.class);
                                    startActivity(intent);
                                    finish();
                                }

                                @Override
                                public void onFailure() {

                                    progressDialog.dismiss();
                                    Toast.makeText(LoginActivity.this,"Something went Wrong! Try again later",
                                            Toast.LENGTH_SHORT).show();
                                }
                            });

                        } else{
                            progressDialog.dismiss();
                            Toast.makeText(LoginActivity.this,task.getException().getMessage(),
                                    Toast.LENGTH_SHORT).show();
                        }

                });
    }

    private void googleSignIn()
    {
        Intent signInIntent = mGoogleSignInClient.getSignInIntent();
        startActivityForResult(signInIntent, RC_SIGN_IN);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        // Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
        if (requestCode == RC_SIGN_IN) {
            Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
            try {
                // Google Sign In was successful, authenticate with Firebase
                GoogleSignInAccount account = task.getResult(ApiException.class);
                //Log.d(TAG, "firebaseAuthWithGoogle:" + account.getId());
                firebaseAuthWithGoogle(account.getIdToken());

            } catch (ApiException e) {

                Toast.makeText(LoginActivity.this,e.getMessage(),Toast.LENGTH_SHORT).show();

            }
        }
    }

    private void firebaseAuthWithGoogle(String idToken) {

        progressDialog.show();

        AuthCredential credential = GoogleAuthProvider.getCredential(idToken,null);
        mAuth.signInWithCredential(credential)
                .addOnCompleteListener(this, task -> {
                    if (task.isSuccessful()) {

                        Toast.makeText(LoginActivity.this,"Google Sign In Successful",Toast.LENGTH_SHORT).show();

                        FirebaseUser user = mAuth.getCurrentUser();

                        if(task.getResult().getAdditionalUserInfo().isNewUser())
                        {
                            DbQuery.createUserData(user.getEmail(), user.getDisplayName(), new MyCompleteListener() {
                                @Override
                                public void onSuccess() {

                                    DbQuery.loadData(new MyCompleteListener() {
                                        @Override
                                        public void onSuccess() {
                                            progressDialog.dismiss();

                                            Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                                            startActivity(intent);
                                            LoginActivity.this.finish();
                                        }

                                        @Override
                                        public void onFailure() {

                                            progressDialog.dismiss();
                                            Toast.makeText(LoginActivity.this,"Something went Wrong! Try again later",
                                                    Toast.LENGTH_SHORT).show();
                                        }
                                    });

                                }

                                @Override
                                public void onFailure() {

                                    progressDialog.dismiss();
                                    Toast.makeText(LoginActivity.this,"Something went Wrong! Try again later",
                                            Toast.LENGTH_SHORT).show();

                                }
                            });
                        }
                        else
                        {
                            DbQuery.loadData(new MyCompleteListener() {
                                @Override
                                public void onSuccess() {
                                    progressDialog.dismiss();

                                    Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                                    startActivity(intent);
                                    LoginActivity.this.finish();
                                }

                                @Override
                                public void onFailure() {
                                    progressDialog.dismiss();
                                    Toast.makeText(LoginActivity.this,"Something went Wrong! Try again later",
                                            Toast.LENGTH_SHORT).show();
                                }
                            });
                        }

                    } else {

                        progressDialog.dismiss();
                        Toast.makeText(LoginActivity.this, task.getException().getMessage(),
                                Toast.LENGTH_SHORT).show();

                    }

                });

    }

}
package com.example.exampros;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import com.google.android.gms.auth.api.signin.GoogleSignIn;
import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
import com.google.android.gms.auth.api.signin.GoogleSignInClient;
import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
import com.google.android.gms.common.api.ApiException;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.*;

public class LoginActivity extends AppCompatActivity {

    private EditText email, pass;
    private Button loginB;
    private TextView forgotpassB, signupB;
    private FirebaseAuth mAuth;
    private Dialog progressDialog;
    private TextView dialogText;
    private RelativeLayout gSignB;
    private GoogleSignInClient mGoogleSignInClient;
    private int RC_SIGN_IN = 104;

    @SuppressLint("SourceLockedOrientationActivity")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        email = findViewById(R.id.email);
        pass = findViewById(R.id.password);
        loginB = findViewById(R.id.LoginB);
        forgotpassB = findViewById(R.id.forgot_pass);
        signupB = findViewById(R.id.signupB);
        gSignB = findViewById(R.id.g_signB);

        progressDialog = new Dialog(LoginActivity.this);
        progressDialog.setContentView(R.layout.dialog_layout);
        progressDialog.setCancelable(false);
        progressDialog.getWindow().setLayout(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);

        dialogText = progressDialog.findViewById(R.id.dialog_text);
        dialogText.setText("Signing in...");

        mAuth = FirebaseAuth.getInstance();

        // Configure Google Sign In
        GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestIdToken(getString(R.string.default_web_client_id))
                .requestEmail()
                .build();

        forgotpassB.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(LoginActivity.this, ForgotPassword.class);
                startActivity(intent);
            }
        });

        mGoogleSignInClient = GoogleSignIn.getClient(this,gso);

        loginB.setOnClickListener((view) -> {

            if(validateData())
            {
                login();
            }

        });

        signupB.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent (LoginActivity.this, SignUpActivity.class);
                startActivity(intent);
            }
        });

        gSignB.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                googleSignIn();
            }
        });

    }

    private boolean validateData()
    {

        if (email.getText().toString().isEmpty()) {
            email.setError("Enter E-Mail Id");
            return false;
        }

        if (pass.getText().toString().isEmpty()) {
            pass.setError("Enter Password");
            return false;
        }

        return true;

    }

        private void login()
    {
        progressDialog.show();

        mAuth.signInWithEmailAndPassword(email.getText().toString().trim(), pass.getText().toString().trim())
                .addOnCompleteListener(this, task -> {
                    if (task.isSuccessful()) {
                            Toast.makeText(LoginActivity.this,"Sign in Successful", Toast.LENGTH_SHORT).show();

                            DbQuery.loadData(new MyCompleteListener() {
                                @Override
                                public void onSuccess() {
                                    progressDialog.dismiss();

                                    Intent intent = new Intent(LoginActivity.this,MainActivity.class);
                                    startActivity(intent);
                                    finish();
                                }

                                @Override
                                public void onFailure() {

                                    progressDialog.dismiss();
                                    Toast.makeText(LoginActivity.this,"Something went Wrong! Try again later",
                                            Toast.LENGTH_SHORT).show();
                                }
                            });

                        } else{
                            progressDialog.dismiss();
                            Toast.makeText(LoginActivity.this,task.getException().getMessage(),
                                    Toast.LENGTH_SHORT).show();
                        }

                });
    }

    private void googleSignIn()
    {
        Intent signInIntent = mGoogleSignInClient.getSignInIntent();
        startActivityForResult(signInIntent, RC_SIGN_IN);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        // Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
        if (requestCode == RC_SIGN_IN) {
            Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
            try {
                // Google Sign In was successful, authenticate with Firebase
                GoogleSignInAccount account = task.getResult(ApiException.class);
                //Log.d(TAG, "firebaseAuthWithGoogle:" + account.getId());
                firebaseAuthWithGoogle(account.getIdToken());

            } catch (ApiException e) {

                Toast.makeText(LoginActivity.this,e.getMessage(),Toast.LENGTH_SHORT).show();

            }
        }
    }

    private void firebaseAuthWithGoogle(String idToken) {

        progressDialog.show();

        AuthCredential credential = GoogleAuthProvider.getCredential(idToken,null);
        mAuth.signInWithCredential(credential)
                .addOnCompleteListener(this, task -> {
                    if (task.isSuccessful()) {

                        Toast.makeText(LoginActivity.this,"Google Sign In Successful",Toast.LENGTH_SHORT).show();

                        FirebaseUser user = mAuth.getCurrentUser();

                        if(task.getResult().getAdditionalUserInfo().isNewUser())
                        {
                            DbQuery.createUserData(user.getEmail(), user.getDisplayName(), new MyCompleteListener() {
                                @Override
                                public void onSuccess() {

                                    DbQuery.loadData(new MyCompleteListener() {
                                        @Override
                                        public void onSuccess() {
                                            progressDialog.dismiss();

                                            Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                                            startActivity(intent);
                                            LoginActivity.this.finish();
                                        }

                                        @Override
                                        public void onFailure() {

                                            progressDialog.dismiss();
                                            Toast.makeText(LoginActivity.this,"Something went Wrong! Try again later",
                                                    Toast.LENGTH_SHORT).show();
                                        }
                                    });

                                }

                                @Override
                                public void onFailure() {

                                    progressDialog.dismiss();
                                    Toast.makeText(LoginActivity.this,"Something went Wrong! Try again later",
                                            Toast.LENGTH_SHORT).show();

                                }
                            });
                        }
                        else
                        {
                            DbQuery.loadData(new MyCompleteListener() {
                                @Override
                                public void onSuccess() {
                                    progressDialog.dismiss();

                                    Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                                    startActivity(intent);
                                    LoginActivity.this.finish();
                                }

                                @Override
                                public void onFailure() {
                                    progressDialog.dismiss();
                                    Toast.makeText(LoginActivity.this,"Something went Wrong! Try again later",
                                            Toast.LENGTH_SHORT).show();
                                }
                            });
                        }

                    } else {

                        progressDialog.dismiss();
                        Toast.makeText(LoginActivity.this, task.getException().getMessage(),
                                Toast.LENGTH_SHORT).show();

                    }

                });

    }

}

XML文件

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:theme="@style/LoginTheme"
        tools:context=".LoginActivity"
        android:background="#FFFFFF"

>

    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:background="@color/Primary"
            android:gravity="center"
            android:padding="8dp"
            app:layout_constraintTop_toTopOf="parent" app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent" android:id="@+id/linearLayout">

        <ImageView
                android:layout_width="131dp"
                android:layout_height="150dp"
                android:src="@drawable/ic_images4"
        />

        <!--<TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/app_name"
                android:layout_marginTop="8dp"
                android:textColor="@color/white"
                android:textSize="32sp"/>-->

    </LinearLayout>

    <EditText
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:inputType="textEmailAddress"
            android:ems="10"
            android:id="@+id/email"
            android:drawableLeft="@drawable/ic_account"
            android:drawableTint="@color/Primary"
            android:drawablePadding="16dp"
            android:backgroundTint="@color/Primary"
            android:textColor="@color/black"
            android:hint="E-Mail ID"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/linearLayout"
            android:layout_marginTop="64dp"
            android:layout_marginStart="32dp"
            android:layout_marginEnd="32dp"/>
    <EditText
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:inputType="textPassword"
            android:ems="10"
            android:drawableLeft="@drawable/ic_lock"
            android:drawableTint="@color/Primary"
            android:drawablePadding="16dp"
            android:backgroundTint="@color/Primary"
            android:textColor="@color/black"
            android:hint="Password"
            android:id="@+id/password"
            app:layout_constraintTop_toBottomOf="@+id/email"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            android:layout_marginTop="16dp"
            android:layout_marginStart="32dp"
            android:layout_marginEnd="32dp"/>
    <TextView
            android:text="Forgot password?"
            android:textColor="@color/Primary"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/forgot_pass"
            app:layout_constraintTop_toBottomOf="@+id/password"
            app:layout_constraintStart_toStartOf="parent"
            android:layout_marginStart="32dp"
            android:layout_marginTop="12dp"/>

    <Button
            android:text="Sign In"
            android:textColor="@color/white"
            android:textSize="18sp"
            android:elevation="5dp"
            android:translationZ="3dp"
            android:stateListAnimator="@null"
            android:layout_width="338dp"
            android:layout_height="51dp"
            android:id="@+id/LoginB"
            app:layout_constraintTop_toBottomOf="@+id/forgot_pass"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            android:background="@drawable/round_corner"
            android:layout_marginTop="52dp"
            android:layout_marginStart="32dp"
            android:layout_marginEnd="32dp" app:layout_constraintHorizontal_bias="0.0"/>
    <TextView
            android:text="Don't have an account?"
            android:textColor="@color/black"
            android:textSize="16sp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/textView4"
            app:layout_constraintStart_toStartOf="parent"
            android:layout_marginStart="80dp"
            app:layout_constraintBottom_toBottomOf="parent"
            android:layout_marginBottom="224dp" app:layout_constraintEnd_toStartOf="@+id/signupB"
            android:layout_marginEnd="16dp"/>
    <TextView
            android:text="SIGN UP"
            android:textColor="@color/Primary"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/signupB"
            android:textSize="16sp"
            app:layout_constraintStart_toEndOf="@+id/textView4"
            android:layout_marginStart="16dp"
            app:layout_constraintBottom_toBottomOf="parent"
            android:layout_marginBottom="224dp" android:layout_marginEnd="88dp"
            app:layout_constraintEnd_toEndOf="parent"/>
    <RelativeLayout
            android:layout_width="0dp"
            android:layout_marginStart="64dp"
            android:layout_marginEnd="64dp"
            android:id="@+id/g_signB"
            android:layout_marginBottom="136dp"
            android:layout_height="40dp"
            android:gravity="center_vertical"
            android:background="@color/Primary"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"

    >

   <ImageView
           android:layout_margin="4dp"
           android:id="@+id/g_img"
           android:layout_width="wrap_content"
           android:layout_height="match_parent"
           android:src="@drawable/ic_google_logo"
           android:background="@color/white"
           android:padding="4dp"
   />

        <TextView
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:text="Sign In with Google"
                android:textColor="@color/white"
                android:textSize="20sp"
                android:layout_marginLeft="24dp"
                android:gravity="center_vertical"
                android:layout_toEndOf="@+id/g_img"

        />
        <Button
                android:layout_width="341dp"
                android:layout_height="126dp"
                android:id="@+id/button"
                android:background="@color/white"
        />

    </RelativeLayout>
    <TextView
            android:text="Sign-In"
            android:textStyle="bold"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="18sp"
            android:textColor="@color/black"
            android:layout_marginTop="13dp"
            app:layout_constraintTop_toBottomOf="@+id/linearLayout" app:layout_constraintStart_toStartOf="parent"
            android:layout_marginStart="8dp" android:layout_marginBottom="32dp"
            app:layout_constraintBottom_toTopOf="@+id/email" android:layout_marginEnd="360dp"
            app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintVertical_bias="0.0"/>

    <!--<RelativeLayout
            android:layout_width="0dp"
            android:layout_marginStart="64dp"
            android:layout_marginEnd="64dp"
            android:id="@+id/g_faceB"
            android:layout_marginBottom="64dp"
            android:layout_height="40dp"
            android:gravity="center_vertical"
            android:background="@color/Primary"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"

    >

        <ImageView
                android:layout_margin="4dp"
                android:id="@+id/facebook_img"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:src="@drawable/ic_facebook"
                android:background="@color/white"
                android:padding="4dp"
        />

        <TextView
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:text="Continue with Facebook"
                android:textColor="@color/white"
                android:textSize="20sp"
                android:layout_marginLeft="13dp"
                android:gravity="center_vertical"
                android:layout_toEndOf="@+id/facebook_img"

        />

    </RelativeLayout>-->

</androidx.constraintlayout.widget.ConstraintLayout>
1tuwyuhd

1tuwyuhd1#

要解决这个问题,我所需要做的就是将Sign Up按钮向下拖动一点。

相关问题