以下是我的一个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);
}
主要活动:
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);
}
}
});
}
}
登录活动:
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();
}
}
});
}
}
所以,我有2个按钮,&#34;登录,&#34;和&#34;注册&#34;两者都应该导致不同的意图。 sign-up
导致mainactivity.class
基本上是注册页面。
但是,即使我已将button1
映射到loginAcitivty类,它仍会转到MainActivity.class
。为什么会这样?
答案 0 :(得分:3)
检查LoginActivity
中的setContentView(R.layout.activity_main);
是否已设置activity_main
.factory('tableService', ['$http', function($http){
return {
getTable: function() {
return $http.get("data/timesheet.json");
}
}
}])
你在两个活动中膨胀的布局是相同的..
答案 1 :(得分:0)
像这样更改你的代码,
Button button1; //log in
Button button2; //sign up
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent loginActivity = new Intent(getApplicationContext(), LoginActivity.class);
startActivity(loginActivity);
}};
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent signupActivity = new Intent(getApplicationContext(), MainActivity.class);
startActivity(signupActivity);
}};