radio group setOnCheckedChangeListener [英] radio group setOnCheckedChangeListener

查看:143
本文介绍了radio group setOnCheckedChangeListener的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对收音机组有一些问题,我是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;


公共类MainActivity扩展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();

}
});

按钮saveinformation =(按钮)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(查看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(体重,身高);
String bmiInterpretation = interpretBMI(Bmivalue);
浮动理想体重=理想体重(体重,身高,性别,年龄,年龄);
每日双卡路里=每日卡路里(体重,身高,性别,年龄,年龄);

//将数据插入db =================================== ================
人员=新人();
person.setUsername(username);
person.setHeight(height);
person.setWeight(weight);
person.setAge(age);
person.setGender(性别);
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 +,你是+ bmiInterpretation,
Toast.LENGTH_LONG)。show();

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

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

private String interpretBMI(int Bmivalue){

if(Bmivalue< 18.5){
return减持;
}否则如果(Bmivalue< 25){
返回正常;
}否则如果(Bmivalue< 30){
返回超重;
}其他{
返回Obese;
}
}
//理想体重================================ ========

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 *(身高*身高);
返回x;

}

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

返回bmi;
}
//每日卡路里===================================== ==========
public double legitpa(String pa){
double legitpa = 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;
}
返回properpa;

}

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

i = 9.36 * weight;
j = 726 *身高;
h = properpa(pa)*(i + j);
k = 6.91 *年龄;
dailycalori = 354-k + h;

}否则if(gender.equals(maleselected)){

i = 15.91 * weight;
j = 539.6 *身高;
h = properpa(pa)*(i + j);
k = 9.53 *年龄;
dailycalori = 662-k + h;
}


return dailycalori;
}
}

XML CODES

 <?xml version =1.0encoding =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 =输入你的高度>
< / EditText>

< EditText
android:id =@ + id / usernametext
android:layout_width =wrap_content
android:layout_height =wrap_content
android:layout_alignParentTop =true
android:layout_centerHorizo​​ntal =true
android:layout_marginTop =20dp
android:ems =10
android:hint = 输入用户名/>

< 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 =输入你的重量/>

< 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 =输入你的年龄>

< requestFocus />
< / EditText>

< / RelativeLayout>

<查看
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 = 性别:
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_centerHorizo​​ntal =true
android:orientation =horizo​​ntal>

< 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>
<查看
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>

<按钮
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 =保存
android:text =save
android:textColor =#ffff
android:textSize =20sp
android:textStyle =bold
android:onClick =saveinformation
android:id =@ + id / saveinformation/>

< / LinearLayout>


解决方案

问题在于您的单选按钮试图引用主活动中的 onRadioButtonClicked 方法。但是你没有指定那个方法。请记住,如果您使用 android:onClick ,则不应在主要活动中使用 setOnCheckedChangeListener ,反之亦然。 / p>

I have some problem with radio group, I am new to Android development so I don't have much idea to how can handle it.This is form which has some input filed and radio group for choosing gender and save button. but I don't know where can I exactly put my radiogroup setoncheakchangelistener? so it gives me errors.

MAIN ACTIVITY :

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;
}
}

XML CODES

<?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>

解决方案

The thing is that your radio buttons trying to refer the onRadioButtonClicked method in your main activity. But you didnt specify that method. Remember, if you're using android:onClick you should not use setOnCheckedChangeListener in your Main activity and vice versa.

这篇关于radio group setOnCheckedChangeListener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆