我正在接受用户的3个联系人,然后作为注册过程的一部分移至主屏幕。但是,当某些细节未填写时,我需要对其进行检查并举杯或提示您填充所有细节。然后,用户可以返回以填充其余的详细信息-空白。填写完详细信息后,用户单击注册按钮,应用程序将切换到主屏幕。
请帮助查找当前代码的问题。我是一个完整的初学者。任何帮助表示赞赏。
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));
}
}});
}
}
答案 0 :(得分:0)
显示onClick(view);
后删除Toast
onClick(view);
仅在用户单击按钮并
用try / catch块包围所有解析器:
try {
emnum1 = Integer.parseInt(num1.getText().toString());
} catch (NumberFormatException e) {
e.printStackTrace();
enum1 = -1
}
如果TextView不包含有效数字,您可以将-1更改为所需的值