2017-10-11 58 views
0

如果在我的onStart()上點擊一個按鈕以檢查點擊的按鈕是註冊還是登錄,我該如何放置if else語句?Android onStart如果點擊按鈕

這裏是我的代碼

public void onClick(View v) { 
     switch (v.getId()) { 
      case R.id.register: 
       i = new Intent(this, RegisterCustomer.class); 
       startActivity(i); 
       finish(); 
       break; 
      case R.id.login: 
       final String email = memail.getText().toString(); 
       final String password = mpassword.getText().toString(); 
       mAuth.signInWithEmailAndPassword(email, password).addOnCompleteListener(CustomerLoginActivty.this, new OnCompleteListener<AuthResult>() { 
        @Override 
        public void onComplete(@NonNull Task<AuthResult> task) { 
         if (!task.isSuccessful()) { 
          Toast.makeText(CustomerLoginActivty.this, "sign in error", Toast.LENGTH_SHORT).show(); 
         } 
        } 
       }); 
       break; 
     } 
    } 

    @Override 
    protected void onStart() { 
     super.onStart(); 
     mAuth.addAuthStateListener(firebaseAuthListener); 
    } 
+0

你想達到什麼樣的? –

+0

嘗試在我的onStart()上放一個if if來檢查正在單擊的按鈕。可能嗎? – osoda

+1

'onStart()'已經被調用。你能不能在你的'onClick()'中放置'mAuth.addAuthStateListener(firebaseAuthListener);'? – codeMagic

回答

0
  1. 類創建一個域。例如int clickedButton;
  2. 在onStart()檢查使用該字段的ID。

    int clickedButton; 
    
    public void onClick(View v) { 
        clickedButton = v.getId(); 
        switch (v.getId()) { 
        case R.id.register: 
         i = new Intent(this, RegisterCustomer.class); 
         startActivity(i); 
         finish(); 
         break; 
        case R.id.login: 
         final String email = memail.getText().toString(); 
         final String password = mpassword.getText().toString(); 
         mAuth.signInWithEmailAndPassword(email, password).addOnCompleteListener(CustomerLoginActivty.this, new OnCompleteListener <AuthResult>() {@Override 
          public void onComplete(@NonNull Task <AuthResult> task) { 
           if (!task.isSuccessful()) { 
            Toast.makeText(CustomerLoginActivty.this, "sign in error", Toast.LENGTH_SHORT).show(); 
           } 
          } 
         }); 
         break; 
        } 
    } 
    
    @Override 
    protected void onStart() { 
        super.onStart(); 
        if(clickedButton != null){ 
         switch (clickedButton) { 
         case R.id.register: 
         //do your thing 
         case R.id.login: 
         //do your thing 
         } 
        } 
    
        mAuth.addAuthStateListener(firebaseAuthListener); 
    
    }