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] (正のボタン)を押すと、名前が存在するかどうかをデータベースで確認します。名前が存在する場合:トーストを表示し、別の名前を書き込むアラートを閉じません。どうやってするか?

4

3 に答える 3

0

OnClick()私は解決策を見つけました、それはのポジティブボタンをオーバーライドすることに基づいています、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 に答える
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 に答える
-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 に答える