简体   繁体   中英

How to validate to rating bar on Button click in Android?

I want to validation on RatingBar in Android. I have 5 rating Bar and 1 Button. I don't want to submit the data without pressed the rating bar. I want to take validation on Rating bar.
Can someone help me. How to take validation on Rating bar?

Here is my Activity code:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.rating_baar);


    databaseHelper = new DatabaseHelper(this);
    databaseHelper.onOpen(db);

    addListenerOnRatingBar_one();
    addListenerOnRatingBar_two();
    addListenerOnRatingBar_three();
    addListenerOnRatingBar_four();
    addListenerOnRatingBar_five();
    addListenerOnButton();

}

@SuppressLint("SimpleDateFormat")
public void addListenerOnButton() {
    buttonSubmitRate = (Button) findViewById(R.id.button_SubmitRate);
    buttonSubmitRate.setOnClickListener(new OnClickListener() {

        @SuppressLint("SimpleDateFormat")
        @Override
        public void onClick(View v) {
             if((etTaskName.getText().toString().isEmpty()))
                 etTaskName.setError("Field Can Not Be Empty !");
             else if (!etTaskName.getText().toString().trim().matches("[a-zA-Z{  }]+"))
                 etTaskName.setError("Accept Alphabets Only.");

             else {
                 strEmpName = textViewNAme.getText().toString().trim();
                 strTaskName = etTaskName.getText().toString().trim();

                 String strCurrentDate = new SimpleDateFormat("dd/MM/yyyy").format(new Date());
                 System.out.println("strCurrentDate  = " + strCurrentDate);

                 String strCurrentMonth = new SimpleDateFormat("MMM").format(new Date());
                 System.out.println("strCurrentDate  = " + strCurrentMonth);

                 String strCurrenYear = new SimpleDateFormat("yyyy").format(new Date());
                 System.out.println("strCurrenYear  = " + strCurrenYear);

                 System.out.println("__________________________________________________________________________________");

                 databaseHelper.insertPerformance_Details(intentStr1, strEmpName,
                                                           strTaskName, rateVal_one, 
                                                           rateVal_two, rateVal_three, 
                                                           rateVal_four,rateVal_five, 
                                                           strCurrentDate,strCurrentMonth,
                                                           strCurrenYear);
                 System.out.println("Data Add SuccesFully !!!!");
                 etTaskName.setText("");

                 Intent i = new Intent(RatingBaar_Class.this, Rating_Msg.class);
                 startActivity(i);
                 finish();
                 overridePendingTransition(R.anim.anim_in,R.anim.anim_out);
            }
        } 
    });   
}

public void addListenerOnRatingBar_one() {
    ratingBar_one = (RatingBar) findViewById(R.id.ratingBar1);
    ratingBar_one.setOnRatingBarChangeListener(new OnRatingBarChangeListener() {
        public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) { 
            rateVal_one = String.valueOf(rating);
            System.out.println(" rateVal_one = " + rateVal_one);
        }
    });
}

/* Exactly the same methods for four other rating bars, except for variable names */
public void addListenerOnRatingBar_two() { ... }
public void addListenerOnRatingBar_three() { ... }
public void addListenerOnRatingBar_four() { ... }
public void addListenerOnRatingBar_five() { ... }

At first set all rating bar to 0 value. Then on click to button, check if the value has altered in all of them. If so, only valid else invalid.

Also you can use listener to each rating bar and change value of some boolean variable to true

Example:

boolean flag1 = false;
boolean flag2 = false;
.... //similarly upto 5

Then in rating bar listener1

{
flag1 = true;
}

Similarly for all set flag to be true

And in button onClickListener do the following:

if(flag1 && flag2 && flag3 && flag4 && flag5){
//do your work
}else{
//display message that one of the rating bar hasn't been touched
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM