安卓图像单选

6
3个回答

2

您可以覆盖所有内容并从头开始创建。首先创建一个描述所需内容的xml。例如:

<!-- test.xml -->
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
 android:background="@color/white">
 <ImageView android:id="@+id/ImageView01"
  android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
  android:src="@drawable/facebook"></ImageView>
 <TextView android:id="@+id/TextView01" 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" 
            android:layout_toRightOf="@+id/ImageView01"
  android:text="Facebook" 
            android:textSize="40sp"></TextView>
 <RadioButton android:id="@+id/RadioButton01"
  android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
  android:layout_toRightOf="@+id/TextView01"></RadioButton>
</RelativeLayout>

然后使用警告对话框构建此xml。您可以让活动处理对话框,只需像这样覆盖onCreateDialog:

@Override
protected Dialog onCreateDialog(int id) {
    View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.test, null);
    return new AlertDialog.Builder(MainActivity.this).setView(view).setTitle("Share")
                    .create();
}

您可以重写onClick方法,将状态存储在StaticPreferences文件中。这样,您就可以完全控制视图中的所有元素。

最后一个建议是,在创建xml时,请尽可能创建平面布局,以便代码得到优化。您还可以调整列表布局,创建类似于此示例的内容。


错误答案,在我的问题布局中提到,在您的xml布局中,您正在将radiobutton作为相对布局的子项,将radiobutton作为radiogroup之外的其他子项不起作用。在这种情况下,您的答案失败了。 - d-man
@Faisal Khan:他只是举了一个例子。 - Macarse

2
在这里,您可以找到有效的解决方案。虽然不是我的,但我使用了所描述的技术,并且它起作用了。不要忘记img.setBounds(),这是必要的!
原始帖子没有显示单选按钮,但我的布局显示了。我的单行 XML 布局文件(示例中缺少 animal_row.xml)在这里: http://mgmblog.com/2010/06/10/arrayadapter-and-alertdialog-for-single-choice-items/ PS:请保留 HTML 标记。
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/miastorow_text"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center_vertical"
android:checkMark="?android:attr/listChoiceIndicatorSingle"
android:paddingLeft="6dip"
android:paddingRight="6dip"
android:textColor="#000"
/>

0

我没有亲自尝试过,但我有一个想法,你可以将API Demos中的三个示例结合起来。

  1. AlertDialogs
  2. SingleChoice Adapter
  3. Efficient Adapter

你需要执行以下步骤:

  • 创建AlertDialog
  • 为你想要实现的视图创建列表适配器类。
  • 使用SetAdapter方法设置ListAdapter。

示例代码:

new AlertDialog.Builder(MainActivity.this).setTitle("Share")setAdapter(new Listadapterclass(),new DialogInterface.OnClickListener(){

                            @Override
                            public void onClick(DialogInterface arg0,
                                    int arg1) {
                                // TODO Auto-generated method stub
        // the click event on the list item selected                        
                            }})
                .create();

希望它能够运行。

无法工作,我们如何让单选按钮在一个组中与充气同步。 - d-man

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接