gpt4 book ai didi

java - 导致主要 Activity 的所有 Intent ?

转载 作者:行者123 更新时间:2023-12-01 21:16:23 25 4
gpt4 key购买 nike

这是我的 Android studio java 类之一的代码:

 Button button1; //log in
Button button2; //sign up

public View.OnClickListener button1Listener = new View.OnClickListener() {
public void onClick (View view){

Intent loginActivity = new Intent(getApplicationContext(), LoginActivity.class);
startActivity(loginActivity);


}};

public View.OnClickListener button2Listener = new View.OnClickListener() {
public void onClick (View view){
Intent signupActivity = new Intent(getApplicationContext(), MainActivity.class);
startActivity(signupActivity);


}};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_front_page);
button1 = (Button) findViewById(R.id.button3);
button2 = (Button) findViewById(R.id.button4);
button1.setOnClickListener(button1Listener);
button2.setOnClickListener(button2Listener);
}

主要 Activity :

public class MainActivity extends AppCompatActivity {

EditText editText;
EditText editText2;
Button button, button2;
public FirebaseAuth auth;
public FirebaseAuth.AuthStateListener authlistener;
String email, pass;
ProgressBar bar;

public View.OnClickListener buttonListener = new View.OnClickListener() {
public void onClick (View view){
email = editText.getText().toString().trim();
pass = editText2.getText().toString().trim();
createUser();

}};

public View.OnClickListener backButtonListener = new View.OnClickListener() {
public void onClick (View view){
Intent backtoMain = new Intent(getApplicationContext(), FrontPageActivity.class);
startActivity(backtoMain);

}};


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText)findViewById(R.id.editText);
editText2 = (EditText)findViewById(R.id.editText2);
button = (Button)findViewById(R.id.button2);
auth = FirebaseAuth.getInstance();
button.setOnClickListener(buttonListener);
bar = (ProgressBar)findViewById(R.id.progressBar2);
bar.setVisibility(View.INVISIBLE);
button2 = (Button)findViewById(R.id.button5);
button2.setOnClickListener(backButtonListener);
}

public void createUser(){
bar.setVisibility(View.VISIBLE);

auth.createUserWithEmailAndPassword(email, pass)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {

if(task.isSuccessful()){
Toast.makeText(getApplicationContext(), "Success", Toast.LENGTH_LONG).show();
bar.setVisibility(View.INVISIBLE);
} else{
Toast.makeText(getApplicationContext(), "Failed, Password must be >=6 characters", Toast.LENGTH_LONG).show();
bar.setVisibility(View.INVISIBLE);
}

}
});


}
}

登录 Activity :

public class LoginActivity extends AppCompatActivity {

EditText editText;
EditText editText2;
Button button;
public FirebaseAuth Auth = FirebaseAuth.getInstance();
public FirebaseAuth.AuthStateListener authlistener;
String email, pass;
ProgressBar bar;

public View.OnClickListener buttonListener = new View.OnClickListener() {
public void onClick (View view){
email = editText.getText().toString().trim();
pass = editText2.getText().toString().trim();
logInUser();

}};


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText)findViewById(R.id.editText);
editText2 = (EditText)findViewById(R.id.editText2);
button = (Button)findViewById(R.id.button2);
Auth = FirebaseAuth.getInstance();
button.setOnClickListener(buttonListener);
bar = (ProgressBar)findViewById(R.id.progressBar2);
bar.setVisibility(View.INVISIBLE);

authlistener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
FirebaseUser user = firebaseAuth.getCurrentUser();
if (user != null) {
// User is signed in

} else {
// User is signed out

}
// ...
}
};
// ...
}

@Override
public void onStart() {
super.onStart();
Auth.addAuthStateListener(authlistener);
}

@Override
public void onStop() {
super.onStop();
if (authlistener != null) {
Auth.removeAuthStateListener(authlistener);
}
}

public void logInUser(){
bar.setVisibility(View.VISIBLE);

Auth.signInWithEmailAndPassword(email, pass)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {

if(task.isSuccessful()) {

Intent loggedInActivity = new Intent(getApplicationContext(), Loggedin.class);
startActivity(loggedInActivity);

}
else{

Toast.makeText(getApplicationContext(), "There was an error, try again", Toast.LENGTH_SHORT).show();
}


}
});


}
}

所以,我有两个按钮,“登录”和“注册”,这两个按钮都应该导致不同的 Intent 。 sign-up 通向 mainactivity.class,它基本上是注册页面。

但是,即使我已将 button1 映射到 loginAcitivty 类,它仍然会转到 MainActivity.class。为什么会发生这种情况?

最佳答案

检查您的LoginActivity,您已设置setContentView(R.layout.activity_main);,它正在夸大activity_main的内容布局

The layout you are inflating in both the activities are same..

关于java - 导致主要 Activity 的所有 Intent ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39993596/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com