我目前正在为一个Android项目的登录界面工作。它有一个用于登录的按钮和一个用作按钮的文本视图,如果用户没有帐户的话。登录屏幕工作得很漂亮但是一旦我在文本视图中添加了一个onClick监听器,它们都没有响应。我对出现问题感到茫然,因为如果我将textview评论出来就可以了。
LoginActivity:
public class LoginActivity extends AppCompatActivity {
//Declare Global Variables
private EditText emailText;
private EditText passwordText;
private TextView signUpText;
private Button loginButton;
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListen;
private static final String TAG = "LoginActivity";
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_login);
initVar();
initFireBase();
initOnClick();
}
private void initVar() {
emailText = (EditText) findViewById(R.id.emailText);
passwordText = (EditText) findViewById(R.id.passwordText);
loginButton = (Button) findViewById(R.id.loginButton);
signUpText = (TextView) findViewById(R.id.signUpText);
Log.d(TAG, "Setting Variables to Relative ID's");
}
private void initFireBase() {
mAuth = FirebaseAuth.getInstance();
mAuthListen = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
if (firebaseAuth.getCurrentUser() != null) {
startActivity(new Intent(LoginActivity.this, HomeScreen.class));
Log.d(TAG, "Loading Home Screen");
finish();
}
}
};
}
private void initOnClick() {
loginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
signIn();
Log.d(TAG, "Executing Sign On");
}
});
signUpText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(LoginActivity.this, SignUpActivity.class);
startActivity(intent);
Log.d(TAG, "Loading SignUp Screen");
}
});
}
private void signIn() {
String email = emailText.getText().toString();
String password = passwordText.getText().toString();
if (TextUtils.isEmpty(email) || TextUtils.isEmpty(password)) {
Toast.makeText(LoginActivity.this, "Put In Password or Email", Toast.LENGTH_LONG).show();
} else {
mAuth.signInWithEmailAndPassword(email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (!task.isSuccessful()) {
Toast.makeText(LoginActivity.this, "Unable to Sign In", Toast.LENGTH_LONG).show();
}
}
});
}
}
@Override
protected void onStart() {
super.onStart();
mAuth.addAuthStateListener(mAuthListen);
}
public void onStop() {
super.onStop();
if (mAuthListen != null) {
mAuth.removeAuthStateListener(mAuthListen);
}
}
}
登录布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:background="@android:color/white">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingTop="55dp"
android:paddingLeft="25dp"
android:paddingRight="25dp"
android:gravity="center"
>
<ImageView
android:id="@+id/imageView"
android:layout_width="197dp"
android:layout_height="223dp"
app:srcCompat="@drawable/icon_final" />
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:hint="Email"
android:id="@+id/emailText"/>
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:hint="passwordText"
android:id="@+id/password"/>
</android.support.design.widget.TextInputLayout>
<android.support.v7.widget.AppCompatButton
android:id="@+id/loginButton"
android:layout_width="213dp"
android:layout_height="58dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="Login"
tools:textColor="#ffff"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/passwordText"
app:layout_constraintVertical_bias="0.044"
tools:background="@color/colorAccent"
tools:ignore="OnClick" />
<TextView
android:id="@+id/signUpText"
android:layout_width="wrap_content"
android:layout_height="17dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:clickable="true"
android:gravity="center"
android:text="Don't have an account?" />
</LinearLayout>
</RelativeLayout>
编辑:我想通了,我正在为第二个看起来相同的活动调用一个反应迟钝的布局
答案 0 :(得分:0)
代码没有问题,我调用了一个与登录屏幕相同的非活动布局... doh