Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何取消选中所有我的复选框_Java_Android - Fatal编程技术网

Java 如何取消选中所有我的复选框

Java 如何取消选中所有我的复选框,java,android,Java,Android,我试图取消选中所有复选框,但没有成功 通过按下java android程序中的任何按钮 public void onListItemClick(ListView parent, View v, int position, long id) { parent.setItemChecked(position, parent.isItemChecked(position)); } @Override protected Dialog onCreateDialog(int

我试图取消选中所有复选框,但没有成功

通过按下java android程序中的任何按钮

public void onListItemClick(ListView parent, View v, int position, long id)
 {
        parent.setItemChecked(position, parent.isItemChecked(position));
 }


 @Override
 protected  Dialog onCreateDialog(int id, Bundle args){
    switch(id){
    case 1:
        return new AlertDialog.Builder(this)
        .setIcon(R.drawable.ic_launcher)
        .setTitle("This is a dialog with some simple text...")
        .setPositiveButton("OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton)
            {
                Toast.makeText(getBaseContext(),"OK clicked!", Toast.LENGTH_SHORT).show();
            }})

        .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton)
            {
                Toast.makeText(getBaseContext(),"Cancel clicked!", Toast.LENGTH_SHORT).show();
            }
            })

        .setMultiChoiceItems(items, itemsChecked, new DialogInterface.OnMultiChoiceClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                Toast.makeText(getBaseContext(),items[which] + (isChecked ? " checked!":" unchecked!"),Toast.LENGTH_SHORT).show();
            }
            })
        .create();
        }
        return(null);
    }

如何选择全部和取消选择全部?

好吧,我的最佳猜测是在列表中为所有这些对象保存一个引用,然后遍历所有对象,选择或取消选择所有对象。像这样的

创建一个引用以保存所有复选框

List<CheckBox> myCheckBoxes = new ArrayList<CheckBox>();
myCheckBoxes.add(cb1);
myCheckBoxes.add(cb2);
myCheckBoxes.add(cb3);
....
创建两个按钮:

Button unselectAllButton = (Button) findViewById(R.id.unCheckButton);
Button selectAllButton = (Button) findViewById(R.id.checkButton);

unselectAllButton.setOnClickListener(setOnClickListener(new OnClickListener() {
   public void onClick(View v) {
       checkAll(false); 
   }
});

selectAllButton.setOnClickListener(setOnClickListener(new OnClickListener() {
   public void onClick(View v) {
       checkAll(true); 
   }
});

我能得到这个的任何样本吗?谢谢你的帮助,我对安卓相当陌生。。。。你能用你的解决方案更新我的代码吗?再看一遍你的代码,我想你想在OnListTyMcCutt上检查你的复选框。在这里,使用所需的布尔值调用checkAll方法。使列表成为类的一个字段,并在“onCreate()开头实例化它(并添加xml布局(或通过编程创建)上的所有复选框)。我不能为你做所有这些,因为我不知道你想要完成的一切。我只是发布了大部分内容,要么你写你想做的一切,然后发布你的代码,要么我不能再帮你了。再次感谢你的帮助,我需要一个简单的解决方案来取消选中所有复选框。在我的程序中,我制作了一个按钮,可以清除所有的选择ed复选框,但我不能重复
Button unselectAllButton = (Button) findViewById(R.id.unCheckButton);
Button selectAllButton = (Button) findViewById(R.id.checkButton);

unselectAllButton.setOnClickListener(setOnClickListener(new OnClickListener() {
   public void onClick(View v) {
       checkAll(false); 
   }
});

selectAllButton.setOnClickListener(setOnClickListener(new OnClickListener() {
   public void onClick(View v) {
       checkAll(true); 
   }
});