验证了警报对话框 [英] Validation for Alert dialog
本文介绍了验证了警报对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
林在一场比赛中工作,当玩家完成了水平而言,我要求球员在警告对话框上的EditText输入他的名字:
Hi Everyone, Im working in a game and when the player finished the level , Im asking the Player to enter his name on EditText in Alert Dialog :
AlertDialog.Builder builder = new Builder(this);
LayoutInflater inflater = getLayoutInflater();
// Inflate and set the layout for the dialog
// Pass null as the parent view because its going in the dialog layout
builder.setView(inflater.inflate(R.layout.dialog_name, null))
// Add action buttons
.setPositiveButton(R.string.alertName, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
playerText = (EditText) ((AlertDialog)dialog).findViewById(R.id.username);
if(playerText.getText() != null && playerText.getText().length() != 0)
Helper.currentPlayer.setName(playerText.getText().toString());
//calculate the score
// save in data base
PlayerDataSource playerDS = new PlayerDataSource(getApplicationContext());
playerDS.open();
Helper.currentPlayer = playerDS.createPlayer(Helper.currentPlayer.getName(), Helper.currentPlayer.getScore(),
Helper.currentPlayer.levels, Helper.currentPlayer.times, LanguageHelper.div);
playerDS.close();
saveStmc();
mainScene.setChildScene(choicesMenu, false, true, true);
}
})
.setTitle("Please Enter Your name")
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
});
alert = builder.create();
当玩家进入他的名字和preSS 确定(正按钮)我将在数据库中检查该名称的存在与否,如果名字存在:显示吐司和不排除再写名字警报。那怎么办?
When the Player enters his name and press OK (Positive button) I will check in database if the name exists or not, if the name is exist : show Toast and not dismiss the alert to write another name. How to do that?
推荐答案
我已经找到了解决方案,它是基于覆盖的OnClick()
为阳性按钮 AlertDialog
这里是解决方案:
I have found the solution, it is based on overriding the OnClick()
for positive button of the AlertDialog
and here is the solution:
AlertDialog.Builder builder = new Builder(this);
LayoutInflater inflater = getLayoutInflater();
// Inflate and set the layout for the dialog
// Pass null as the parent view because its going in the dialog layout
builder.setView(inflater.inflate(R.layout.dialog_name, null))
.setPositiveButton(R.string.alertName, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
}
})
.setTitle("Please Enter Your name");
builder = builder.setCancelable(false);
alert = builder.create();
alert.setOnShowListener(new OnShowListener() {
@Override
public void onShow(DialogInterface dialog) {
alert = (AlertDialog)dialog;
Button b = alert.getButton(AlertDialog.BUTTON_POSITIVE);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
playerText = (EditText) ((AlertDialog)alert).findViewById(R.id.username);
if(playerText.getText() == null || playerText.getText().length() == 0)
{
showToastMsg("Please enter valid name");
}
else
{
if(nameIsExist())
{
showToastMsg("Name already exist!!");
}
else
{
//Do stuff because every this is OK
alert.dismiss();
}
}
}
});
}
});
这篇关于验证了警报对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文