布局preparing定制单选组类型 [英] Preparing customized radio group type of layout

查看:102
本文介绍了布局preparing定制单选组类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图prepare定制的 RadioGroup中像下面的图片布局。我有近8-10行这样做。所以,我prepared有一个线性布局有水平方向,并添加了的ImageView 的TextView 单选编程。

I am trying to prepare custom radiogroup like layout in the below image. I have nearly 8-10 rows to do that. So, I prepared one linear layout which has horizontal orientation and added the imageview, textview and radiobutton programatically.

所以,如果我在一个检查单选按钮,其他的单选按钮会自动选中。去之前那个任务本身,我得到了另外一个问题,如果我的单选按钮被检查一次,则单选按钮都没有uncheckable虽然点击它们。下面是我的code。

So if I check on one radio button, the other radio buttons should automatically unchecked. Before going to that task itself, I got another problem that if my radio buttons are checked once, then the radio button are not uncheckable though clicked on them. Below is my code.

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()作为第一次当那些越来越检查,而不是在未选中。我不知道那些是uncheckable。

I have observed through keeping logs. The control enters onCheckedChanged() for the first time when those are getting checked, but not when unchecked. I wonder how those are uncheckable.

根据我的要求,我还有一个想法,prepare此布局就像我在XML prepare多了一个布局在即可。然后膨胀的观点10倍。但随后也,我怎么能只检查一个单选按钮,这样其他选择一个得到选中。是否有人可以建议我如何实现这种 RadioGroup中通过最好的方式?

OR According to my requirement I have one more idea to prepare this layout like I prepare one more layout for the row in xml. Then inflating the view 10 times. But then also, how can I check only one radio button so that other selected one gets unchecked. Can someone please suggest me how to achieve this kind of radiogroup through the best way?

注意:我遵守了我超过code,因为显示什么我都试过了。如果是完全错误的方式,请去容易,请建议我如何弄完。

Note: I have kept my above code because to show what I have tried. If it is completely wrong way, please go easy and please suggest me how to get that done.

推荐答案

我编辑的版面删除的第二部分,它是无用你的目的。

如果你看一下布局,你会看到什么就是把右边的按钮的伎俩。

这可以在code制成,同样,如果你不想使用的布局(为什么?)

使用我的布局(无需一cusom类),你会得到的结果是...

I edited the layout removing the second part, which was useless for your purpose.
If you look at the layout, you will see what is the trick to put the button on the right.
This can be made in code, too, if you don't want to use the layout (why?)
Using my layout (without the need for a cusom class), the result you'd get is...

神似你的目标,不是吗?


最棘手的线,你会想在你的code复制(再次,为什么?)是:

Quite similar to your goal, isn't it?

The tricky lines you'll want to replicate in your code (again, why?) are:

android:button="@null"
android:drawableRight="@android:drawable/btn_radio" // or your selector, with custom on/off pngs
android:drawableLeft="@drawable/ic_launcher" // or whatever

这篇关于布局preparing定制单选组类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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