CustomizeRadioActivity.java
package com.mw.app.view.activity;
import android.os.Bundle;
import android.widget.AbsListView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import com.mw.app.R;
import java.util.ArrayList;
public class CustomizeRadioActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.customize_radio_layout);
//初始化数据
ArrayList<String> listData = new ArrayList<>();
for (int i = 0; i < 13; i++) {
listData.add("单选"+i);
}
//初始化ListView
ListView listView = (ListView) findViewById(R.id.customize_radio_layout_listview);
//设置选择模式 通过listView设置单选模式
listView.setChoiceMode(AbsListView.CHOICE_MODE_SINGLE);
//创建adapter,布局加载系统默认的单选框 -- 想自己写ui,重写一个xml ui文件就OK
ArrayAdapter arrayAdapter = new ArrayAdapter(this,android.R.layout.simple_list_item_single_choice);
arrayAdapter.addAll(listData);
listView.setAdapter(arrayAdapter);
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="多选实现" />
<ListView
android:id="@+id/customize_radio_layout_listview"
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="wrap_content"></ListView>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="确定" />
</LinearLayout>