如何使用Android在没有toast(Alter对话框)的情况下使用set错误消息设置编辑文本框?

时间:2015-12-05 08:39:57

标签: android android-alertdialog

嘿,我每个人都在android中创建重命名应用程序,我将在设置错误消息中设置一个编辑文本框,使用不带吐司的警告对话框

示例代码:

        AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
        alert.setTitle(R.string.rename_title);

        folderManager = new FolderManager(getActivity());
        folderManager.open();

        Cursor c = folderManager.queryAll(itemPos);
        if (c.moveToFirst()) {
            do {
                Newnamefolder = c.getString(1);
            } while (c.moveToNext());
        }

        // Set an EditText view to get user input
        final EditText input = new EditText(getActivity());
        input.setText(Newnamefolder);
        alert.setView(input);
        alert.setPositiveButton(R.string.rename_position_button, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton)
            {

                Newnamefolder = input.getText().toString();
                String Mesage_one = getResources().getString(R.string.folder_already_exit);
                String Mesage_two = getResources().getString(R.string.types_minimum_eight_charcter);
                String Mesage_three = getResources().getString(R.string.folder_empty);
                String Matchnamerename = folderManager.getmatchfoldername(Newnamefolder);

                if(Newnamefolder.equals(Matchnamerename))

                {

                    input.setError(Mesage_one);


                }
                else if(Newnamefolder.length()>12)
                {

                    input.setError(Mesage_two);

                }

                else if(Newnamefolder.equals(""))
                {

                    input.setError(Mesage_three);

                }

                else
                {
                    int newfolder =  folderManager.update(itemPos,Newnamefolder);
                    reload();
                }

            }
        });


        alert.show();

但问题一次点击“确定”按钮不显示错误消息以退出“警报”对话框...

给我任何解决方案......朋友?

1 个答案:

答案 0 :(得分:1)

您可以扩展Dialog并创建自己的对话框

public class CustomDialog extends Dialog implements View.OnClickListener {
    private boolean success = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.custom_dialog);
        Button positive = (Button) findViewById(R.id.button_positive);
        Button negative = (Button) findViewById(R.id.button_negative);
        EditText field = (EditText) findViewById(R.id.field);

        positive.setOnClickListener(this);
        negative.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch(v.getId()) {
            case R.id.button_positive:
                 onPositiveButtonClicked();
            break;
            case R.id.button_negative:
                 //onNegativeButtonClicked();
            break;
        }
    }

    private void onPositiveButtonClicked() {
        if(verifyForm()) {
             success = true;
             dismiss();
        }
    }

    public boolean isSuccess() {
         return success;
    }

    private boolean verifyForm() {
         boolean valid = true;
         /* verify each field and setError() if not valid */
         if(!TextUtils.isEmpty(field.getText())) { //or any other condition
             valid = false;
             field.setError("error message");
         }
         return valid;
    }
}

您可以显示CustomDialog这样的

final CustomDialog customDialog = new CustomDialog();
dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
    @Override
    public void onDismiss(DialogInterface dialog) {
        if(customDialog.isSuccess()) {
            //update your folder manager
        }
    }
}
customDialog.show();