Android:RadioGroup.OnCheckedChangeListener无法在我的应用中运行

时间:2013-02-11 06:00:19

标签: android radio-button radio-group

我正在使用此Github项目https://github.com/ysamlan/horizontalpager来使用水平分页器 。这工作正常并执行水平交换。但在我的项目中,我动态添加了布局,按钮和单选按钮。当静态放置时,单选按钮会在水平分页上更改其状态,但我已将它们动态放置,因此RadioGroup.OnCheckedChangeListener无法正常工作。

我的主要课程是

package com.PointofSale;

import java.util.List;
import database.in.*;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.view.ViewPager.LayoutParams;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.DigitalClock;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;

public class SalesActivity extends Activity{
    private HorizontalPager mPager;
    private RadioGroup mRadioGroup;
    com.PointofSale.HorizontalPager horizontalPager;
    ProductDbHandler prodDbHand;
    Cursor mCursor;
    RelativeLayout deptSysKeys;
    RadioButton[] rb;
    int number_Of_Pages;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sale_layout);
        DigitalClock dc = (DigitalClock) findViewById(R.id.digitalClock1);
        prodDbHand=new ProductDbHandler(this);
        mPager=(HorizontalPager)findViewById(R.id.horizontal_pager);
        mRadioGroup=(RadioGroup)findViewById(R.id.radioGroup);
        deptSysKeys=(RelativeLayout)findViewById(R.id.DeptKeyslayout);
        List<GetDeptInfo> getDept = prodDbHand.getAllDept();  
        int deptSize=getDept.size();
        number_Of_Pages=(deptSize/5+1);
        rb = new RadioButton[number_Of_Pages];
        ((ViewGroup)mRadioGroup.getParent()).removeView(mRadioGroup);
        for(int i=0; i<number_Of_Pages; i++){
            rb[i]  = new RadioButton(this);
            //rb[1].setChecked(true);
            rb[i].setId(i);
            mRadioGroup.addView(rb[i]); //the RadioButtons are added to the radioGroup instead of the layout
        }
        rb[0].setChecked(true);
        deptSysKeys.addView(mRadioGroup);//you add the whole RadioGroup to the layout
        final LinearLayout[] keysLayout=new LinearLayout[number_Of_Pages];
        com.PointofSale.HorizontalPager.LayoutParams llp = new  com.PointofSale.HorizontalPager.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
        Button[] deptBtn=new Button[deptSize];
        int[] DeptId=new int[deptSize];
        String[] DeptName=new String[deptSize];
        int k=0;
        int a=0;
        for(int i=0;i<number_Of_Pages;i++){
            keysLayout[i]=new LinearLayout(this);
            keysLayout[i].setOrientation(LinearLayout.HORIZONTAL);
            keysLayout[i].setId(i);
            System.out.println("LinerLayout Created:"+i);
            keysLayout[i].setLayoutParams(llp);
            for(int j=a;j<deptSize;j++){
                deptBtn[j]=new Button(this);
                deptBtn[j].setText(getDept.get(j).getName());
                System.out.println("Departments:"+getDept.get(j).getName());
                deptBtn[j].setId(getDept.get(j).getID());
                keysLayout[i].addView(deptBtn[j]);
                k++;
                if(k==5){
                    break;
                }
            }
            k=0;
            a=a+5;
            mPager.addView(keysLayout[i]);
        } 
    } 
    private final HorizontalPager.OnScreenSwitchListener onScreenSwitchListener =
            new HorizontalPager.OnScreenSwitchListener() {
                @Override
                public void onScreenSwitched(final int screen) {
                    // Check the appropriate button when the user swipes screens.
                    /*switch (screen) {

                        case 0:
                            mPager.setCurrentScreen(0, true);
                           // mRadioGroup.check(R.id.rg[0]);
                            break;
                        case 1:
                            mPager.setCurrentScreen(1, true);
                          // mRadioGroup.check(R.id.radio_btn_1);
                            break;
                        case 2:
                            mPager.setCurrentScreen(2, true);
                            //mRadioGroup.check(R.id.radio_btn_2);
                            break;
                        default:
                            break;
                    }*/

                    for(int i=0; i<number_Of_Pages;i++){
                        if(screen==i){
                            mPager.setCurrentScreen(i, true);
                            //RadioButton btn = (RadioButton) mRadioGroup.getChildAt(i);
                            mRadioGroup.check(rb[i].getId());
                            System.out.println("Radio Button ID:"+rb[i].getId());
                        }
                    }
                }
            };

          private final RadioGroup.OnCheckedChangeListener onCheckedChangedListener =
            new RadioGroup.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(final RadioGroup group, final int checkedId) {
                    // Slide to the appropriate screen when the user checks a button.
                     /*switch (checkedId) {
                        case btn.getId():
                            mPager.setCurrentScreen(0, true);
                            break;
                        case btn.getId():
                            mPager.setCurrentScreen(1, true);
                            break;
                        case btn.getId():
                            mPager.setCurrentScreen(2, true);
                            break;
                        default:
                            break;
                    }*/
                    for(int i=0; i<number_Of_Pages;i++){
                        if(checkedId==i){
                            mPager.setCurrentScreen(i, true);

                        }
                    }

                }
            };


}

上面评论了用于更改单选按钮状态的代码静态代码。建议我在哪里错了,任何帮助都是适当的。

2 个答案:

答案 0 :(得分:6)

试试这个:

RadioGroup rdGroup = (RadioGroup) findViewById(R.id.rdbGp1);
            rdGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(RadioGroup group, int checkedId) {
                    // TODO Auto-generated method stub

                }
            });

答案 1 :(得分:4)

您不能在setOnCheckedChangeListener对象

上致电RadioButton
 for(int i=0; i<number_Of_Pages; i++){
        rb[i]  = new RadioButton(this);           
        rb[i].setId(i);

        // Add this line to register your onCheckedChangedListener handler
        rb[i].setOnCheckedChangeListener(onCheckedChangedListener);
        mRadioGroup.addView(rb[i]); //the RadioButtons are added to the radioGroup instead of the layout
   }