Java 使用复选框的动态方式
我有一个带有自定义复选框的alertdialog,我需要将选定的选项传递给主机活动,并保留选定的选项。这是我的密码:Java 使用复选框的动态方式,java,android,checkbox,android-alertdialog,Java,Android,Checkbox,Android Alertdialog,我有一个带有自定义复选框的alertdialog,我需要将选定的选项传递给主机活动,并保留选定的选项。这是我的密码: public class TimelineSettings extends DialogFragment { ArrayList<Integer> selected_categories = new ArrayList<Integer>(); boolean[] itemsChecked = {false, false, false
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;
}
}
}
}
公共类TimelineSettings扩展了DialogFragment{
ArrayList selected_categories=新建ArrayList();
boolean[]itemsChecked={false,false,false,false,false};
私人公寓、公寓、公寓、航海、住宿、媒体、公寓;
公共接口对话侦听器{
公共无效onOkay(选定ArrayList);
公共无效取消();
}
对话监听器;
@凌驾
公共事务主任(活动){
超级转速计(活动);
//确保主机活动实现回调接口
试一试{
//实例化dialogListener,以便向主机发送事件
mlistener=(dialoglistener)活动;
}catch(ClassCastException e){
//如果活动未实现接口,则引发异常
抛出新的ClassCastException(activity.toString()
+“必须实现dialogListener”);
}
}
@凌驾
创建对话框上的公共对话框(Bundle savedInstanceState){
AlertDialog.Builder=新建AlertDialog.Builder(getActivity());
LayoutInflater充气器=LayoutInflater.from(getActivity());
查看自定义=充气机。充气(R.layout.custom\u设置,空);
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);
如果(所选类别包含(0)){
fourniture.setChecked(真);
}
if(所选类别包含(1)){
无错误。设置已检查(正确);
}
if(所选类别包含(2)){
航程。设置已检查(真);
}
if(所选类别包含(3)){
能力。设置已检查(真);
}
if(所选类别包含(4)){
medias.setChecked(true);
}
if(所选类别包含(5)){
autres.setChecked(真);
}
builder.setView(自定义)
.setPositiveButton(“确定”,新的DialogInterface.OnClickListener(){
@凌驾
public void onClick(DialogInterface对话框,int-id){
checkboxState();
mlistener.onOkay(选定类别);
}
})
.setNegativeButton(“环空器”,新的DialogInterface.OnClickListener(){
@凌驾
public void onClick(DialogInterface对话框,int-id){
mlistener.onCancel();
}
});
返回builder.create();
}
public void checkboxState(){
if(fourniture.isChecked()){
如果(!selected_categories.contains(0)){
所选类别。添加(0);
itemsChecked[0]=真;
}
else if(所选类别包含(0)){
//否则,如果该项已在数组中,请将其删除
所选类别。删除(0);
itemsChecked[0]=false;
}
}
if(nourriture.isChecked()){
如果(!selected_categories.contains(1)){
所选类别。添加(1);
itemsChecked[1]=真;
}
else if(所选类别包含(1)){
所选类别。删除(1);
itemsChecked[1]=假;
}
}
if(voyages.isChecked()){
如果(!selected_categories.contains(2)){
选定的_类别。添加(2);
itemsChecked[2]=真;
}
else if(所选类别包含(2)){
所选类别。删除(2);
itemsChecked[2]=假;
}
}
if(habillement.isChecked()){
如果(!所选类别包含(3)){
选定的_类别。添加(3);
itemsChecked[3]=真;
}
else if(所选类别包含(3)){
所选类别。删除(3);
itemsChecked[3]=假;
}
}
if(medias.isChecked()){
如果(!所选类别包含(4)){
选定的_类别。添加(4);
itemsChecked[4]=真;
}
else if(所选类别包含(4)){
所选类别。删除(4);
itemsChecked[4]=假;
}
}
if(autres.isChecked()){
如果(!所选类别包含(5)){
选定的_类别。添加(5);
itemsChecked[5]=真;
}
else if(所选类别包含(5)){
所选类别。删除(5);
itemsChecked[5]=假;
}
}
}
}
这段代码工作得很好,但看起来不是这样!如果elses。。。
我的问题是,是否有一种方法可以在我的复选框上使用循环来获取它们的状态,然后将数组itemschecked
的元素设置为true/false以获得持久性。
谢谢 保留一组FlatCheckBox:
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]。。。可能会造成混淆并导致编程错误。您能告诉我如何使用该阵列替换吗