12-25:工作需求 需要做带多选功能的diglog--实现
//损益时间diglog
private void lossTimeDigLog(List<String> years){
AlertDialog.Builder builder = new AlertDialog.Builder(ExpenditureDetailAct.this);
LayoutInflater inflater = LayoutInflater.from(ExpenditureDetailAct.this);
View v = inflater.inflate(R.layout.losstime_dig, null);
final LossTimesAdap adap = new LossTimesAdap(ExpenditureDetailAct.this,years);
TextView queding = v.findViewById(R.id.queding);
TextView quxiao = v.findViewById(R.id.quxiao);
ListView lv1 = v.findViewById(R.id.lv1);
lv1.setDivider(null);
lv1.setAdapter(adap);
final Dialog dialog = builder.create();
dialog.show();
dialog.getWindow().setContentView(v);//自定义布局应该在这里添加,要在dialog.show()的后面
queding.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
List<String> list = adap.getMap();
String tt="";
for(String ii:list){
tt=tt+ii+",";
}
if(tt.endsWith(",")){
tt=tt.substring(0,tt.length()-1);
}
times = list;
sunyiyear_tv.setText(tt);
dialog.dismiss();
}
});
quxiao.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dialog.dismiss();
}
});
}
adapter
public class LossTimesAdap extends BaseAdapter {
private Context context;
private List<String> times;
HashMap<String,Boolean> map=new HashMap<>();
public LossTimesAdap(Context context, List<String> times) {
this.context = context;
this.times = times;
for (int i=0;i<times.size();i++){
map.put(times.get(i),false);
}
}
@Override
public int getCount() {
return times.size();
}
@Override
public Object getItem(int i) {
return times.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
static class ViewHolder{
public TextView t01;
public CheckBox cc;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
final String s = times.get(i);
LossTimesAdap.ViewHolder ViewHolder=null;
if(view==null){
ViewHolder=new LossTimesAdap.ViewHolder();
view= LayoutInflater.from(context).inflate(R.layout.item_lossset_diglog,viewGroup,false);
ViewHolder.t01=view.findViewById(R.id.t01);
ViewHolder.cc=view.findViewById(R.id.fufub);
view.setTag(ViewHolder);
AutoUtils.autoSize(view);
}else {
ViewHolder = (LossTimesAdap.ViewHolder) view.getTag();
}
ViewHolder.t01.setText(s.toString());
final LossTimesAdap.ViewHolder finalViewHolder = ViewHolder;
ViewHolder.cc.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if(b){
finalViewHolder.cc.setChecked(true);
map.put(s,true);
}else {
finalViewHolder.cc.setChecked(false);
map.put(s,false);
}
}
});
if(map.get(s)){
ViewHolder.cc.setChecked(true);
}else {
ViewHolder.cc.setChecked(false);
}
return view;
}
public List<String> getMap(){
List<String> list=new ArrayList<>();
Set<Map.Entry<String, Boolean>> entries = map.entrySet();
for(Map.Entry<String, Boolean> ss:entries){
if(ss.getValue()==true){
list.add(ss.getKey());
}
}
return list;
}
}
diglog布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#ffffff"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/chaosongban"
android:textSize="28px"
android:textColor="#333333"
android:text="请选择损益时间"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ListView
android:id="@+id/lv1"
android:layout_below="@id/chaosongban"
android:layout_width="400px"
android:layout_height="300px"></ListView>
<TextView
android:id="@+id/queding"
android:text="确定"
android:layout_marginTop="50px"
android:layout_marginLeft="340px"
android:textSize="28px"
android:layout_below="@id/lv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/quxiao"
android:text="取消"
android:layout_marginTop="50px"
android:layout_marginLeft="250px"
android:textSize="28px"
android:layout_below="@id/lv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
adapter布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_marginBottom="15px"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="40px">
<CheckBox
android:layout_centerVertical="true"
android:layout_below="@id/fufu"
android:button="@null"
android:id="@+id/fufub"
android:layout_width="30px"
android:layout_marginLeft="10px"
android:layout_height="30px"
android:background="@drawable/checkstyle"
/>
<TextView
android:layout_marginLeft="10px"
android:layout_toRightOf="@id/fufub"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:id="@+id/t01"
android:text="111"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
</LinearLayout>
来源:https://blog.csdn.net/shi779276212/article/details/85246783