我有一个对话框,允许用户输入值,每个值都经过验证,以确保输入正确的值。但是,在无效条目上,当用户单击“取消”时,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);
}
});
}
我希望它一次显示所有不正确的字段,并且不将值保存为取消。似乎无法做到这一点。下面是当前情况的图像。
答案 0 :(得分:1)
尝试此代码,并带有一个布尔标志,该标志指示所有值是否有效。
我包含了“取消”按钮的代码,只是为了关闭对话框(希望没有错别字)。
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();
}
});