大家好,我正在玩游戏,当玩家完成关卡时,我要求玩家在警报对话框的 EditText 上输入他的名字:
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();
当玩家输入他的名字并按OK(确定按钮)时,我将检查数据库是否存在该名称,如果存在该名称:显示 Toast 并且不关闭警报以写入另一个名称。怎么做?