空验证上的EditText框警告对话框 - 机器人 [英] Null Validation on EditText box in Alert Dialog - Android

查看:193
本文介绍了空验证上的EditText框警告对话框 - 机器人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想一些文字验证添加到位于一个警告对话框编辑文本字段。它提示用户在名称输入。

I am trying to add some text validation to an edit text field located within an alert dialog box. It prompts a user to enter in a name.

我想添加一些验证,这样,如果他们已经进入的是空或空,它不会从创建吐司说的错误做任何事情分开。

I want to add some validation so that if what they have entered is blank or null, it does not do anything apart from creating a Toast saying error.

到目前为止,我有:

    AlertDialog.Builder alert = new AlertDialog.Builder(this);
    alert.setTitle("Record New Track");
    alert.setMessage("Please Name Your Track:");
    // Set an EditText view to get user input
    final EditText trackName = new EditText(this);
    alert.setView(trackName);
    alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {

            String textString = trackName.getText().toString(); // Converts the value of getText to a string.
            if (textString != null && textString.trim().length() ==0)
            {   

                Context context = getApplicationContext();
                CharSequence error = "Please enter a track name" + textString;
                int duration = Toast.LENGTH_LONG;

                Toast toast = Toast.makeText(context, error, duration);
                toast.show();


            }
            else 
            {

                SQLiteDatabase db = waypoints.getWritableDatabase();
                ContentValues trackvalues = new ContentValues();
                trackvalues.put(TRACK_NAME, textString);
                trackvalues.put(TRACK_START_TIME,tracktimeidentifier );
                insertid=db.insertOrThrow(TRACK_TABLE_NAME, null, trackvalues);

            }

但是,这只是关闭警告对话框,然后显示吐司。我想警告对话框仍然是在屏幕上。

But this just closes the Alert Dialog and then displays the Toast. I want the Alert Dialog to still be on the screen.

感谢

推荐答案

我想你应该重新创建对话框,因为它似乎在 DialogInterface 的onClick()不给你一个选择停止对话框关闭

I think you should recreate the Dialog, as it seems the DialogInterface given as a parameter in onClick() doesn't give you an option to stop the closure of the Dialog.

我也有一对夫妇的提示您:

I also have a couple of tips for you:

尝试使用 Activity.onCreateDialog() Activity.on prepareDialog(),当然 Activity.showDialog()。他们使对话框的使用更容易(ATLEAST对我来说),也对话框使用看起来更像是菜单的使用。使用这些方法,你也能更easilty再次显示该对话框。

Try using Activity.onCreateDialog(), Activity.onPrepareDialog() and of course Activity.showDialog(). They make dialog usage much easier (atleast for me), also dialog usage looks more like menu usage. Using these methods, you will also be able to more easilty show the dialog again.

我想给你一个提示。这不是一个回答你的问题,但在回答这样做是更具可读性。

I want to give you a tip. It's not an answer to your question, but doing this in an answer is much more readable.

,你可以简单地做:

new AlertDialog.Builder(this)
.setTitle("Record New Track")
.setMessage("Please Name Your Track:")
//and some more method calls
.create();
//or .show();

节省您参考,并大量的输入)。 (几乎?) AlertDialog.Builder 的所有方法返回一个 AlertDialog.Builder 的对象,你可以直接调用的方法上。

Saves you a reference and a lot of typing ;). (almost?) All methods of AlertDialog.Builder return an AlertDialog.Builder object, which you can directly call a method on.

这同样适用于吐司 S:

Toast.makeText(this, "Please enter...", Toast.LENGTH_LONG).show();

这篇关于空验证上的EditText框警告对话框 - 机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆