简体   繁体   中英

How to assign an integer value to a radiobutton?

Currently I have four radiobuttons in the activity and each represent a value (10$, 50$, 100$ and 200$, for example). Now i would like to assign the integer values of these numbers to each of these radiobuttons.

The problem is when I tried implementing multiple solutions such as doing a "Case function", or an "If-Else" loop, I get various errors such as "==" doesn't accept Integer, or the variable assigned to each radiobutton is never used.

So, how to properly assign integer values to these radiobuttons?

"The Java code of the activity where the radiobuttons are located."

    package com.example.msi.fyp;

    import android.content.Intent;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.RadioButton;
    import android.widget.RadioGroup;

    import static com.example.msi.fyp.R.id.bProceed;

    public class Process extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_process);

    Button bProceed = (Button) findViewById(bProceed);
    final RadioButton btn10 = (RadioButton) findViewById(R.id.btn10);
    final RadioButton btn50 = (RadioButton) findViewById(R.id.btn50);
    final RadioButton btn100 = (RadioButton) findViewById(R.id.btn100);
    final RadioButton btn200 = (RadioButton) findViewById(R.id.btn200);
    EditText credNum = (EditText) findViewById(R.id.credNum);
    EditText cvv = (EditText) findViewById(R.id.cvv);
    Radiogroup amnts = (Radiogroup) findViewById(R.id.amnts);

    bProceed.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(new Intent(Process.this, done_donation.class));
        }
    });

}
}  

(The reason the code looks short because I deleted those "radiobutton value assigning" codes I mentioned above as they gave errors.

You can use setTag()

Sets the tag associated with this view. A tag can be used to mark a view in its hierarchy and does not have to be unique within the hierarchy. Tags can also be used to store data within a view without resorting to another data structure.

SAMPLE CODE

radioButton.setTag(150);

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