gpt4 book ai didi

android - 准备自定义 radio 组类型的布局

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:25:56 25 4
gpt4 key购买 nike

我正在尝试准备自定义 radiogroup 如下图中的布局。我有近 8-10 行可以做到这一点。因此,我准备了一个水平方向的线性布局,并以编程方式添加了imageviewtextviewradiobutton

enter image description here

因此,如果我选中一个 radio button,其他单选按钮应该会自动取消选中。在执行该任务本身之前,我遇到了另一个问题,如果我的单选按钮被选中一次,那么尽管单击了单选按钮,但单选按钮并不是不可选中的。下面是我的代码。

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(),但在未选中时则不会。我想知道这些是如何无法检查的。

OR 根据我的要求,我有一个准备此布局的想法,就像我为 xml 中的行 准备一个布局一样。然后将 View 充气 10 次。但是,我怎样才能只选中一个单选按钮,以便取消选中其他选定的单选按钮。有人可以建议我如何通过最佳方式实现这种radiogroup吗?

注意:我保留上面的代码是为了展示我的尝试。如果这是完全错误的方式,请放轻松,并建议我如何完成。

最佳答案

根据我的经验,使用这些自定义的 RadioGroups 有点麻烦。我已经为您准备了一些代码,这些代码将是我解决您要完成的任务的方法。希望它对你有用!

首先你必须在你的 onCreate() 中调用这个函数(或者你创建 View 的任何地方)

 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 - 准备自定义 radio 组类型的布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20456322/

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