我有自定义复选框的alertdialog,我需要将选定的选项传递给主机活动,并保留所选的选项。这是我的代码:
public class TimelineSettings extends DialogFragment {
ArrayList<Integer> selected_categories = new ArrayList<Integer>();
boolean[] itemsChecked = {false, false, false, false, false, false};
private FlatCheckBox fourniture,nourriture,voyages,habillement,medias,autres;
public interface dialoglistener {
public void onOkay(ArrayList<Integer> selected);
public void onCancel();
}
dialoglistener mlistener;
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
// ensure that the host activity implements the callback interface
try {
// Instantiate the dialogListener so we can send events to the host
mlistener = (dialoglistener) activity;
} catch (ClassCastException e) {
// if activity doesn't implement the interface, throw an exception
throw new ClassCastException(activity.toString()
+ " must implement dialogListener");
}
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = LayoutInflater.from(getActivity());
View custom = inflater.inflate(R.layout.custom_settings,null);
fourniture = (FlatCheckBox)custom.findViewById(R.id.fourniture);
nourriture = (FlatCheckBox)custom.findViewById(R.id.nourriture);
voyages = (FlatCheckBox)custom.findViewById(R.id.voyages);
habillement = (FlatCheckBox)custom.findViewById(R.id.habillement);
medias = (FlatCheckBox)custom.findViewById(R.id.medias);
autres = (FlatCheckBox)custom.findViewById(R.id.autres);
if (selected_categories.contains(0)){
fourniture.setChecked(true);
}
if (selected_categories.contains(1)){
nourriture.setChecked(true);
}
if (selected_categories.contains(2)){
voyages.setChecked(true);
}
if (selected_categories.contains(3)){
habillement.setChecked(true);
}
if (selected_categories.contains(4)){
medias.setChecked(true);
}
if (selected_categories.contains(5)){
autres.setChecked(true);
}
builder.setView(custom)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
checkboxState();
mlistener.onOkay(selected_categories);
}
})
.setNegativeButton("Annuler", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
mlistener.onCancel();
}
});
return builder.create();
}
public void checkboxState(){
if (fourniture.isChecked()){
if(!selected_categories.contains(0)){
selected_categories.add(0);
itemsChecked[0]=true;
}
else if (selected_categories.contains(0)) {
// Else, if the item is already in the array, remove it
selected_categories.remove(0);
itemsChecked[0]=false;
}
}
if (nourriture.isChecked()){
if(!selected_categories.contains(1)){
selected_categories.add(1);
itemsChecked[1]=true;
}
else if (selected_categories.contains(1)) {
selected_categories.remove(1);
itemsChecked[1]=false;
}
}
if (voyages.isChecked()){
if(!selected_categories.contains(2)){
selected_categories.add(2);
itemsChecked[2]=true;
}
else if (selected_categories.contains(2)) {
selected_categories.remove(2);
itemsChecked[2]=false;
}
}
if (habillement.isChecked()){
if(!selected_categories.contains(3)){
selected_categories.add(3);
itemsChecked[3]=true;
}
else if (selected_categories.contains(3)) {
selected_categories.remove(3);
itemsChecked[3]=false;
}
}
if (medias.isChecked()){
if(!selected_categories.contains(4)){
selected_categories.add(4);
itemsChecked[4]=true;
}
else if (selected_categories.contains(4)) {
selected_categories.remove(4);
itemsChecked[4]=false;
}
}
if (autres.isChecked()){
if(!selected_categories.contains(5)){
selected_categories.add(5);
itemsChecked[5]=true;
}
else if (selected_categories.contains(5)) {
selected_categories.remove(5);
itemsChecked[5]=false;
}
}
}
}
这段代码工作正常,但看起来并不是这样!如果没有太多......
我的问题是,是否有办法在我的复选框上使用循环来获取其状态,然后将数组itemschecked
的元素设置为true / false以表示持久性。
谢谢!
答案 0 :(得分:2)
保留FlatCheckBoxs数组:
private FlatCheckBox [] check = new FlatCheckBox[6];
并使用枚举来检索您需要的复选框:
private enum Names
{
fourniture(0),
nourriture(1),
voyages(2),
habillement(3),
medias(4),
autres(5);
private int val;
Names(int a)
{
val = a;
}
public int get()
{
return val;
}
};
现在使用:
public void checkboxState()
{
for(FlatCheckBox fb : check)
{
if (fb.isChecked())
{
}
}
}
您可以像这样访问数组的各个元素:
FlatCheckBox fourniture = check[Names.fourniture.get()];
我会使用枚举来获得更好的可读性,使用[0],[1] ...可能会造成混淆并导致编程错误。