2011-09-28 38 views
0

我需要在ListView中添加RadioButton,事實上我已經嘗試過了,但是我得到的問題是,當我單擊單選按鈕時,它會被選中得非常好......但同時如果不希望選擇那個特定的按鈕......它只是被選中。在這裏,我提供我的代碼:在ListView中添加radioBurton

import android.app.ListActivity; 
import android.graphics.Color; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.ArrayAdapter; 
import android.widget.ListView; 
import android.widget.Toast; 

public class RadioInCustomListView extends ListActivity { 

    ListView lv; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 


     String[] names = new String[] { "Issues", "Request for Information", 
        "Contracts", "Purchase Orders", "Change Orders", "Proposals", 
        "Submittals" }; 
      lv = getListView(); 
      lv.setCacheColorHint(Color.TRANSPARENT); 
      ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.radio_item, names); 
      //this.setListAdapter (new ArrayAdapter<String>(RadioInCustomListView.this,R.layout.radio_item, names)); 
      setListAdapter(adapter); 
    } 

     protected void onListItemClick(ListView l, View v, int position, long id) { 
      super.onListItemClick(l, v, position, id); 
      Object o = this.getListAdapter().getItem(position); 
      Toast.makeText(RadioInCustomListView.this, o.toString(), Toast.LENGTH_LONG).show(); 


      } 
    } 

我個XML: 的main.xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 
<ListView 
    android:id="@+android:id/list" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    /> 
</LinearLayout> 

radio_item.xml

<?xml version="1.0" encoding="utf-8"?> 
<RadioButton xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/text1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:textAppearance="?android:attr/textAppearanceLarge" 
    android:gravity="center_vertical" 
    android:checkMark="?android:attr/listChoiceIndicatorSingle" 
    android:paddingLeft="6dip" 
    android:paddingRight="6dip" 
/> 
+0

讓我們看看這個帖子。有類似的問題[點擊這裏] [1] [1]:http://stackoverflow.com/questions/4250599/android-listview-with-radiobutton-in-singlechoice-mode-and-一個自定義的行佈局 – sandy

回答

2

可能是這個代碼幫助您

ls.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_single_choice,list_sexuality)); 
ls.setChoiceMode(ListView.CHOICE_MODE_SINGLE); 
+1

謝謝你的友善迴應,是啊我可以做到這一點,但我需要創建自己的佈局....有沒有什麼辦法來自定義佈局(layout.simple_list_item_single_choice) –