如何检查选择的警报对话框项目?

时间:2015-01-12 11:03:19

标签: android android-alertdialog

我在textview上设置了警告对话框,我在那里使用了String数组,现在如何检查没有选择任何项目,如果没有项目被选中则不应该提交..检查下面我的代码片段..提前谢谢..

  sp3=(TextView)findViewById(R.id.daydates);
  btnsubmit=(Button)findViewById(R.id.btnreg);

 final String[] items = new String[] {"01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31"};
    final ArrayAdapter<String> adapter123 = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_dropdown_item, items);


    sp3.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View w) {
              new AlertDialog.Builder(RegistrationForm.this)
              .setTitle("Select Day")
              .setAdapter(adapter123, new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {

                    sp3.setText(adapter123.getItem(which).toString());

                  dialog.dismiss();
                }
              }).create().show();
            }
    });
   btnsubmit.setOnClickListener(new OnClickListener()
     {

        @Override
        public void onClick(View v) 
        {
            if(emailtext.getText().toString().trim().length() > 0 && passtext.getText().toString().trim().length() > 0 && confirmpass.getText().toString().trim().length() > 0 && username.getText().toString().trim().length() > 0 && firstname.getText().toString().trim().length() > 0 && lastname.getText().toString().trim().length() > 0 && isSetDay==true && isSetMonth==true && isSetYear==true && isSetPro==true)
        {
            isSetDay=false;
            isSetMonth=false;
            isSetYear=false;
            isSetPro=false;
        new AttemptLogin().execute();

        final String uname=username.getText().toString();
        if(!isValidUserName(uname))
        {
            username.setError("Invalid User Name");
        }
        final String fname=firstname.getText().toString();
        if(!isValidFirstName(fname))
        {
            firstname.setError("Invalid First Name");
        }
        final String lname=lastname.getText().toString();
        if(!isValidLastName(lname))
        {
            lastname.setError("Invalid Last Name");
        }

        }

        else
        {
            Toast.makeText(getApplicationContext(), "Fill the details", Toast.LENGTH_LONG).show();
        }
        break;
        }
    });

1 个答案:

答案 0 :(得分:1)

为什么不使用布尔值来确定在单击btnsubmit时是否已设置sp3,然后在需要重新使用时重置它。

这是我的建议:

 sp3=(TextView)findViewById(R.id.daydates);
 btnsubmit=(Button)findViewById(R.id.btnreg);
 boolean isSet = false;

 final String[] items = new String[] {"01","02","03","04","05","06","07","08","09","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31"};
    final ArrayAdapter<String> adapter123 = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_dropdown_item, items);


    sp3.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View w) {
              new AlertDialog.Builder(RegistrationForm.this)
              .setTitle("Select Day")
              .setAdapter(adapter123, new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {

                    sp3.setText(adapter123.getItem(which).toString());
                    isSet = true;

                  dialog.dismiss();
                }
              }).create().show();
            }
    });

     btnsubmit.setOnClickListener(new OnClickListener()
     {

        @Override
        public void onClick(View v) 
        {
            if(!isSet)
            {
                //Do Something
                isSet = false;
            }
        }
    });