我的代码的目标是:当我输入密码用户名和密码时,按Enter按钮,打开一个Activity3。否则,打开Activity2。这是我的代码:
包edcomp.terraraimoveis;
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;
}
}
我在manifest.xml中添加了(活动android:name =&#34; .AdicionarCredenciais&#34;)和(activity android:name =&#34; .DoisAtividade&#34;)。但是,当我输入秘密用户名和密码,然后按Enter键,下一个活动就不是我想要的了。
答案 0 :(得分: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:
转到这两个活动并检查是否确实将内容视图设置为正确的布局,您可能已为这两个活动使用相同的布局。
答案 1 :(得分: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;
}