android 点击后重复对话框

android repeat dialog after clicked on it

我有一个应用程序让我在开始时显示一个对话框,其中输入用户名和密码用于登录。如果您的用户名和密码正确,我的程序更改活动,如果不正确,它应该重复我的对话框以再次输入用户名和密码.我的问题是当输入不正确时如何重复对话

这里是代码:

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();
}

谢谢大家

试试这个简单的方法:保持对话框显示并清除字段用户名和密码:

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();
        }