验证警报对话框

时间:2012-12-18 10:31:34

标签: android sqlite android-alertdialog

大家好, 我在游戏中工作,当玩家完成关卡时,我要求玩家在Alert Dialog的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();

当玩家输入他的名字并按确定(正面按钮)时,我会检查数据库中是否存在名称,如果名称存在:显示Toast而不是解雇警告写另一个名字。怎么做?

3 个答案:

答案 0 :(得分:0)

见:

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();

                      /////Here check for name in database
                      if (name does not exist)
                      {

                       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);
                       builder.dismiss()
                       }
                       else
                       {
                             Toast.makeText(getApplicationContext(), "Enter other name..",
                Toast.LENGTH_SHORT).show();
                       }
                   }
               })
               .setTitle("Please Enter Your name")
               .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {

                   }
               });
        builder.setCanceledOnTouchOutside(false);            
        alert = builder.create();
        builder.show()

答案 1 :(得分:0)

我找到了解决方案,它基于覆盖OnClick()的{​​{1}}正面按钮,这是解决方案:

AlertDialog

答案 2 :(得分:-2)

Set the dilaog as cancelable true and overide the onBackPressed() Method if need and 

    AlertDialog.Builder builder = new Builder(this);
    builder.setCancelable(true);

// onBackPressed在对话框中遇到必要条件时将其设置为true

@Override
public void onBackPressed(){
    if(IsNameInserted)
    super.onBackPressed();
}