如何使对话框保持不变直到被明确驳回?

时间:2011-06-03 12:55:39

标签: android

我正在开发用户身份验证方案。我已经放下了以下代码的关键。

我正在调用函数

来自我的activity的onCreate()函数的

showDialog(DIALOG_LOGIN);。我的问题是,在任何按钮点击内执行代码后,对话框会自动解除。但是我希望对话框保持不变直到我提到如果认证失败,我希望对话框不被解雇。如何做到这一点?

        protected Dialog onCreateDialog(int id) {
                switch (id) {

                case DIALOG_LOGIN:
                    // Inflating the View from the xml
                    factory = LayoutInflater.from(this);
                    loginView = factory.inflate(R.layout.alert_dialog_text_entry, null);

                    return new AlertDialog.Builder(this)
                            .setTitle(R.string.alert_dialog_login)
                            .setView(loginView)
                            .setPositiveButton(R.string.dialog_ok,
                                    new DialogInterface.OnClickListener() {
                                        public void onClick(DialogInterface dialog,
                                                int whichButton) {
                                            Context context = getApplicationContext();
                                            SharedPreferences prefs = PreferenceManager
                                                    .getDefaultSharedPreferences(context);
                                            SharedPreferences.Editor editor = prefs
                                                    .edit();

                                            EditText username = (EditText) loginView
                                                    .findViewById(R.id.username_edit);
                                            EditText password = (EditText) loginView
                                                    .findViewById(R.id.password_edit);

                                            //Authenticating UserName and Password.
                                            String params [] ={username.getText().toString(),
                                                               password.getText().toString()};
                                            new AsyncAuthneticationTask().execute(params);


                                        }
                                    })

    .setNegativeButton(R.string.dialog_cancel,
                                new OnClickListener() {

                                    @Override
                                    public void onClick(DialogInterface dialog,
                                            int which) {
                                        // TODO Auto-generated method stub

                                    }
                                }).create();
}

        return null;
    }

异步任务的OnPostExecute功能:

@Override
    protected void onPostExecute(String response){


        Toast toast;
        if(response.contains("Invalid")){
            toast=Toast.makeText(Login.this,response, Toast.LENGTH_LONG);
            toast.show();

        }
        else{
            toast=Toast.makeText(Login.this, getString(R.string.authentication_success_msg), Toast.LENGTH_LONG);
            toast.show();
            dismissDialog(DIALOG_LOGIN);
        }


    }

5 个答案:

答案 0 :(得分:0)

避免.setPositiveButton并使用自定义界面instad,你可以随时解除对话

带有自定义控件的Samplecode

             LayoutInflater mInflater = LayoutInflater.from(tab3.this);
             AlertDialog.Builder builder = new AlertDialog.Builder(tab3.this);
             builder.setTitle(params[4]);
             View convertView = mInflater.inflate(R.xml.dialog_addr, null);

             final EditText et_city = (EditText) convertView.findViewById(R.id.dialog_et_1);
             final EditText et_street = (EditText) convertView.findViewById(R.id.dialog_et_2);
             final EditText et_number = (EditText) convertView.findViewById(R.id.dialog_et_3);
             ((TextView)convertView.findViewById(R.id.dialog_tv_1)).setText(R.string.dialog_city);
             ((TextView)convertView.findViewById(R.id.dialog_tv_2)).setText(R.string.dialog_street);
             ((TextView)convertView.findViewById(R.id.dialog_tv_3)).setText(R.string.dialog_number);


             builder.setView(convertView);
             final AlertDialog alert = builder.create();
             et_city.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                    @Override
                    public void onFocusChange(View v, boolean hasFocus) {
                        if (hasFocus) {
                            alert.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
                        }
                    }
                }); 
             alert.show();

答案 1 :(得分:0)

你有一个包含整个对话框的While循环:

boolean success = false;
...
case DIALOG_LOGIN:
    do
    {
    ...
        .setPositiveButton(R.string.dialog_ok,new DialogInterface.OnClickListener()
        {
            ...
            success=true;
        }
        ...
    }while (!success)

答案 2 :(得分:0)

嗯,这是Android的Dialog类的默认行为。如果你想明确地保持它,我认为你需要创建自己的对话窗口,你可以自己定义行为。例如,谷歌吧。

答案 3 :(得分:0)

我不明白你打电话的原因

dismissDialog(DIALOG_LOGIN);

的ShowDialog(DIALOG_LOGIN);

一个接一个?我的意思是你不需要这样做。当用户按下negetive按钮时,不要做任何事情。或者只是关闭对话框。

此外,身份验证似乎是在新的AsyncAuthneticationTask()中完成的.execute(params); 线。您是否错误地使用onpostexecute方法中的dismiss()调用关闭对话框?

答案 4 :(得分:0)

我想知道自定义对话框是否可行:

    // UPDATE BUTTON HANDLER
    final Button buttonUpdate= (Button)d.findViewById(R.id.ButtonPasswordUpdate);
    buttonUpdate.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            String entryFirst= editTextPasswordFirst.getText().toString();
            String entrySecond= editTextPasswordSecond.getText().toString();        
            if (entryFirst.equals(entrySecond)) { // do NOT use == with string values
                if (entryFirst.length() != lengthPassword) { // invalid key length
                    editTextPasswordFirst.setText("Invalid Key Length of "+
                            new Integer(entryFirst.length()).toString());                        
                    editTextPasswordSecond.setText("");
                    editTextPasswordFirst.selectAll();
                    editTextPasswordFirst.requestFocus();
                }
                else {
                    editTextPassword.setText(entryFirst);
                    // *** Clear Key Fields ***
                    editTextPasswordFirst.setText("");                   
                    editTextPasswordSecond.setText("");
                    d.dismiss();
                }
            }
            else { // entries do not match
                editTextPasswordFirst.setText("Entries did not match!");                   
                editTextPasswordSecond.setText("");
                editTextPasswordFirst.selectAll();
                editTextPasswordFirst.requestFocus();
            }
        }
    });

More code here.