简体   繁体   English

如何为单选按钮分配整数值?

[英]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). 目前,我在活动中有四个单选按钮,每个按钮代表一个值(例如10 $,50 $,100 $和200 $)。 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. 问题是当我尝试实现多个解决方案(例如执行“ Case函数”或“ If-Else”循环)时,遇到各种错误,例如“ ==”不接受整数或分配给每个单选按钮的变量从未使用过。

So, how to properly assign integer values to these radiobuttons? 那么,如何为这些单选按钮正确分配整数值呢?

"The Java code of the activity where the radiobuttons are located." “单选按钮所在活动的Java代码。”

    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() 您可以使用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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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