Java 无线组设置检查更改侦听器

Java 无线组设置检查更改侦听器,java,android,xml,radio-button,radio-group,Java,Android,Xml,Radio Button,Radio Group,我对广播组有一些问题,我是Android开发新手,所以我不知道如何处理它。这是一个表单,有一些输入字段和广播组,用于选择性别和保存按钮。 但我不知道我的radiogroup setoncheakchangelistener到底放在哪里?所以它给了我错误 主要活动: package com.dietandroidproject; import Databasedata.Person; import android.app.Activity; import android.os.Bundle; i

我对广播组有一些问题,我是Android开发新手,所以我不知道如何处理它。这是一个表单,有一些输入字段和广播组,用于选择性别和保存按钮。 但我不知道我的radiogroup setoncheakchangelistener到底放在哪里?所以它给了我错误

主要活动:

package com.dietandroidproject;

import Databasedata.Person;
import android.app.Activity;
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 android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends Activity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final RadioGroup genderselected = (RadioGroup) findViewById(R.id.selectgender);

    genderselected.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()            {

        @Override
        public void onCheckedChanged(RadioGroup arg0, int selectedId) {
            selectedId=genderselected.getCheckedRadioButtonId();
            RadioButton genderchoosed = (RadioButton) findViewById(selectedId);
            String gender = genderchoosed.getText().toString();

        }
     });

    Button saveinformation = (Button) findViewById(R.id.saveinformation);
    saveinformation.setOnClickListener(new View.OnClickListener() {

        EditText weighttext = (EditText) findViewById(R.id.weighttext);
        EditText heighttext = (EditText) findViewById(R.id.heighttext);
        EditText usernametext = (EditText) findViewById(R.id.usernametext);
        EditText agetext = (EditText) findViewById(R.id.agetext);
        Spinner activitytext = (Spinner) findViewById(R.id.chooseactivity);
        Button saveinformation = (Button) findViewById(R.id.saveinformation);
        String pa = activitytext.getSelectedItem().toString();
         @Override
        public void onClick(View v) {

            int weight = (int) Float.parseFloat(weighttext.getText()
                    .toString());
            int height = (int) Float.parseFloat(heighttext.getText()
                    .toString());
            String username = usernametext.getText().toString();
            int age = (int) Float.parseFloat(agetext.getText().toString());
            TextView genderchoosed = (TextView) findViewById(genderselected
                    .getCheckedRadioButtonId());
            String gender = genderchoosed.getText().toString();
            String pa = activitytext.getSelectedItem().toString();

       //BMI==========================================================
            int Bmivalue = calculateBMI(weight, height);
            String bmiInterpretation = interpretBMI(Bmivalue);
            float idealweight = idealweight(weight, height, gender, pa, age);
            double dailycalories=dailycalories(weight,height,gender,pa,age);

         //insert data in to db===================================================
           Person person = new Person();
            person.setUsername(username);
            person.setHeight(height);
            person.setWeight(weight);
            person.setAge(age);
            person.setGender(gender);
            person.setPa(pa);
            person.setBmivalue(Bmivalue);
            person.setBmiInterpretation(bmiInterpretation);
            person.setIdealweight(idealweight);
            person.setDailycalories(dailycalories);
            Databasedata.DatabaseAdapter dbAdapter = new Databasedata.DatabaseAdapter(
                    MainActivity.this);
            dbAdapter.insertPerson(person);

            Toast.makeText(getApplicationContext(),
                    Bmivalue + "and you are" + bmiInterpretation,
                    Toast.LENGTH_LONG).show();

           }
         });
        }
        //BMI FUNCTION===============================================
        private int calculateBMI(int weight, int height) {

           return (int) (weight / (height * height));
        };

        private String interpretBMI(int Bmivalue) {

         if (Bmivalue < 18.5) {
          return "Underweight";
         } else if (Bmivalue < 25) {
           return "Normal";
         } else if (Bmivalue < 30) {
           return "Overweight";
         } else {
           return "Obese";
         }
        }
         //IDEAL WEIGHT========================================

private float idealweight(int weight, int height, String gender, String pa,
        int age) {
    float x = (float) 0.0;
    int bmi;

    bmi = idealbmi(age);
    x = bmi * (height * height);
    return x;

  }

   public int idealbmi(int age) {
    int bmi = 0;
    if (age > 17 && age <= 19) {
        bmi = 21;
    }
    if (age > 19 && age <= 24) {
        bmi = 22;
    }
    if (age > 24 && age <= 34) {
        bmi = 23;
    }
    if (age > 34 && age <= 44) {
        bmi = 24;
    }
    if (age > 44 && age <= 54) {
        bmi = 25;
    }
    if (age > 54 && age <= 64) {
        bmi = 26;
    }
    if (age > 64) {
        bmi = 27;
    }

    return bmi;
    }
    //DAILY CALORIES===============================================
    public double suitablepa(String pa) {
    double suitablepa = 0;
    if (pa.equals("Highly active")) {
        suitablepa =  1.48;
    }
    if (pa.equals("Active")) {
        suitablepa =  1.25;
    }
    if (pa.equals("Low activity")) {
        suitablepa =  1.11;
    }
    if (pa.equals("Sedentary")) {
        suitablepa =  1;
    }
    return suitablepa;

  }

  private double dailycalories(int weight, int height, String gender,
        String pa, int age) {
    double dailycalori = 0;
    double i;
    double j;
    double h;
    double k;
    if(gender.equals("femaleselected")){

        i=9.36*weight;
        j=726*height;
        h=suitablepa(pa)*(i+j);
        k=6.91*age;
        dailycalori=354-k+h;

    }else if(gender.equals("maleselected")){

        i=15.91*weight;
        j=539.6*height;
        h=suitablepa(pa)*(i+j);
        k=9.53*age;
        dailycalori=662-k+h;
    }


    return dailycalori;
}
}
package com.dietandroidproject;
导入数据库数据;
导入android.app.Activity;
导入android.os.Bundle;
导入android.view.view;
导入android.widget.Button;
导入android.widget.EditText;
导入android.widget.RadioButton;
导入android.widget.RadioGroup;
导入android.widget.Spinner;
导入android.widget.TextView;
导入android.widget.Toast;
公共类MainActivity扩展了活动{
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
最终放射组性别选择=(放射组)findViewById(R.id.selectgender);
genderselected.setOnCheckedChangeListener(新的RadioGroup.OnCheckedChangeListener(){
@凌驾
检查更改后的公共无效(放射组arg0,int-selectedId){
selectedId=genderselected.getCheckedRadioButtonId();
RadioButton genderchoosed=(RadioButton)findViewById(selectedId);
字符串gender=genderchoosed.getText().toString();
}
});
按钮保存信息=(按钮)findViewById(R.id.saveinformation);
saveinformation.setOnClickListener(新视图.OnClickListener(){
EditText-weighttext=(EditText)findViewById(R.id.weighttext);
EditText heighttext=(EditText)findViewById(R.id.heighttext);
EditText usernametext=(EditText)findViewById(R.id.usernametext);
EditText agetext=(EditText)findViewById(R.id.agetext);
微调器活动文本=(微调器)findViewById(R.id.chooseactivity);
按钮保存信息=(按钮)findViewById(R.id.saveinformation);
字符串pa=activitytext.getSelectedItem().toString();
@凌驾
公共void onClick(视图v){
int-weight=(int)Float.parseFloat(weighttext.getText()
.toString());
int height=(int)Float.parseFloat(heighttext.getText())
.toString());
字符串username=usernametext.getText().toString();
int age=(int)Float.parseFloat(agetext.getText().toString());
TextView genderchoosed=(TextView)findViewById(genderselected
.getCheckedRadioButtonId());
字符串gender=genderchoosed.getText().toString();
字符串pa=activitytext.getSelectedItem().toString();
//体重指数==========================================================
int BMI值=计算BMI(体重、身高);
字符串bmi解释=解释bmi(bmi值);
浮动理想体重=理想体重(体重、身高、性别、pa、年龄);
双倍奶量=奶量(体重、身高、性别、pa、年龄);
//在数据库中插入数据===================================================
Person=新人();
person.setUsername(用户名);
人。设置高度(高度);
人。设定重量(重量);
人.体位(年龄);
person.setGender(性别);
人.setPa(pa);
个人价值(Bmivalue);
个人解释(BMI解释);
人.设定理想体重(理想体重);
个人。setDailycalories(dailycalories);
Databasedata.DatabaseAdapter dbAdapter=新建Databasedata.DatabaseAdapter(
主要活动(本);
dbAdapter.insertPerson(person);
Toast.makeText(getApplicationContext(),
Bmivalue+,而您是“+Bmivalue”,
Toast.LENGTH_LONG).show();
}
});
}
//体重指数函数===============================================
专用整数计算器BMI(整数重量、整数高度){
返回值(整数)(重量/(高度*高度));
};
专用字符串解释BMI(整数BMI值){
如果(BMI值<18.5){
返回“减持”;
}否则如果(BMI值<25){
返回“正常”;
}否则如果(BMI值<30){
返回“超重”;
}否则{
返回“肥胖”;
}
}
//理想重量========================================
私人浮动理想重量(整数重量、整数高度、字符串性别、字符串pa、,
智力(年龄){
浮动x=(浮动)0.0;
体重指数;
bmi=理想bmi(年龄);
x=体重指数*(身高*身高);
返回x;
}
公共国际理想BMI(国际年龄){
int bmi=0;
如果(年龄>17岁和19岁和24岁和34岁和44岁和54岁和64岁){
体重指数=27;
}
返回体重指数;
}
//每日卡路里===============================================
公共双适配pa(字符串pa){
双适配PA=0;
如果(pa.equals(“高活性”)){
合适的PA=1.48;
}
如果(pa等于(“有效”)){
合适的PA=1.25;
}
如果(pa等于(“低活性”)){
合适的PA=1.11;
}
如果(pa.equals(“久坐”)){
合适的PA=1;
}
返回合适的PA;
}
私人双人日积月累(整数重量、整数高度、字符串性别、,
字符串(pa,int年龄){
双倍dailycalori=0;
双i;
双j;
双h;
双k;
if(性别平等(“女性当选”)){
i=9.36*重量;
j=726*高度;
h=合适的pa(pa)*(i+j);
k=6.91*年龄;
dailycalori=354-k+h;
}else if(性别平等(“男性选择”)){
i=15.91*重量;
j=539.6*高度;
h=合适的pa(pa)*(i+j);
k=9.53*年龄;
dailycalori=662-k+h;
}
返回dailycalori;
}
}
XML代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/backgroundmain"
android:orientation="vertical" >

<RelativeLayout
    android:id="@+id/personinformation"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1.98" >

    <EditText
        android:id="@+id/heighttext"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/usernametext"
        android:layout_below="@+id/usernametext"
        android:ems="10"
        android:hint="Enter Your Height" >
    </EditText>

    <EditText
        android:id="@+id/usernametext"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:ems="10"
        android:hint="Enter Username" />

    <EditText
        android:id="@+id/weighttext"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/heighttext"
        android:layout_below="@+id/heighttext"
        android:ems="10"
        android:hint="Enter Your Weight" />

    <EditText
        android:id="@+id/agetext"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/weighttext"
        android:layout_below="@+id/weighttext"
        android:ems="10"
        android:hint="Enter Your Age" >

        <requestFocus />
    </EditText>

</RelativeLayout>

<View
    android:layout_width="250dp"
    android:layout_height="1dip"
    android:layout_gravity="center"
    android:layout_marginTop="20dp"
    android:background="#aaa" />

<RelativeLayout
    android:id="@+id/choosegender"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="0.78" >

    <TextView
        android:id="@+id/choosefemaleormale"
        android:layout_width="match_parent"
        android:layout_height="30dip"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="10dip"
        android:gravity="center"
        android:text="Gender : "
        android:textAlignment="center"
        android:textColor="#555"
        android:textSize="19sp" />

    <RadioGroup
        android:id="@+id/selectgender"
        android:layout_width="220dip"
        android:layout_height="wrap_content"
        android:layout_below="@+id/choosefemaleormale"
        android:layout_centerHorizontal="true"
        android:orientation="horizontal" >

        <RadioButton
            android:id="@+id/femaleselected"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="left"
            android:layout_weight="1"
            android:checked="true"
            android:text="female"
            android:onClick="onRadioButtonClicked"
             />

        <RadioButton
            android:id="@+id/maleselected"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:layout_weight="1"
            android:text="male"
            android:onClick="onRadioButtonClicked" />
    </RadioGroup>
</RelativeLayout>
    <View
    android:layout_width="250dp"
    android:layout_height="1dip"
    android:layout_gravity="center"
    android:layout_marginTop="20dp"
    android:background="#aaa" />
<RelativeLayout
    android:id="@+id/choosepa"
    android:layout_width="250dip"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:layout_gravity="center" >

    <Spinner
        android:id="@+id/chooseactivity"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:entries="@array/activityitems"
        android:gravity="center"
        android:prompt="@string/level_of_activity" />

</RelativeLayout>

<Button
    android:layout_width="90dp"
    android:layout_height="0dp"
    android:layout_gravity="right"
    android:layout_marginBottom="10dip"
    android:layout_marginRight="20dp"
    android:layout_weight="0.46"
    android:background="@drawable/recent_foods_depressed"
    android:hint="save"
    android:text="save"
    android:textColor="#fff"
    android:textSize="20sp"
    android:textStyle="bold" 
    android:onClick="saveinformation"
    android:id="@+id/saveinformation"/>

 </LinearLayout>
 radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
    {
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            // checkedId is the RadioButton selected
            RadioButton rb=(RadioButton)findViewById(checkedId);
            Toast.makeText(getApplicationContext(), rb.getText(), Toast.LENGTH_SHORT).show();
        }
    });
RadioGroup yourRadioGroup = (RadioGroup) findViewById(R.id.radioGroup1);
        yourRadioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener()
        {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId)
            {
                switch(checkedId)
                {
                case R.id.radio0:
                    // TODO Something
                    break;
                case R.id.radio1:
                    // TODO Something
                    break;
                case R.id.radio2:
                    // TODO Something
                    break;
                }
            }
        });
yourRadioGroupName.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
        {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId)
            {
                int idRadioButtonChosen = yourRadioGroupName.getCheckedRadioButtonId();

                if(idRadioButtonChosen > 0){

                    radioButtonChosen = (RadioButton) findViewById(idRadioButtonChosen);
                    textViewOnclick.setText(radioButtonChosen.getText());

                }


            }
        });
  radioGroup.setOnCheckedChangeListener(RadioGroup.OnCheckedChangeListener { group, checkedId ->

          val radio:RadioButton = group.findViewById(checkedId)
          Log.e("selectedtext-->",radio.text.toString())

  })
radioGroup.setOnCheckedChangeListener(RadioGroup.OnCheckedChangeListener { group, checkedId ->

         var selectedId = rg.checkedRadioButtonId
         val radio:RadioButton = group.findViewById(selectedId)
            Log.e("selectedtext-->",radio.text.toString())

   })