如何使用显示的编辑文本保留警报对话框

时间:2012-08-02 15:29:30

标签: android spinner android-alertdialog

我目前正在尝试保留一个警告对话框,其中包含屏幕上显示的编辑文字。像往常一样,有Okay和Cancel按钮,我想在流程中添加一些错误处理。如果用户没有在编辑文本框中输入任何文本并尝试按好,我想要弹出一个Toast消息,通知他们他们的错误。该对话框应该仍然显示,因为如果他们想要按下它们仍然需要输入内容。

这是我到目前为止所拥有的。顺便说一句,根据用户从下拉列表中选择的内容弹出对话框。任何有助于保持对话框保持运行的帮助都将非常感谢!!!

这是我的代码:

    regionSpinner.setOnItemSelectedListener(new OnItemSelectedListener()
    {
        @Override
        public void onItemSelected(AdapterView<?> parent, View v, int position, long rowId)
        {
            regionSelect = regionSpinner.getItemAtPosition(position).toString();
            regionInteger = (int) regionSpinner.getItemIdAtPosition(position);




            chosenRegion = regionSpinner.getSelectedItem().toString();

            if (chosenRegion.equals(ENTER_OPTION))

            {
                final AlertDialog.Builder alert = new AlertDialog.Builder(LoginActivity.this);

                alert.setTitle(TITLE_DIALOG);

                // Set an EditText view to get user input
                 final EditText input = new EditText(LoginActivity.this);
                alert.setView(input);

                alert.setPositiveButton("Ok", new DialogInterface.OnClickListener()
                {
                    public void onClick(DialogInterface dialog, int whichButton)
                    {
                        if(input.getText().toString().length() > 0 && input != null)
                        {
                        textValueForEnteredRegion = input.getText().toString();
                        Toast.makeText(getApplicationContext(), "Input Accepted", Toast.LENGTH_SHORT).show();


                        dialog.dismiss();
                        //added soft keyboard code to make keyboard go away.
                        getWindow().setSoftInputMode(
                                  WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);


                        }
                        else
                        {

                            Toast.makeText(getApplicationContext(), "You must enter a selection.", Toast.LENGTH_SHORT).show();

                        }
                    }


                });



                alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener()
                {
                    public void onClick(DialogInterface dialog, int whichButton)
                    {
                        // Canceled.
                        dialog.dismiss();

                        //added soft keyboard code to make keyboard go away.
                        getWindow().setSoftInputMode(
                                WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);


                    }
                });

                alert.show();



            }


        }



        @Override
        public void onNothingSelected(AdapterView<?> arg0)
        {

        }


    });

2 个答案:

答案 0 :(得分:2)

覆盖按钮的onClick方法。

请参阅:Android SDK override AlertDialog events

    AlertDialog.Builder build=new AlertDialog.Builder(this);
build.setMessage("This is a message, dawg.")
.setPositiveButton("Awesomesauce.", new OnClickListener()
{
    public void onClick(DialogInterface dialog, int id)
    {
        //Change stuff in here so that dialog.dismiss() is not called.
        //dialog.dismiss(); //closes the window
    }
};
AlertDialog myAlert=build.create();
myAlert.show();

编辑:看起来你已经有了这个...你只需要在你的onPostitiveButton onClick方法中删除dialog.dismiss(),并在它周围放一个条件来检查你的EditText中是否有一些文本

答案 1 :(得分:1)

要使AlertDialog无法取消,您需要调用此方法alert.setCancelable(false);

并显示一个Toast消息,如果Edittext为空,你只需要检查Edittext内容,如果它是空的,向他们展示一个toast别的做任何你想要的,你的代码应该看起来像下面的代码。

alert.setPositiveButton("Ok", new DialogInterface.OnClickListener()
                {
                    public void onClick(DialogInterface dialog, int whichButton)
                    {
                        if(!input.getText().toString().equals("")) // if not empty do what u were doing
                        {
                        textValueForEnteredRegion = input.getText().toString();
                        Toast.makeText(getApplicationContext(), "Input Accepted", Toast.LENGTH_SHORT).show();


                        dialog.dismiss();
                        //added soft keyboard code to make keyboard go away.
                        getWindow().setSoftInputMode(
                                  WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);


                        } else{ // here edittext is empty.. show user a toast and return,
 Toast.makeText(getApplicationContext(), "Input is empty", Toast.LENGTH_SHORT).show();


}