- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试准备自定义 radiogroup 如下图中的布局。我有近 8-10 行可以做到这一点。因此,我准备了一个水平方向的线性布局
,并以编程方式添加了imageview
、textview
和radiobutton
。
因此,如果我选中一个 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/
我对软件 radio 感兴趣。我基本上没有涉及任何电气工程或数学主题的经验,但我正在阅读...... 与其尝试处理天线和 ADC 的硬件方面,我想它会很有趣(而且更便宜)来播放一些预先录制的(甚至是实
我是 C++ 的新手,想了解一些东西。我的 main.cpp 中有这段代码: Radio r = Radio("PSR", 100.8); 或者那个代码: Radio r("PSR", 100.8);
我是 C++ 的新手,想了解一些东西。我的 main.cpp 中有这段代码: Radio r = Radio("PSR", 100.8); 或者那个代码: Radio r("PSR", 100.8);
如何将选定的 radio (从 radio 组)复制到另一个 radio ?这是我的jsfiddle:http://jsfiddle.net/fu3oyscq/1/ 这是我的jquery $("#co
我有这个表格: 1 X 2 1 2 我想实现这个: If a=1 then b=1 If a=X then unset b If a=2 then b=2 我尝试了这个,但没有任何作用: $
我为什么要为此苦苦挣扎? 我有一个值:5 如何查看值为 5 的组“mygroup”的单选按钮? $("input[name=mygroup]").val(5); // doesn't work? 最佳
正如标题所说例如: User1 User2 如何获取文本:用户 1 最佳答案 $('input:radio:checked').siblings('label:first').html() 更新:
无法在 Bootstrap radio 组中检查 radio 。只有 div 处于事件状态。 Link Bootstrap 单选组: Enabled Disa
如何禁用 radio 组中的一个 radio 输入? Option1 Option2 Option3 Option4 我的问题是我想在单击任何其他按钮后禁用选项1 例如: when i sele
我正在使用 jQuery 验证来验证其上设置了单选按钮的表单(在本例中设置 = 两个单选按钮)。带标签的单选按钮如下。该表单位于 jQueryUI 对话框上。 Yes No 我从 jquery.com
我有一个包含 26 个问题的表格,所有问题都有单选按钮组作为用户的答案选择(它计算给定属性(property)上特定入侵植物物种的风险因素)。最后,将每个 radio 组中的选定值相加。 让事情变得复
我创建了一个单选组列表并将它们附加到一个产品类别。因此,我正在做的是检查该类别应具有的组,然后选取该类别可用的这些组中的单选值并将其显示给用户进行填充。 接下来我想将它们传递给我的 ajax 函数,然
圆形边缘刚好从正方形或圆形中伸出,因此如果我在 before 之前提供白色背景,则 radio 边缘仍然略微可见。 input[type=radio] { box-sizing: border-b
我找到了一个纯 CSS 星级系统,但是对于给定的代码,我只能在每个页面上使用一个星级系统。我试过更改类(class)名称,但不幸的是,第二个星级评分表在按下时会检查第一个星级评分表。有什么办法解决这个
我有一个包含 radio 输入和标签的无序列表。当用户点击列表项时,它会切换一些信息并选择单选输入。 但是,由于上述原因,如果用户点击实际的 radio 输入本身,它不会选择它,因为我猜点击是针对 r
我想使用一些 radio 输入创建一个表单。检查输入时,还会使用 jQuery 自动检查另一个 radio 。 例如: 问题1:A(勾选),B,Cquestion2: D(auto checked),
如果我检查了名称为“most1”且值相同的单选按钮,如何禁用名称为“least1”的单选按钮 最佳答案 在
如何在选择第一个 radio 输入时选择所有复选框并在选择第二个 radio 输入时取消选中所有复选框? (Javascript)? 我在这里研究过以前的类似问题,但它们似乎都是关于使用复选框而不是单
Please, please check me Am I selected ? 我想要以下行为: 当 myCheckbox 被选中时,myRadio 将被选中。 当 myCheckbox
我知道无符号数永远不会小于 0,因此无需对其进行测试。但我想避免调谐器出现虚假频率。 /* freqKHz is in KHz (duh) */ void videoinput_set_tuner
我是一名优秀的程序员,十分优秀!