android– 准备自定义的无线电组类型的布局

android– 准备自定义的无线电组类型的布局,第1张

概述我正在尝试在下图中准备自定义无线电组,如布局.我有将近8-10行.因此,我准备了一个具有水平方向的线性布局,并以编程方式添加了imageview,textview和radiobutton.因此,如果我检查一个单选按钮,其他单选按钮应自动取消选中.在进行该任务之前,我遇到了另一个问题,即如果我的单选按钮

我正在尝试在下图中准备自定义无线电组,如布局.我有将近8-10行.因此,我准备了一个具有水平方向的线性布局,并以编程方式添加了imagevIEw,textvIEw和radiobutton.

因此,如果我检查一个单选按钮,其他单选按钮应自动取消选中.在进行该任务之前,我遇到了另一个问题,即如果我的单选按钮被选中一次,那么虽然点击它们但单选按钮不可取消.以下是我的代码.

public class MainActivity extends Activity{Radiobutton[] radioBtns = new Radiobutton[10];    String texts[] = {"text1", "text2", .... "text10"};    @OverrIDe    protected voID onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentVIEw(R.layout.main);        entireLayout = (linearLayout)findVIEwByID(R.ID.main_layout);        for(int i =0; i<10; i++)        {            linearLayout rowLayout=new linearLayout(this);            radioBtns[i] = new Radiobutton(this);            radioBtns[i].setID(i);            radioBtns[i].setonCheckedchangelistener(cbListener);            ImageVIEw imageVIEw = new ImageVIEw(this);            TextVIEw tv = new TextVIEw(this);            tv.setText(texts[i]);            rowLayout.addVIEw(imageVIEw);            rowLayout.addVIEw(tv);                        rowLayout.addVIEw(radioBtns[i]);            entireLayout.addVIEw(rowLayout);                VIEw line = new VIEw(this);                 line.setBackgroundcolor(getResources().getcolor(R.color.horizontallinecolor));                entireLayout.addVIEw(line, new VIEwGroup.LayoutParams( VIEwGroup.LayoutParams.MATCH_PARENT, 1));        }         }Compoundbutton.OnCheckedchangelistener cbListener = new Compoundbutton.OnCheckedchangelistener()    {      public voID onCheckedChanged(Compoundbutton checkedbutton, boolean paramAnonymousBoolean)      {        switch (checkedbutton.getID())        {        case 0:          Log.d("tag", "checked 0th position");          break;          ..........................          ..........................        }      }     };}

我通过记录日志来观察.控件在第一次进入onCheckedChanged()时会进行检查,但是在未选中时则不会.我想知道那些是如何不可检查的.

或者根据我的要求,我还有一个想法来准备这个布局,就像我为xml中的行准备一个更多的布局.然后膨胀视图10次.但是,如何只检查一个单选按钮,以便其他选定的单选按钮取消选中.有人可以建议我如何通过最好的方式实现这种放射组?

注意:我保留了上面的代码,因为它显示了我尝试过的内容.如果这是完全错误的方式,请放轻松,请建议我如何完成.

解决方法:

根据我的经验,使用这些定制的RadioGroups有点麻烦.我已经为你准备了一些代码,这些代码将是我解决你想要完成的事情的方法.希望这对你有用!

首先,您必须在onCreate()(或创建视图的任何位置)中调用此函数

 private voID addRadiobuttons() {    linearLayout llGroup = (linearLayout) findVIEwByID(R.ID.linearLayoutGroup);    for(int i=0; i<10; i++){        MyRadiobutton mrb = new MyRadiobutton(this);        mrb.setText(String.valueOf(i));        llGroup.addVIEw(mrb.getVIEw());    }}

这堂课应该是

private static class MyRadiobutton implements VIEw.OnClickListener{    private ImageVIEw iv;    private TextVIEw tv;    private Radiobutton rb;    private VIEw vIEw;    public MyRadiobutton(Context context) {        vIEw = VIEw.inflate(context, R.layout.my_radio_button, null);        rb = (Radiobutton) vIEw.findVIEwByID(R.ID.radiobutton1);        tv = (TextVIEw) vIEw.findVIEwByID(R.ID.textVIEw1);        iv = (ImageVIEw) vIEw.findVIEwByID(R.ID.imageVIEw1);        vIEw.setonClickListener(this);        rb.setonCheckedchangelistener(null);    }    public VIEw getVIEw() {        return vIEw;    }    @OverrIDe    public voID onClick(VIEw v) {        boolean nextState = !rb.isChecked();        linearLayout lGroup = (linearLayout)vIEw.getParent();        if(lGroup != null){            int child = lGroup.getChildCount();            for(int i=0; i<child; i++){                //uncheck all                ((Radiobutton)lGroup.getChildAt(i).findVIEwByID(R.ID.radiobutton1)).setChecked(false);            }        }        rb.setChecked(nextState);    }    public voID setimage(Bitmap b){        iv.setimageBitmap(b);    }    public voID setText(String text){        tv.setText(text);    }    public voID setChecked(boolean isChecked){        rb.setChecked(isChecked);    }}

并且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="wrap_content"    androID:gravity="center_vertical"    androID:orIEntation="horizontal" >    <ImageVIEw        androID:ID="@+ID/imageVIEw1"        androID:layout_wIDth="wrap_content"        androID:layout_height="wrap_content"        androID:src="@drawable/ic_launcher" />    <TextVIEw        androID:ID="@+ID/textVIEw1"        androID:layout_wIDth="match_parent"        androID:layout_height="wrap_content"        androID:layout_weight="1"        androID:text="Medium Text"        androID:textAppearance="?androID:attr/textAppearanceMedium" />    <Radiobutton        androID:ID="@+ID/radiobutton1"        androID:layout_wIDth="wrap_content"        androID:layout_height="wrap_content" /></linearLayout>
总结

以上是内存溢出为你收集整理的android – 准备自定义的无线电组类型的布局全部内容,希望文章能够帮你解决android – 准备自定义的无线电组类型的布局所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1098393.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存