2010-07-09 42 views

Trả lời

2

Ở đây bạn có giải pháp làm việc. Không phải của tôi, nhưng tôi đã sử dụng kỹ thuật mô tả và nó hoạt động. Đừng quên về img.setBounds(), điều đó là cần thiết!

http://mgmblog.com/2010/06/10/arrayadapter-and-alertdialog-for-single-choice-items/

PS: ban đầu bài không hiển thị radiobutton, nhưng cách bố trí của tôi không. Tệp bố cục XML hàng đơn của tôi (ví dụ animal_row.xml bị thiếu) là ở đây:

<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

tuyệt vời, cảm ơn. –

2

Bạn có thể ghi đè mọi thứ và tạo từ đầu. Trước tiên, hãy tạo xml mô tả nội dung bạn muốn. Ví dụ:

<!-- 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> 

Sau đó, tạo xml này bằng hộp thoại cảnh báo. Bạn có thể cho phép hoạt động xử lý hộp thoại của mình và chỉ ghi đè lên onCreateDialog như sau:

@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(); 
} 

Sau đó bạn có thể ghi đè lênNhấp để lưu trữ trạng thái trong tệp StaticPreferences của bạn. Bằng cách này, bạn sẽ có toàn quyền kiểm soát tất cả các yếu tố trong chế độ xem của mình.

Một lời khuyên cuối cùng, khi tạo xml đảm bảo rằng bạn tạo bố cục bằng phẳng nhất có thể để mã được tối ưu hóa. Bạn cũng có thể chơi với bố cục danh sách để tạo ra một cái gì đó như thế này.

+0

Câu trả lời sai, trong bố cục câu hỏi của tôi, trong bố cục xml bạn đang tạo bố cục con phụ thuộc bố cục tương đối, tạo nút radio trẻ em khác ngoài radiogroup không hoạt động. Trong trường hợp này, câu trả lời của bạn không thành công. –

+0

@Faisal khan: Anh ấy chỉ đưa ra một ví dụ. – Macarse

0

Tôi đã không làm việc trên đó. Nhưng tôi có một ý tưởng cho bạn đã kết hợp ba ví dụ về trình diễn API.

  1. AlertDialogs
  2. SingleChoice Adapter
  3. Efficient Adapter

các bước bạn phải làm là:

  • Tạo AlertDialog
  • Tạo danh sách Adaptor Class cho quan điểm bạn muốn Hoàn thành.
  • Sử dụng phương pháp SetAdapter để đặt ListAdapter.

Mã mẫu:

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(); 

Hy vọng Nó hoạt động.

+0

không hoạt động như thế nào chúng tôi có nút radio hoạt động trong một nhóm với inflat –

Các vấn đề liên quan