我的代碼的目標是:當我輸入一個祕密的用戶名和密碼,然後按回車鍵,打開一個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按鈕,下一個活動就不是我想要的。
https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java – njzk2