javascript - 取消时 EditText 值被写入数据库值

标签 javascript android sqlite android-edittext

我有一个对话框,允许用户输入值,每个值都会经过验证以确保输入了正确的值。但是,对于无效条目,当用户单击“取消”时,EditText 的值将写入 SQLite 数据库。

这是我的对话框代码:

final AlertDialog alertDialog = alertDialogBuilderUserInput.create();
        alertDialog.show();

        alertDialog.getButton( AlertDialog.BUTTON_POSITIVE).setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Show Toast Message When No Text Is Entered
                //Validation Being Done Here
                if (TextUtils.isEmpty(inputScore.getText().toString())) {
                    inputScore.setText( "0" );
                    //return;
                }
                if (TextUtils.isEmpty(inputStrikes.getText().toString())) {
                    inputStrikes.setText( "0" );
                    //return;
                }
                if (TextUtils.isEmpty(inputSpares.getText().toString())) {
                    inputSpares.setText( "0" );
                    //return;
                }
                if (TextUtils.isEmpty(inputSplits.getText().toString())) {
                    inputSplits.setText( "0" );
                    //return;
                }
                if (TextUtils.isEmpty(inputSplitConversions.getText().toString())) {
                    inputSplitConversions.setText( "0" );
                    //return;
                }
                if (TextUtils.isEmpty(inputOpenFrames.getText().toString())) {
                    inputOpenFrames.setText( "0" );
                    //return;
                }

                final int valueScore = Integer.valueOf(inputScore.getText().toString());
                final int valueStrikes = Integer.valueOf(inputStrikes.getText().toString());
                final int valueSpares = Integer.valueOf(inputSpares.getText().toString());
                final int valueSplits = Integer.valueOf(inputSplits.getText().toString());
                final int valueSplitConversions = Integer.valueOf(inputSplitConversions.getText().toString());
                final int valueOpenFrame = Integer.valueOf(inputOpenFrames.getText().toString());
                if (valueScore > 300) {
                    inputScore.setError("Maximum value allowed is 300");
                    //return;
                } else
                if (valueStrikes > 12) {
                    inputStrikes.setError("Maximum value allowed is 12");
                    //return;
                } else
                if (valueSpares > 10) {
                    inputSpares.setError("Maximum value allowed is 10");
                    //return;
                } else
                if (valueSplits > 10) {
                    inputSplits.setError("Maximum value allowed is 10");
                    //return;
                } else
                if (valueSplitConversions > 10) {
                    inputSplitConversions.setError("Maximum value allowed is 10");
                    //return;
                } else
                if (valueOpenFrame > 10) {
                    inputOpenFrames.setError("Maximum value allowed is 10");
                    //return;
                } else {

                        alertDialog.dismiss();
                    }


                //Check If User Is Updating Game
                //Create New Game
                if (shouldUpdate && game != null) {
                    //Update Game By It's Id
                    updateGame(inputScore.getText().toString(), inputStrikes.getText().toString(), inputSpares.getText().toString(), inputSplits.getText().toString(), inputSplitConversions.getText().toString(), inputOpenFrames.getText().toString(), position);
                } else {
                    createGame(leagueId.getText().toString(), bowlerId.getText().toString(), seriesId.getText().toString(), inputScore.getText().toString(), inputStrikes.getText().toString(), inputSpares.getText().toString(), inputSplits.getText().toString(), inputSplitConversions.getText().toString(), inputOpenFrames.getText().toString());
                }
                seriesAverage = db.getSeriesAverage(savedLeagueId, savedBowlerId, savedSeriesId);
                bowlerAverage = db.getBowlerAverage(savedLeagueId, savedBowlerId);
                leagueAverage = db.getLeagueAverage(savedLeagueId);
            }
        });

    }

我希望它立即显示所有不正确的字段,并且不保存取消时的值。但似乎无法让这种情况发生。下面是当前正在发生的情况的图片。

enter image description here

最佳答案

尝试此代码,并使用一个 bool 标志来指示所有值是否有效。
我添加了取消按钮的代码,只是为了关闭对话框(我希望没有拼写错误)。

final AlertDialog alertDialog = alertDialogBuilderUserInput.create();
alertDialog.show();

alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener( new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        //Show Toast Message When No Text Is Entered
        //Validation Being Done Here
        Boolean allValid = true;

        if (TextUtils.isEmpty(inputScore.getText().toString())) {
            allValid = false;
            inputScore.setText( "0" );
        }
        if (TextUtils.isEmpty(inputStrikes.getText().toString())) {
            allValid = false;
            inputStrikes.setText( "0" );
        }
        if (TextUtils.isEmpty(inputSpares.getText().toString())) {
            allValid = false;
            inputSpares.setText( "0" );
        }
        if (TextUtils.isEmpty(inputSplits.getText().toString())) {
            allValid = false;
            inputSplits.setText( "0" );
        }
        if (TextUtils.isEmpty(inputSplitConversions.getText().toString())) {
            allValid = false;
            inputSplitConversions.setText( "0" );
        }
        if (TextUtils.isEmpty(inputOpenFrames.getText().toString())) {
            allValid = false;
            inputOpenFrames.setText( "0" );
        }

        final int valueScore = Integer.valueOf(inputScore.getText().toString());
        final int valueStrikes = Integer.valueOf(inputStrikes.getText().toString());
        final int valueSpares = Integer.valueOf(inputSpares.getText().toString());
        final int valueSplits = Integer.valueOf(inputSplits.getText().toString());
        final int valueSplitConversions = Integer.valueOf(inputSplitConversions.getText().toString());
        final int valueOpenFrame = Integer.valueOf(inputOpenFrames.getText().toString());
        if (valueScore > 300) {
            allValid = false;
            inputScore.setError("Maximum value allowed is 300");
        } 
        if (valueStrikes > 12) {
            allValid = false;
            inputStrikes.setError("Maximum value allowed is 12");
        } 
        if (valueSpares > 10) {
            allValid = false;
            inputSpares.setError("Maximum value allowed is 10");
        } 
        if (valueSplits > 10) {
            allValid = false;
            inputSplits.setError("Maximum value allowed is 10");
        } 
        if (valueSplitConversions > 10) {
            allValid = false;
            inputSplitConversions.setError("Maximum value allowed is 10");
        } 
        if (valueOpenFrame > 10) {
            allValid = false;
            inputOpenFrames.setError("Maximum value allowed is 10");
        } 

        if (!allValid) {
            return;
        }

        alertDialog.dismiss();

        //Check If User Is Updating Game
        //Create New Game
        if (shouldUpdate && game != null) {
            //Update Game By It's Id
            updateGame(inputScore.getText().toString(), inputStrikes.getText().toString(), inputSpares.getText().toString(), inputSplits.getText().toString(), inputSplitConversions.getText().toString(), inputOpenFrames.getText().toString(), position);
        } else {
            createGame(leagueId.getText().toString(), bowlerId.getText().toString(), seriesId.getText().toString(), inputScore.getText().toString(), inputStrikes.getText().toString(), inputSpares.getText().toString(), inputSplits.getText().toString(), inputSplitConversions.getText().toString(), inputOpenFrames.getText().toString());
        }
        seriesAverage = db.getSeriesAverage(savedLeagueId, savedBowlerId, savedSeriesId);
        bowlerAverage = db.getBowlerAverage(savedLeagueId, savedBowlerId);
        leagueAverage = db.getLeagueAverage(savedLeagueId);
    }
});

alertDialog.getButton( AlertDialog.BUTTON_NEGATIVE).setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        alertDialog.dismiss();
    }
});

关于javascript - 取消时 EditText 值被写入数据库值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51695546/

相关文章:

java - 我无法提供 Token Firebase

android - 什么时候关闭android上的数据库连接?每次操作完成或应用退出后

android - 无法使用 JMeter 录制 Android 应用程序

javascript - 在 Q Promise (Node.js) 中运行顺序函数

javascript - 如何在 json_encode 的警报弹出窗口中显示新行?

javascript - 比较多个值并分配关联变量

android - 如何测试订阅的Google Play实时通知?

android - 在 Android 中获取 Sqlite 数据库轮询异常

sqlite - SQL IFNULL,带条件并写入CSV

javascript - 如何允许用户在不经过身份验证/登录过程的情况下查看我的 Google Analytics 图表数据