Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/230.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 警报对话框的验证_Android_Sqlite_Android Alertdialog - Fatal编程技术网

Android 警报对话框的验证

Android 警报对话框的验证,android,sqlite,android-alertdialog,Android,Sqlite,Android Alertdialog,大家好,, 我正在游戏中工作,当玩家完成关卡时,我要求玩家在警报对话框的编辑文本中输入他的名字: AlertDialog.Builder builder = new Builder(this); LayoutInflater inflater = getLayoutInflater(); // Inflate and set the layout for the dialog // Pass null as the parent view bec

大家好,, 我正在游戏中工作,当玩家完成关卡时,我要求玩家在警报对话框的编辑文本中输入他的名字:

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,而不是取消写另一个名字的警告。如何做到这一点?

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

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

我找到了解决方案,它基于覆盖
AlertDialog
的肯定按钮的
OnClick()
,以下是解决方案:

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

                               }

                         }
                     });
                }
            });

我已将builder.setCanceledOnTouchOut设置为(false);但不起作用,当用户单击“确定”时仍然可以解除警报对不起,Vipin它不起作用,我希望用户在输入有效名称之前不能解除警报可能是您正在膨胀您的视图您需要为对话框中的按钮设置自己的按钮单击侦听器,如果您的绑定视图,则在视图中使用按钮,然后