【问题标题】:How to add radio buttons' float value together?如何将单选按钮的浮点值加在一起?
【发布时间】:2017-04-05 06:47:20
【问题描述】:

我试图在我的代码中查找错误。而且我不知道为什么我的应用程序总是崩溃。我试图添加用户单击单选按钮的值。但是当我点击提交按钮时,我的应用程序不幸停止了。并假设单选按钮只能在单选组中单击一次,但我的,所有按钮都可以单击。你们能帮帮我吗?

p/s 我在这方面还是个新手,所以请不要嘲笑我的编码。我真的不知道从哪里开始。从那以后已经过去了两个星期,如果您能就此提供一些反馈,我将不胜感激。

这是我的 xml

<TextView android:id="@+id/tv2" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:fontFamily="serif" android:paddingTop="10dp" android:text=" 1. INTRODUCTION (Problem statement, research 
                objectives, research questions, hypothesis, limitation of the
                  study, significance of the study)" android:textAppearance="@style/TextAppearance.AppCompat" />

<RadioGroup android:id="@+id/rg1" android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingTop="10dp">

  <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal">

    <RadioButton android:id="@+id/rb1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:checked="false" android:text="@string/_1" />

    <RadioButton android:id="@+id/rb2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:checked="false" android:text="@string/_2" />

    <RadioButton android:id="@+id/rb3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:checked="false" android:text="@string/_3" />

    <RadioButton android:id="@+id/rb4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:checked="false" android:text="@string/_4" />

    <RadioButton android:id="@+id/rb5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:checked="false" android:text="@string/_5" />
  </LinearLayout>
</RadioGroup>

</LinearLayout>

<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginTop="10dp" android:orientation="vertical">

  <TextView android:id="@+id/tv3" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:fontFamily="serif" android:paddingTop="10dp" android:text=" 2. LITERATURE REVIEW (Relevant literature)" android:textAppearance="@style/TextAppearance.AppCompat"
  />

  <RadioGroup android:id="@+id/rg2" android:layout_width="match_parent" android:layout_height="match_parent">

    <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" android:paddingTop="10dp">

      <RadioButton android:id="@+id/rb6" style="@style/Widget.AppCompat.CompoundButton.RadioButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="1" />

      <RadioButton android:id="@+id/rb7" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="2" />

      <RadioButton android:id="@+id/rb8" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="3" />

      <RadioButton android:id="@+id/rb9" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="4" />

      <RadioButton android:id="@+id/rb10" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="5" />
    </LinearLayout>
  </RadioGroup>
</LinearLayout>

<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginTop="10dp" android:orientation="vertical">


  <TextView android:id="@+id/tv4" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:fontFamily="serif" android:paddingTop="10dp" android:text=" 3. RESEARCH METHODOLOGY (Research design, research context, data collection, data analysis)"
    android:textAppearance="@style/TextAppearance.AppCompat" />

  <RadioGroup android:id="@+id/rg3" android:layout_width="match_parent" android:layout_height="match_parent">

    <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" android:paddingTop="10dp">

      <RadioButton android:id="@+id/rb11" style="@android:style/Widget.CompoundButton.RadioButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="1" />

      <RadioButton android:id="@+id/rb12" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="2" />

      <RadioButton android:id="@+id/rb13" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="3" />

      <RadioButton android:id="@+id/rb14" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="4" />

      <RadioButton android:id="@+id/rb15" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="5" />
    </LinearLayout>
  </RadioGroup>
</LinearLayout>

<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical">

  <TextView android:id="@+id/tv5" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:fontFamily="serif" android:paddingTop="10dp" android:text=" 4. RESULT AND FEASIBILITY OF STUDY (Justification of the result, implication of the study, recommendation for future research"
    android:textAppearance="@style/TextAppearance.AppCompat" />

  <RadioGroup android:id="@+id/rg4" android:layout_width="match_parent" android:layout_height="match_parent">

    <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" android:paddingTop="10dp">

      <RadioButton android:id="@+id/rb16" style="@android:style/Widget.DeviceDefault.CompoundButton.RadioButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="1" />

      <RadioButton android:id="@+id/rb17" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="2" />

      <RadioButton android:id="@+id/rb18" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="3" />

      <RadioButton android:id="@+id/rb19" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="4" />

      <RadioButton android:id="@+id/rb20" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="5" />
    </LinearLayout>
  </RadioGroup>
</LinearLayout>

<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical">

  <TextView android:id="@+id/tv6" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:fontFamily="serif" android:paddingTop="10dp" android:text=" 5. POSTER PRESENTATION (Type size and type face are easily read, aesthetically pleasing layout, appropriate pictures, and colour is strategically used)"
    android:textAppearance="@style/TextAppearance.AppCompat" />

  <RadioGroup android:id="@+id/rg5" android:layout_width="match_parent" android:layout_height="match_parent">

    <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" android:paddingTop="10dp">

      <RadioButton android:id="@+id/rb21" style="@android:style/Widget.DeviceDefault.Light.CompoundButton.RadioButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="1" />

      <RadioButton android:id="@+id/rb22" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="2" />

      <RadioButton android:id="@+id/rb23" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="3" />

      <RadioButton android:id="@+id/rb24" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="4" />

      <RadioButton android:id="@+id/rb25" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="5" />
    </LinearLayout>
  </RadioGroup>
</LinearLayout>

<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical">

  <TextView android:id="@+id/tv7" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:fontFamily="serif" android:paddingTop="10dp" android:text=" 6. PRESENTATION SKILLS (Smooth transition, eye contact, voice and pacing, enthusiasm, body language)"
    android:textAppearance="@style/TextAppearance.AppCompat" />

  <RadioGroup android:id="@+id/rg6" android:layout_width="match_parent" android:layout_height="match_parent">

    <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:orientation="horizontal" android:paddingTop="10dp">

      <RadioButton android:id="@+id/rb26" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="1" />

      <RadioButton android:id="@+id/rb27" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="2" />

      <RadioButton android:id="@+id/rb28" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="3" />

      <RadioButton android:id="@+id/rb29" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="4" />

      <RadioButton android:id="@+id/rb30" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="5" />
    </LinearLayout>

  </RadioGroup>
</LinearLayout>

<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical">

  <TextView android:id="@+id/tv8" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:fontFamily="serif" android:paddingTop="10dp" android:text=" 7. CONCLUSION (End argument, defend the research, able to answer questions based on research)"
    android:textAppearance="@style/TextAppearance.AppCompat" />

  <RadioGroup android:id="@+id/rg7" android:layout_width="match_parent" android:layout_height="match_parent">

    <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:orientation="horizontal" android:paddingTop="10dp">

      <RadioButton android:id="@+id/rb31" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="1" />

      <RadioButton android:id="@+id/rb32" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1" android:text="2" />

      <RadioButton android:id="@+id/rb33" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="3" />

      <RadioButton android:id="@+id/rb34" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="4" />

      <RadioButton android:id="@+id/rb35" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="5" />
    </LinearLayout>

  </RadioGroup>
</LinearLayout>

<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginTop="10dp" android:orientation="vertical" android:weightSum="1">

  <TextView android:id="@+id/tv_comment" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:fontFamily="serif" android:paddingBottom="10dp" android:paddingTop="10dp" android:text="Comment" android:textAppearance="@style/TextAppearance.AppCompat"
    android:textSize="18sp" android:textStyle="bold" />

  <EditText android:id="@+id/et_comment" android:layout_width="match_parent" android:layout_height="0dp" android:layout_marginTop="10dp" android:ems="10" android:fontFamily="serif" android:gravity="top" android:inputType="textMultiLine" android:lines="5"
    android:paddingTop="20dp" android:text="Please Enter Your Comment Here" android:layout_weight="0.85" />
</LinearLayout>

<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal">

  <Button android:id="@+id/btn_submit" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="Submit" />
</LinearLayout>

<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal">

  <TextView android:id="@+id/tv_outcome" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</ScrollView>

这是我的编码

package com.example.posterevaluation;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.view.View;

import android.widget.RadioButton;

import android.widget.RadioGroup;

import android.widget.Button;

import android.widget.TextView;


public class FormActivity extends AppCompatActivity {

//RadioGroup rg1,rg2,rg3,rg4,rg5,rg6,rg7;
// RadioButton rb1, rb2,rb3,rb4,rb5,rb6,rb7;
// EditText et1;
Button btn_submit;
TextView tv_outcome;

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

    final RadioGroup rg1 = (RadioGroup) findViewById(R.id.rg1);
    final RadioGroup rg2 = (RadioGroup) findViewById(R.id.rg2);
    final RadioGroup rg3 = (RadioGroup) findViewById(R.id.rg3);
    final RadioGroup rg4 = (RadioGroup) findViewById(R.id.rg4);
    final RadioGroup rg5 = (RadioGroup) findViewById(R.id.rg5);
    final RadioGroup rg6 = (RadioGroup) findViewById(R.id.rg6);
    final RadioGroup rg7 = (RadioGroup) findViewById(R.id.rg7);
    //et1 = (EditText) findViewById (R.id.et1);

    tv_outcome = (TextView) findViewById(R.id.tv_outcome);

    btn_submit = (Button) findViewById(R.id.btn_submit);


    btn_submit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            String value1 = ((RadioButton) findViewById(rg1.getCheckedRadioButtonId())).getText().toString();
            String value2 = ((RadioButton) findViewById(rg2.getCheckedRadioButtonId())).getText().toString();
            String value3 = ((RadioButton) findViewById(rg3.getCheckedRadioButtonId())).getText().toString();
            String value4 = ((RadioButton) findViewById(rg4.getCheckedRadioButtonId())).getText().toString();
            String value5 = ((RadioButton) findViewById(rg5.getCheckedRadioButtonId())).getText().toString();
            String value6 = ((RadioButton) findViewById(rg6.getCheckedRadioButtonId())).getText().toString();
            String value7 = ((RadioButton) findViewById(rg7.getCheckedRadioButtonId())).getText().toString();


            float marks = Float.parseFloat(value1 + value2 + value3 + value4 + value5 + value6 + value7);

            //calculate total

            float total = makeCalculations(marks);

            tv_outcome.setText(String.valueOf(total));

        }
    });   

}

private float makeCalculations(float marks) {

    return ((marks / 35) * 100);

}
}

【问题讨论】:

  • > 检查此answer
  • if (rg.isChecked()),把它放在值中。然后添加它。
  • 我是否需要为我的每个广播组添加该编码?

标签: android


【解决方案1】:

您在此处发布了 xml 和 java 文件,但如果我们也有您的 日志 会更有帮助。所以,下次请不要忘记你的日志。

应大家的要求,如果你想一次单击一个单选按钮,首先从RadioGroup中删除LinearLayout,如果你想要那些单选,将RadioGroup的方向设置为horizontal像这样在一行中的按钮;

<RadioGroup
  android:id="@+id/rg1"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:orientation="horizontal">

  <RadioButton
    android:id="@+id/rb1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/_1" />

  <RadioButton
    android:id="@+id/rb2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/_2" />

  <RadioButton
    android:id="@+id/rb3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/_3" />

  <RadioButton
    android:id="@+id/rb4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/_4" />

  <RadioButton
    android:id="@+id/rb5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/_5" />
</RadioGroup>

而且,我认为,由于我们这里没有日志,这就是您的代码对我来说很奇怪的地方。试试

 float marks = Float.parseFloat(value1) + Float.parseFloat(value2) + Float.parseFloat(value3) + Float.parseFloat(value4) + Float.parseFloat(value5) + Float.parseFloat(value6) + Float.parseFloat(value7);

而不是

float marks = Float.parseFloat(value1 + value2 + value3 + value4 + value5 + value6 + value7);

【讨论】:

  • 天哪。太感谢了!它有效.. 我会提前两周问我是否知道会发生这种情况... tqvm!!
  • 这就是问题所在。欢迎和愉快的编码。
【解决方案2】:

您无需在RadioGroup 中使用LinearLayout。设置RadioGroup 的方向并将RadioButton 直接放在RadioGroup 内。

<RadioGroup
    android:id="@+id/rg1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:paddingTop="10dp">

    <RadioButton
        android:id="@+id/rb1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:checked="false"

        android:text="@string/_1" />

    <RadioButton
        android:id="@+id/rb2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:checked="false"

        android:text="@string/_2" />

    <RadioButton
        android:id="@+id/rb3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"

        android:checked="false"

        android:text="@string/_3" />

    <RadioButton
        android:id="@+id/rb4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:checked="false"

        android:text="@string/_4" />

    <RadioButton
        android:id="@+id/rb5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:checked="false"

        android:text="@string/_5" />
</RadioGroup>

【讨论】:

  • 我希望它在水平线上。我也使用了滚动视图。如果我删除线性布局,它将成为错误
  • 您可以将RadioGroup 方向设置为水平。删除RadioGroup内的LinearLayout。
  • 谢谢。它有帮助,但计算有问题。当我点击所有单选按钮“5”时,它假设为 100%。但它变成了 1.5873014E7。你能看看我的计算部分吗?
【解决方案3】:

RadioButton 应该是 RadioGroup 的直接子级,否则分组不起作用。

例如:

<RadioGroup android:id="@+id/rg4" android:layout_width="match_parent" android:layout_height="match_parent">

  <RadioButton android:id="@+id/rb16" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="1" />

  <RadioButton android:id="@+id/rb17" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="2" />

  <RadioButton android:id="@+id/rb18" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="3" />

  <RadioButton android:id="@+id/rb19" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="4" />

  <RadioButton android:id="@+id/rb20" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="5" />
</RadioGroup>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-24
    • 2015-08-25
    • 2018-06-01
    • 2012-06-07
    • 2021-04-14
    • 2012-05-20
    相关资源
    最近更新 更多