多选框清除上次选择
package com.imooc.dateandtimedialog; import android.app.DatePickerDialog; import android.content.DialogInterface; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.DatePicker; import android.widget.TextView; import android.widget.Toast; import java.util.Calendar; public class MainActivity extends AppCompatActivity implements View.OnClickListener{ private Button btn_setDate, btn_singleDialog, btn_multiDialog; private TextView tv_info; private AlertDialog singleListDlg, multiListDlg; private DatePickerDialog datePickerDialog; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); //初始化控件 } //初始化控件 private void initView(){ btn_setDate = (Button) findViewById(R.id.btn_setDate); btn_singleDialog = (Button) findViewById(R.id.btn_singleDialog); btn_multiDialog = (Button) findViewById(R.id.btn_multiDialog); tv_info = (TextView) findViewById(R.id.tv_info); initDateDialog(); initSingleListDialog(); initMultiListDialog(); //设置监听事件 btn_setDate.setOnClickListener(this); btn_singleDialog.setOnClickListener(this); btn_multiDialog.setOnClickListener(this); } //初始化dateDialog private void initDateDialog(){ datePickerDialog = new DatePickerDialog(MainActivity.this, new DatePickerDialog.OnDateSetListener() { @Override public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { Toast.makeText(MainActivity.this, year+"年"+(monthOfYear+1)+"月"+dayOfMonth+"日", Toast.LENGTH_LONG).show(); } }, Calendar.DAY_OF_YEAR, Calendar.DAY_OF_MONTH, Calendar.DATE); } //初始化singleListDlg private void initSingleListDialog(){ AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setTitle("性别选择"); builder.setIcon(android.R.drawable.btn_star); builder.setSingleChoiceItems(R.array.array_sex, android.R.layout.simple_selectable_list_item, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { switch (which){ case 0: Toast.makeText(MainActivity.this, "您选择了:男 性", Toast.LENGTH_LONG).show(); break; case 1: Toast.makeText(MainActivity.this, "您选择了:女 性", Toast.LENGTH_LONG).show(); break; case 2: Toast.makeText(MainActivity.this, "您选择了:性别未知", Toast.LENGTH_LONG).show(); break; case 3: Toast.makeText(MainActivity.this, "您选择了:你猜", Toast.LENGTH_LONG).show(); break; } singleListDlg.dismiss(); } }); singleListDlg = builder.create(); } //初始化multiListDlg private void initMultiListDialog(){ final String[] loves = getResources().getStringArray(R.array.array_love); final boolean[] isCheckedItem = new boolean[loves.length]; AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setTitle("选择个人爱好"); builder.setIcon(android.R.drawable.btn_star); builder.setMultiChoiceItems(R.array.array_love, isCheckedItem, new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { isCheckedItem[which] = isChecked; } }); builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { String info = "个人爱好:\n"; //换行好看点,视频没有换行 for (int i=0; i<loves.length; i++){ if (isCheckedItem[i]) info = info + loves[i]+"\n"; } tv_info.setText(info); multiListDlg.dismiss(); } }); multiListDlg = builder.create(); } @Override public void onClick(View v) { switch (v.getId()){ case R.id.btn_setDate: datePickerDialog.show(); break; case R.id.btn_singleDialog: singleListDlg.getListView().clearChoices(); singleListDlg.show(); break; case R.id.btn_multiDialog: multiListDlg.show(); break; } } }
单选框在show()之前用 getListView().clearChoices() 方法可以初始化,清除上次选择。但,多选框用 getListView().clearChoice() 方法没法清除,试了
for (int i=0; i<multiListDlg.getListView().getCount(); i++){ multiListDlg.getListView().setItemChecked(i, false); }
也不行。请问,有什么方法可以清除上次的选择?
43
收起
正在回答 回答被采纳积分+1
2回答
Android零基础入门2018版
- 参与学习 人
- 提交作业 5461 份
- 解答问题 7238 个
此次推出的专题为Android攻城狮培养计划的第一部分语法与界面基础篇,将带大家从0开始学习Android开发。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星