Preparing customized radio group type of layout(准备自定义无线电组类型的布局)
问题描述
我正在尝试在下图中准备自定义 radiogroup 布局.我有将近 8-10 行来做这件事.所以,我准备了一个水平方向的 linear layout
,并以编程方式添加了 imageview
、textview
和 radiobutton
.
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.
因此,如果我选中一个 单选按钮
,其他单选按钮应该会自动取消选中.在执行该任务之前,我遇到了另一个问题,如果我的单选按钮被选中一次,那么即使单击单选按钮也不是不可选中的.下面是我的代码.
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(),但在未选中时则不会.我想知道这些是如何无法检查的.
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.
或根据我的要求,我还有一个想法来准备这个布局,就像我为 xml 中的行准备一个布局一样.然后将视图膨胀 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?
注意:我保留上面的代码是为了展示我尝试过的内容.如果这是完全错误的方式,请放轻松,请建议我如何完成.
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.
推荐答案
根据我的经验,使用这些定制的 RadioGroup 有点麻烦.我已经为您准备了一些代码,这将是我解决您想要完成的事情的方法.希望它对你有用!
In my experience is a little of a troublemaker to work with these kind of customized RadioGroups. I've prepared you some code that would be my approach to solve what you're trying to accomplish. Hope it works for you!
首先你必须在你的 onCreate() 中调用这个函数(或者在你创建视图的任何地方)
First you must call this function in your onCreate() (or wherever you're creating the views)
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,类似于:
And the xml to inflate, something like:
<?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>
这篇关于准备自定义无线电组类型的布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:准备自定义无线电组类型的布局
基础教程推荐
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01