gpt4 book ai didi

Android:RadioGroup.OnCheckedChangeListener 在我的应用程序中不起作用

转载 作者:行者123 更新时间:2023-11-29 14:25:09 26 4
gpt4 key购买 nike

我正在努力通过使用这个 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);

}
}

}
};


}

更改单选按钮状态的代码静态代码在上面有注释。建议我一些我错了的地方,并提供任何帮助。

最佳答案

您没有在 RadioButton 对象上调用 setOnCheckedChangeListener

 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
}

关于Android:RadioGroup.OnCheckedChangeListener 在我的应用程序中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14806711/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com