Android 如何选中“全部输入”复选框
我有一个listview和一个复选框,如何选择listview中给出的所有复选框,下面是我的getView()代码。是否有任何财产来完成这项任务?请发送您对我当前代码的建议。谢谢 代码:Android 如何选中“全部输入”复选框,android,Android,我有一个listview和一个复选框,如何选择listview中给出的所有复选框,下面是我的getView()代码。是否有任何财产来完成这项任务?请发送您对我当前代码的建议。谢谢 代码: public class MainActivity extends Activity { MyCustomAdapter dataAdapter = null; private ArrayList<CheckBox> checkBoxes = new ArrayList<&
public class MainActivity extends Activity {
MyCustomAdapter dataAdapter = null;
private ArrayList<CheckBox> checkBoxes = new ArrayList<>();
private String[] application = { "AA", "BB", "CC"}; // Dynamic Application Names
private String[] device = { "EE", "FF", "GG"}; // Dynamic
// Device
// Names
private RadioGroup radioGroup1;
private RadioGroup radioGroup2;
private RadioButton btn;
private RadioButton btn2;
private String text1;
private String text2;
RadioButton button1;
RadioButton button2;
Button selectall;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Generate list View from ArrayList
displayListView();
checkButtonClick();
radioGroup1 = (RadioGroup) findViewById(R.id.radio1);
radioGroup2 = (RadioGroup) findViewById(R.id.radio1);
ViewGroup hourButtonLayout = (ViewGroup) findViewById(R.id.radio1); // This
// is
// the
// id
// of
// the
// RadioGroup
// we
// defined
for (int i = 0; i < application.length; i++) {
button1 = new RadioButton(this);
button1.setId(i);
button1.setText(application[i]);
hourButtonLayout.addView(button1);
radioGroup1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup mRadioGroup2, int checkedId2) {
for(int i=0; i<mRadioGroup2.getChildCount(); i++) {
btn = (RadioButton) mRadioGroup2.getChildAt(i);
int t=mRadioGroup2.getId();
System.out.println(t);
if(btn.getId() == checkedId2) {
text1 = btn.getText().toString();
Toast.makeText(getApplicationContext(), "You selected : " + text1, Toast.LENGTH_SHORT).show();
return;
}
}
}
});
}
ViewGroup hourButtonLayout2 = (ViewGroup) findViewById(R.id.radio2); // This
// is
// the
// id
// of
// the
// RadioGroup
// we
// defined
for (int i = 0; i < device.length; i++) {
button2 = new RadioButton(this);
button2.setId(i);
button2.setText(device[i]);
hourButtonLayout2.addView(button2);
radioGroup2.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup mRadioGroup, int checkedId) {
for(int i=0; i<mRadioGroup.getChildCount(); i++) {
btn2 = (RadioButton) mRadioGroup.getChildAt(i);
int t=mRadioGroup.getId();
System.out.println(t);
if(btn2.getId() == checkedId) {
text2 = btn2.getText().toString();
Toast.makeText(getApplicationContext(), "You selected : " + text2, Toast.LENGTH_SHORT).show();
return;
}
}
}
});
}
}
private void displayListView() {
// Array list of countries
ArrayList<Country> countryList = new ArrayList<Country>();
Country country = new Country("", "Al Jazeera", false);
countryList.add(country);
country = new Country("", "AA", true);
countryList.add(country);
country = new Country("", "BB", false);
countryList.add(country);
// create an ArrayAdaptar from the String Array
dataAdapter = new MyCustomAdapter(this, R.layout.country_info,
countryList);
ListView listView = (ListView) findViewById(R.id.listView1);
// Assign adapter to ListView
listView.setAdapter(dataAdapter);
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
Country country = (Country) parent.getItemAtPosition(position);
Toast.makeText(getApplicationContext(),
"Clicked on Row: " + country.getName(),
Toast.LENGTH_LONG).show();
}
});
}
private class MyCustomAdapter extends ArrayAdapter<Country> {
private ArrayList<Country> countryList;
public MyCustomAdapter(Context context, int textViewResourceId,
ArrayList<Country> countryList) {
super(context, textViewResourceId, countryList);
this.countryList = new ArrayList<Country>();
this.countryList.addAll(countryList);
}
private class ViewHolder {
TextView code;
CheckBox name;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
Log.v("ConvertView", String.valueOf(position));
if (convertView == null) {
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.country_info, null);
holder = new ViewHolder();
holder.code = (TextView) convertView.findViewById(R.id.code);
holder.name = (CheckBox) convertView
.findViewById(R.id.checkBox1);
convertView.setTag(holder);
checkBoxes.add(holder.name);
selectall = (Button) findViewById(R.id.selectall);
selectall.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
for (int i = 0; i < checkBoxes.size(); i++) {
CheckBox currentChecBox = checkBoxes.get(i);
currentChecBox.setChecked(true);
}
}
});
holder.name.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
CheckBox cb = (CheckBox) v;
Country country = (Country) cb.getTag();
Toast.makeText(
getApplicationContext(),
"Clicked on Checkbox: " + cb.getText() + " is " + cb.isChecked(), Toast.LENGTH_LONG).show();
country.setSelected(cb.isChecked());
}
});
} else {
holder = (ViewHolder) convertView.getTag();
}
Country country = countryList.get(position);
holder.code.setText("");
holder.name.setText(country.getName());
holder.name.setChecked(country.isSelected());
holder.name.setTag(country);
return convertView;
}
}
private void checkButtonClick() {
Button myButton = (Button) findViewById(R.id.findSelected);
myButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
StringBuffer responseText = new StringBuffer();
responseText.append("The following were selected : ");
ArrayList<Country> countryList = dataAdapter.countryList;
for (int i = 0; i < countryList.size(); i++) {
Country country = countryList.get(i);
if (country.isSelected()) {
responseText.append(", " + country.getName());
String response = responseText.toString();
}
}
// Get selected radiobuttons
if(radioGroup1.getCheckedRadioButtonId()!=-1){
text1 = btn.getText().toString();
Log.d("Button", "Text 1 : " + text1);
}
if(radioGroup2.getCheckedRadioButtonId()!=-1){
text2 = btn2.getText().toString();
Log.d("Button", "Text 2 : " + text2);
}
Toast.makeText(getApplicationContext(), "SHOW NAMES : " + responseText + "and APPLICATION : " + text1 + "and DEVICE : " + text2, Toast.LENGTH_LONG).show();
}
});
}
}
公共类MainActivity扩展活动{
MyCustomAdapter dataAdapter=null;
私有ArrayList复选框=新建ArrayList();
私有字符串[]应用程序={“AA”、“BB”、“CC”};//动态应用程序名称
私有字符串[]设备={“EE”、“FF”、“GG”};//动态
//装置
//名字
私人放射组放射组1;
私人放射组放射组2;
专用无线按钮;
专用无线按钮btn2;
私有字符串text1;
私有字符串text2;
单选按钮1;
无线电按钮2;
按钮选择全部;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//从ArrayList生成列表视图
displayListView();
选中按钮单击();
放射组1=(放射组)findViewById(R.id.radio1);
放射组2=(放射组)findViewById(R.id.radio1);
ViewGroup hourButtonLayout=(ViewGroup)findViewById(R.id.radio1);//此
//是
//
//身份证
//的
//
//放射组
//我们
//明确的
for(int i=0;i 对于(int i=0;i而言,没有此属性
您需要跟踪所有复选框,当您单击“全选”时,通过在每个复选框上调用setChecked(true)
标记所有复选框。您可以创建一个包含所有复选框的数组列表,然后当您希望选中所有复选框时,只需在数组中执行for循环并设置checked(true)在每一个上。例如
在适配器顶部声明arraylist
private ArrayList<CheckBox> checkBoxes = new ArrrayList<>();
然后在onClick中
for (int i = 0; i < checkBoxes.size(); i++) {
CheckBox currentChecBox = checkBoxes.get(i);
currentChecBox.setChecked(true);
}
for(int i=0;i
您可以使用..实现它。。
单击“全选”时,只需应用此代码
for (int i=0;i<your_list_view.getChildCount();i++) {
View v = your_list_view.getChildAt(i);
CheckBox cb = v.findViewById(R.id.your_checkbox_id);
cb.setChecked(true);
}
用于(int i=0;iThanks@Andy:我如何用我当前的实现来实现这一点?不,Andy,我正在更新我的代码。你能在点击后查看一下吗?你能注销复选框数组的大小以确保添加了。你在哪里声明了数组?谢谢@Manish:我如何用我当前的实现来实现这一点
for (int i=0;i<your_list_view.getChildCount();i++) {
View v = your_list_view.getChildAt(i);
CheckBox cb = v.findViewById(R.id.your_checkbox_id);
cb.setChecked(true);
}