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