1

我目前正在尝试保持一个警报对话框,并在屏幕上显示编辑文本。像往常一样,有“确定”和“取消”按钮,我想在该过程中添加一些错误处理。如果用户没有在编辑文本框中输入任何文本并尝试按 OK,我希望弹出一条 toast 消息,通知他们他们的错误。对话框应该仍然显示,因为如果他们想按 OK,仍然需要输入一些内容。

这就是我到目前为止所拥有的。顺便说一句,对话框会根据用户从下拉列表中选择的内容弹出。任何帮助让对话框保持不变将不胜感激!!!

这是我的代码:

    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)
        {

        }


    });
4

2 回答 2

2

覆盖按钮的 onClick 方法。

请参阅:Android SDK 覆盖 AlertDialog 事件

    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 中是否有一些文本。

于 2012-08-02T15:39:33.223 回答
1

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

如果 Edittext 为空,则要显示 toast 消息,您只需要检查 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();


}
于 2012-08-02T15:46:34.980 回答