0

大家好,我正在玩游戏,当玩家完成关卡时,我要求玩家在警报对话框的 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 并且不关闭警报以写入另一个名称。怎么做?

4

3 回答 3

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()
于 2012-12-18T10:55:11.840 回答
0

我找到了解决方案,它基于覆盖OnClick()for positive 按钮,AlertDialog这是解决方案:

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

                               }

                         }
                     });
                }
            });
于 2012-12-18T21:00:14.267 回答
-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();
}
于 2012-12-18T10:44:30.280 回答