【问题标题】:Form validation on Button click in android在android中单击按钮的表单验证
【发布时间】:2014-04-13 01:41:37
【问题描述】:

我正在开发一个 android 应用程序,其中 Login 活动具有 editText、RadioButton、Spinner 和一个按钮。因此,当按下按钮时,我必须通过检查所有字段是否填写来验证我的表单,否则会出现警报消息给用户。任何人都可以帮我处理java代码。 提前致谢。

 public void OnClickListener(View v)
{
  name=(EditText)findViewById(R.id.editText1);
  years1=(RadioButton)findViewById(R.id.radioButton3);
  years2=(RadioButton)findViewById(R.id.radioButton4);
  years3=(RadioButton)findViewById(R.id.radioButton5);
  manager=(RadioButton)findViewById(R.id.radioButton1);
  teamleader=(RadioButton)findViewById(R.id.radioButton2);
  rg1=(RadioGroup)findViewById(R.id.Designation);
  rg2=(RadioGroup)findViewById(R.id.Years);
  Button proceed = (Button) findViewById(R.id.button1);
  proceed.setOnClickListener(new View.OnClickListener()
  {
        @Override   
        public void onClick(View v) 
        {
            if (validationSuccess()) 
            {

            }

            else if(manager.isChecked())
        {
            Intent managerIntent = new Intent(getApplicationContext(), ManagerQuestionnaire1.class); // <----- START "SEARCH" ACTIVITY
            startActivityForResult(managerIntent, 0);
        }
            else if(teamleader.isChecked())
              {
              Intent teamleaderIntent = new Intent(getApplicationContext(), TeamleaderQuestionnaire1.class); // <----- START "TYPE ENTRIES OUT" ACTIVITY
              startActivityForResult(teamleaderIntent, 0);
              }
          else {
              AlertDialog();
               }
        }
  });
}




private Boolean validationSuccess()
{
    if(name.getText().toString().equalsIgnoreCase(""))
    {
    Toast.makeText(getApplicationContext(),"Please enter name",0).show();
      return false;
    }

    if(rg1.getCheckedRadioButtonId()<=0)
    {
       Toast.makeText(getApplicationContext(),"Please select designation",0).show();
      return false;
    }

    if(rg2.getCheckedRadioButtonId()<=0)
    {
        Toast.makeText(getApplicationContext(),"Please select Experience",0).show();
      return false;
    }

    if(dept.getSelectedItemPosition()==0)
    {
        Toast.makeText(getApplicationContext(),"Please select Depatrment",0).show();
      return false;
    }
    return true;
}
private void AlertDialog()
 {
     AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(Login.this);
        alertDialogBuilder.setMessage("Please ensure all Questions are answered").setCancelable(false).setPositiveButton("OK", new DialogInterface.OnClickListener()
        {
                    public void onClick(DialogInterface dialog, int id) 
                    {
                        dialog.cancel();
                    }
                });

            AlertDialog alert = alertDialogBuilder.create();
            alert.show();

 }

【问题讨论】:

标签: android validation onclicklistener android-spinner buttonclick


【解决方案1】:

您可以在按钮单击侦听器中尝试以下代码。

 if(editUsername.getText().toString().trim().length()<=0){
    //user name is not inserted
}

if(radioGroupDesignation.getCheckedRadioButtonId()<=0){
    //designation is not selected
}

if(radioGroupExp.getCheckedRadioButtonId()<=0){
    //experience is not selected
}

//set dummy item as first item of spinner (select one)

if(spinner.getSelectedItemPosition()==0){
    //department is not selected
}

【讨论】:

    【解决方案2】:

    使用下面的代码:

     mBtnNext.setOnClickListener(new OnClickListener() {
    
                    @Override
                    public void onClick(View v) {
                        if (validationSuccess()) {
    
                                Intent mIntent = new Intent(
                                        YourActivity.this,
                                        NewActivity.class);
                                } else {
                                AlertDialog();
                            }
                });
    
    
        private Boolean validationSuccess(){
        if(editUsername.getText().toString().equalsIgnoreCase("")){
        Toast.makeText(getApplicationContext();"Please enter name",0).show();
          return false;
        }
    
        if(radioGroupDesignation.getCheckedRadioButtonId()<=0){
           Toast.makeText(getApplicationContext();"Please select designation",0).show();
          return false;
        }
    
        if(radioGroupExp.getCheckedRadioButtonId()<=0){
            Toast.makeText(getApplicationContext();"Please select Experience",0).show();
          return false;
        }
    
        if(spinner.getSelectedItemPosition()==0){
            Toast.makeText(getApplicationContext();"Please select Depatrment",0).show();
          return false;
        }
        return true;
        }
    

    【讨论】:

    • 在您当前活动的下一个按钮单击时使用此代码
    • 明白了..所以你应该根据你的要求重新排列代码
    • Jyomin,请查看我的帖子我已经修改了我的验证代码。代码正在运行,但出现运行时错误。如果我将文本名称字段留空并选择按钮(继续)。显示一条消息说请输入名称,但它会打开下一个活动.. 请帮助
    【解决方案3】:

    1. > if(name.equals("")) { message="Name is required";
         edtname.requestFocus(); return false; } if(contactno.equals("")) {
         message="Contact Number is Required";
         edtcontactnumber.requestFocus(); return false; }else {
         if(contactno.length()&lt;10) { message="Invalid Contact Number";
         edtcontactnumber.requestFocus(); return false; } }
         if(address.equals("")) { message="Address is Required";
         edtaddress.requestFocus(); return false; } return true;</li>
      


    【讨论】:

      猜你喜欢
      • 2016-08-02
      • 1970-01-01
      • 2018-08-22
      • 2014-06-10
      • 2020-05-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多