Java 输入联系人并将其带到主屏幕
我接受用户的3个联系人,然后作为注册过程的一部分移动到主屏幕。但是,当某些细节部分没有填写时,我需要检查它,并敬酒或提示填写所有细节。然后用户可以返回来填充其余的细节——空白。填写详细信息后,用户单击注册按钮,应用程序切换到主屏幕 请帮助查找当前代码的错误。我是一个完全的初学者。感谢您的帮助Java 输入联系人并将其带到主屏幕,java,android,Java,Android,我接受用户的3个联系人,然后作为注册过程的一部分移动到主屏幕。但是,当某些细节部分没有填写时,我需要检查它,并敬酒或提示填写所有细节。然后用户可以返回来填充其余的细节——空白。填写详细信息后,用户单击注册按钮,应用程序切换到主屏幕 请帮助查找当前代码的错误。我是一个完全的初学者。感谢您的帮助 import android.content.Context; import android.content.Intent; import android.content.SharedPreferences
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class AddContacts extends AppCompatActivity {
SharedPreferences sharedPreferences;
SharedPreferences.Editor editor;
Button registerbtn;
EditText name1, num1, name2, num2, name3, num3;
String emname1,emname2,emname3;
int emnum1,emnum2,emnum3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_addcontacts);
sharedPreferences = this.getSharedPreferences("universe.sk.syndriveapp.addcontacts", Context.MODE_PRIVATE);
editor = sharedPreferences.edit();
registerbtn= findViewById(R.id.registerbtn);
name1 = findViewById(R.id.name1);
num1 = findViewById(R.id.num1);
name2 = findViewById(R.id.name2);
num2 = findViewById(R.id.num2);
name3 = findViewById(R.id.name3);
num3 = findViewById(R.id.num3);
registerbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
emname1 = name1.getText().toString();
emnum1 = Integer.parseInt(num1.getText().toString());
emname2 = name2.getText().toString();
emnum2 = Integer.parseInt(num2.getText().toString());
emname3 = name3.getText().toString();
emnum3 = Integer.parseInt(num3.getText().toString());
if (emname1.isEmpty() || emname2.isEmpty() || emname3.isEmpty()){
Toast.makeText(AddContacts.this,"Please fill all details!",Toast.LENGTH_SHORT).show();
}
else {
editor.putString("name1", emname1);
editor.putInt("num1", emnum1);
editor.putString("name2", emname2);
editor.putInt("num2", emnum2);
editor.putString("name3", emname3);
editor.putInt("num3", emnum3);
editor.apply();
Toast.makeText(AddContacts.this,"Registration Success",Toast.LENGTH_SHORT).show();
startActivity(new Intent(AddContacts.this,NavigationActivity.class));
}
}});
}
}
删除onClick(视图)代码>在您显示土司后
onClick(视图)仅当用户单击按钮并
时,才应触发代码>
用try/catch块包围所有解析器:
try {
emnum1 = Integer.parseInt(num1.getText().toString());
} catch (NumberFormatException e) {
e.printStackTrace();
enum1 = -1
}
如果TextView不包含有效的数字,您可以将-1更改为您想要的值。是的,但当用户仅在填写部分详细信息后单击“注册”,应用程序就会崩溃。