在Android中单击按钮时进行表单验证 [英] Form validation on Button click in android

查看:365
本文介绍了在Android中单击按钮时进行表单验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个Android应用程序,其中的Login活动具有editText,RadioButton,Spinner和一个按钮.因此,当按下按钮时,我必须通过检查是否所有字段都已填写来验证表单,否则会收到警告消息提供给用户.任何人都可以在Java代码方面为我提供帮助. 预先感谢.

I'm Developing an android app in which the Login activity has editText,RadioButton,Spinner and a button.So when the button is pressed I've to validate my form by checking whether all the fields filled else a alert message is given to the user.Can anyone please help me with the java code. Thanks in Advance.

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

 }

推荐答案

使用以下代码:

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

这篇关于在Android中单击按钮时进行表单验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆