2016-11-09 261 views
-1

我的代碼的目標是:當我輸入一個祕密的用戶名和密碼,然後按回車鍵,打開一個Activity3。否則,打開Activity2。這是我的代碼:
package edcomp.terraraimoveis;祕密登錄密碼 - Android

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.support.design.widget.TextInputLayout; 
import android.view.KeyEvent; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.inputmethod.EditorInfo; 
import android.widget.AutoCompleteTextView; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 

public class Principal extends Activity implements OnClickListener { 

    // UI references. 
    private AutoCompleteTextView userName; 
    private EditText mPasswordView; 
    private TextInputLayout nameLayout; 
    private TextInputLayout passLayout; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_principal); 

     userName = (AutoCompleteTextView) findViewById(R.id.username); 
     //populateAutoComplete(); 
     mPasswordView = (EditText) findViewById(R.id.password); 
     nameLayout = (TextInputLayout) findViewById(R.id.namelayout); 
     passLayout = (TextInputLayout) findViewById(R.id.passlayout); 
     Button entrarBtn = (Button) findViewById(R.id.entrarbtn); 
     entrarBtn.setOnClickListener(this); 

    } 

    @Override 
    public void onClick(View view) { 

     if(validateData() == true){ 
      if(vipPass() == true){ 
       Intent myintent = new Intent(Principal.this, AdicionarCredenciais.class); 
       Principal.this.startActivity(myintent); 
      }else { 
       Intent myintent = new Intent(Principal.this, DoisAtividade.class); 
       Principal.this.startActivity(myintent); 
      } 
     } 
    } 

    public boolean validateData(){ 
     String nome = userName.getText().toString(); 
     String pass = mPasswordView.getText().toString(); 

     boolean result = true; 

     if(nome == null){ 
      nameLayout.setError(getString(R.string.error_invalid_name)); 
      result = false; 
     }else{ 
      nameLayout.setErrorEnabled(false); 
     } 

     if(pass == null || pass.length() < 8){ 
      passLayout.setError((getString(R.string.error_invalid_password))); 
     result = false; 
     } 
     return result; 
    } 

    public boolean vipPass() { 
     String nome = userName.getText().toString(); 
     String pass = mPasswordView.getText().toString(); 

     boolean vip_mode = false; 
     if (nome == "VIPUSER" && pass == "987654321") { 
      vip_mode = true; 
     } 
     return vip_mode; 
    } 
} 

我添加(活動機器人:名字= 「AdicionarCredenciais 」)和:在manifest.xml文件(活動的android名=「。DoisAtividade」)。但是,當我輸入祕密用戶名和密碼,然後按下Enter按鈕,下一個活動就不是我想要的。

+0

https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java – njzk2

回答

0

問題稍有是你的if語句中,試試這個:

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.support.design.widget.TextInputLayout; 
import android.view.KeyEvent; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.inputmethod.EditorInfo; 
import android.widget.AutoCompleteTextView; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 

public class Principal extends Activity implements OnClickListener { 

    // UI references. 
    private AutoCompleteTextView userName; 
    private EditText mPasswordView; 
    private TextInputLayout nameLayout; 
    private TextInputLayout passLayout; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_principal); 

     userName = (AutoCompleteTextView) findViewById(R.id.username); 
     //populateAutoComplete(); 
     mPasswordView = (EditText) findViewById(R.id.password); 
     nameLayout = (TextInputLayout) findViewById(R.id.namelayout); 
     passLayout = (TextInputLayout) findViewById(R.id.passlayout); 
     Button entrarBtn = (Button) findViewById(R.id.entrarbtn); 
     entrarBtn.setOnClickListener(this); 

    } 

    @Override 
    public void onClick(View view) { 

     if(validateData() == true){ 
          Intent myintent = new Intent(Principal.this, AdicionarCredenciais.class); 
       Principal.this.startActivity(myintent); 
      }else 
if(vipPass() == true){ 
       Intent myintent = new Intent(Principal.this, DoisAtividade.class); 
       Principal.this.startActivity(myintent); 
      } 
     } 
    } 

    public boolean validateData(){ 
     String nome = userName.getText().toString(); 
     String pass = mPasswordView.getText().toString(); 

     boolean result = true; 

     if(nome == null){ 
      nameLayout.setError(getString(R.string.error_invalid_name)); 
      result = false; 
     }else{ 
      nameLayout.setErrorEnabled(false); 
     } 

     if(pass == null || pass.length() < 8){ 
      passLayout.setError((getString(R.string.error_invalid_password))); 
     result = false; 
     } 
     return result; 
    } 

    public boolean vipPass() { 
     String nome = userName.getText().toString(); 
     String pass = mPasswordView.getText().toString(); 

     boolean vip_mode = false; 
     if (nome == "VIPUSER" && pass == "987654321") { 
      vip_mode = true; 
     } 
     return vip_mode; 
    } 
} 

方法2:

轉到這兩個活動,並檢查你是否設置的內容以正確的佈局,你可能對兩個活動都使用了相同的佈局。

+0

我認爲 「諾姆」 和 「通行證」如果是vipPass()true,那麼它也是一個validateData()。因此,「VIPUSER」和「987654321」也滿足validateData()條件。 –

+0

有道理,但試試看看我的編輯。 –

+0

只需在if語句中將vippass()第一個validatedata()放入。 –

0

嘗試使用此代碼

public boolean vipPass() { 
     String nome = userName.getText().toString(); 
     String pass = mPasswordView.getText().toString(); 

     if (nome.equals("VIPUSER") && pass.equals("987654321")) { 
      return true; 
     } 
     return false; 
    }