因此,我正在为我的编程课开发一个项目,该项目要求我们创建一个登录屏幕,一个用户输入生日和其他信息的屏幕以及一个空白屏幕(稍后将做更多,不过此刻这只是一个空白屏幕)。当我启动我的应用并正确输入登录信息时,它会跳过第二个屏幕,直接进入空白屏幕,我不确定为什么会这样做。这是登录屏幕MainActivity的代码:
public enum LoginSuccess {
login(1),
password(2),
success(0);
private int intValue;
LoginSuccess(int value){
intValue = value;
}
public int getIntValue(){
return intValue;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView txtLogin = (TextView) findViewById(R.id.idLoginText);
final TextView txtPassword = (TextView) findViewById(R.id.idPassword);
Button btnLogin = (Button) findViewById(R.id.btnLogin);
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast toastError = null;
switch(CheckLogin(txtLogin.getText().toString(), txtPassword.getText().toString())){
case login:
toastError.makeText(getApplicationContext(), getString(R.string.errMessageLogin), toastError.LENGTH_LONG).show();
txtLogin.requestFocus();
break;
case password:
toastError.makeText(getApplicationContext(), getString(R.string.errMessagePassword), toastError.LENGTH_LONG).show();
txtPassword.requestFocus();
break;
default:
startActivity(new Intent(MainActivity.this, MainClassList.class));
}
}
});
}
LoginSuccess CheckLogin(String txtLogin, String txtPassword){
String holdLogin = "Codie";
String holdPassword = "password";
if (!holdLogin.equals(txtLogin)){
return LoginSuccess.login;
}
if (!holdPassword.equals(txtPassword)){
return LoginSuccess.password;
}
return LoginSuccess.success;
}
}
这是第二个屏幕MainClassList的代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_class_list);
final Switch swDegreeCert = (Switch) findViewById(R.id.swDegreeCert);
final Spinner spnDegree = (Spinner) findViewById(R.id.spnDegree);
final Spinner spnCertificate = (Spinner) findViewById(R.id.spnCertificate);
final TextView txtCertificate = (TextView) findViewById(R.id.lblCertificate);
final TextView txtDegree = (TextView) findViewById(R.id.lblDegree);
final Button btnNext = (Button) findViewById(R.id.btnNext);
final EditText firstName = (EditText) findViewById(R.id.txtFirstName);
final EditText lastName = (EditText) findViewById(R.id.txtLastName);
final EditText phone = (EditText) findViewById(R.id.txtPhone);
final Spinner spMonth = (Spinner) findViewById(R.id.spnMonth);
final EditText txtDay = (EditText) findViewById(R.id.txtDay);
final EditText txtYear = (EditText) findViewById(R.id.txtYear);
firstName.requestFocus();
swDegreeCert.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked){
spnDegree.setVisibility(View.VISIBLE);
txtDegree.setVisibility(View.VISIBLE);
spnCertificate.setVisibility(View.GONE);
txtCertificate.setVisibility(View.GONE);
} else {
spnDegree.setVisibility(View.GONE);
txtDegree.setVisibility(View.GONE);
spnCertificate.setVisibility(View.VISIBLE);
txtCertificate.setVisibility(View.VISIBLE);
}
}
});
btnNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (checkData()){
String doBirth = "";
doBirth = spMonth.getSelectedItem().toString() + "/" + txtDay.getText().toString() + "/" + txtYear.getText().toString();
// old way
// startActivity(new Intent(MainClassList.this, ChooseClass.class);
//new way
Intent nextScreen = new Intent(MainClassList.this, ChooseClass.class);
nextScreen.putExtra("FirstName", firstName.getText().toString());
nextScreen.putExtra("LastName", lastName.getText().toString());
nextScreen.putExtra("Phone", phone.getText().toString());
nextScreen.putExtra("BirthDate", doBirth);
if (spnDegree.getVisibility() == View.VISIBLE){
nextScreen.putExtra("isDegreeCert", "Degree");
nextScreen.putExtra("degreeCert", spnDegree.getSelectedItem().toString());
} else {
nextScreen.putExtra("isDegreeCert", "Certificate");
nextScreen.putExtra("degreeCert", spnCertificate.getSelectedItem().toString());
}
//Start Activity
startActivity(nextScreen);
}
}
});
}
private boolean checkData(){
final EditText firstName = (EditText) findViewById(R.id.txtFirstName);
final EditText lastName = (EditText) findViewById(R.id.txtLastName);
final EditText phone = (EditText) findViewById(R.id.txtPhone);
final EditText txtDay = (EditText) findViewById(R.id.txtDay);
final EditText txtYear = (EditText) findViewById(R.id.txtYear);
if(firstName.getText().toString().isEmpty()){
//error
firstName.setError("Invalid First Name");
firstName.requestFocus();
return false;
}
if(lastName.getText().toString().isEmpty()){
lastName.setError("Invalid Last Name");
lastName.requestFocus();
return false;
}
if(phone.getText().toString().isEmpty()) {
phone.setError("Invalid Phone Number");
phone.requestFocus();
return false;
}
if(txtDay.getText().toString().isEmpty()) {
txtDay.setError("Invalid Date Selection");
txtDay.requestFocus();
return false;
}
if(txtYear.getText().toString().isEmpty()) {
txtYear.setError("Invalid Date Selection");
txtYear.requestFocus();
return false;
}
return true;
}
}
第三类ChooseClass没有任何有效的代码。重申一下,该应用程序从MainActivity直接转到ChooseClass,我不知道为什么。任何帮助都将不胜感激。
这是我的清单:
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainClassList" />
<activity android:name=".ChooseClass"></activity>
</application>
</manifest>