我有一个应用程序让我在启动时显示输入用户名和密码用于登录的对话框。如果您的用户名和密码是正确的我的程序更改活动,如果不正确它应该重复我再次输入用户名和密码的对话框。我的问题是如何在输入不正确时重复对话
这里是代码:
public Dialog onCreateDialog(Bundle savedInstanceState) {
inflater = getActivity().getLayoutInflater();
v = inflater.inflate(R.layout.my_dialog_layout,null);
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setView(v).setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
username = (EditText) v.findViewById(R.id.username);
password = (EditText) v.findViewById(R.id.password);
String user = "";
String pswd = "";
try {
File file=new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/Download/"+"user.txt");
InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(file));
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
StringBuilder stringBuilder = new StringBuilder();
user = bufferedReader.readLine().toString();
pswd = bufferedReader.readLine();
}catch (FileNotFoundException e) {
Log.e("login activity", "File not found: " + e.toString());
}catch (IOException e) {
Log.e("login activity", "Can not read file: " + e.toString());
}
if (username.getText().toString().equals(user) && password.getText().toString().equals(pswd)) {
Intent i = new Intent("user_activity");
startActivity(i);
Toast.makeText(getActivity(), "Welcome: " + username.getText().toString(), Toast.LENGTH_LONG).show();
} else {
//here it should me repeat this dialog
Toast.makeText(getActivity(), "Username invalid", Toast.LENGTH_LONG).show();
}
}
}).setNegativeButton("EXIT", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
((MainActivity)getActivity()).finish();
}
});
return builder.create();
}
谢谢大家
答案 0 :(得分:1)
尝试这种简单的方法:保持对话框显示并清除字段用户名和密码:
if (username.getText().toString().equals(user) && password.getText().toString().equals(pswd)) {
Intent i = new Intent("user_activity");
startActivity(i);
Toast.makeText(getActivity(), "Welcome: " + username.getText().toString(), Toast.LENGTH_LONG).show();
dialog.dismiss();
} else {
//here it should me repeat this dialog
username.setText("", TextView.BufferType.EDITABLE);;
password.setText("", TextView.BufferType.EDITABLE);
Toast.makeText(getActivity(), "Username invalid", Toast.LENGTH_LONG).show();
}