android怎么同时监听在多个checkbox并显示内容

android怎么同时监听在多个checkbox并显示内容,第1张

全选的点击事件改下就行了

public class Main extends Activity {

private static int positions;

String[] imgs;

private GridView gv ;

private Button button;

CheckBox checkBox;

private String string;

public void onCreate(Bundle savedInstanceState) {

imgs = getResources()getStringArray(Rarraycity);

superonCreate(savedInstanceState);

setContentView(Rlayoutmain0);

gv= (GridView) findViewById(Ridgridview);

button = (Button) findViewById(Ridbtn);

gvsetAdapter(new CheckBoxAdapter(this));

gvsetBackgroundResource(Rdrawableback);

buttonsetOnClickListener(new OnClickListener(){

@Override

public void onClick(View v) {

// TODO Auto-generated method stub

//循环CheckBox数量

for (int index = 0; index < imgslength; index++) {

//根据你前面设置的ID,查找CheckBox

//其实这样效率有点低

//可以在创建 checkBox 后,把checkBox加入到一个集合里,然后在循环集合

//这样就不用查找了控件了

checkBox = (CheckBox)gvfindViewById(index);

if(checkBox != null){

checkBoxsetChecked(true);

}

String str = checkBoxgetText()toString();

Logi("xxx", str); 

}

}

});

}

class CheckBoxAdapter extends BaseAdapter {

Context context;

public CheckBoxAdapter(Context context) {

thiscontext = context;

}

public int getCount() {

return imgslength;

}

public Object getItem(int position) {

return position;

}

public long getItemId(int position) {

return position;

}

public View getView(int position, View convertView, ViewGroup parent) {

checkBox= new CheckBox(context);

checkBoxsetText(imgs[position]);

checkBoxsetId(position);

positions=position;

checkBoxsetOnCheckedChangeListener(new OnCheckedChangeListener(){

@Override

public void onCheckedChanged(CompoundButton buttonView,

boolean isChecked) {

// TODO Auto-generated method stub

if(buttonViewisChecked()){

ToastmakeText(Mainthis, "你点击的是第" + buttonViewgetText() + "张",

ToastLENGTH_LONG)show(); 

string=buttonViewgetText()toString();

}

}

});

return checkBox;

}

}

}

protected void Page_Load(object sender, EventArgs e)

{

CheckBox chk = new CheckBox();

chkText = "testall"; // 这里可以换成数据库的内容

chkCheckedChanged += new EventHandler(chk_CheckedChanged);

chkAutoPostBack = true;

PageFormControlsAdd(chk);

for (int i = 0; i < 10; i++)

{

CheckBox chk2 = new CheckBox();

chk2Text = "test" + iToString(); // 这里可以换成数据库的内容

chk2Checked = (i % 3 == 0); // 这里可以换成数据库的内容

PageFormControlsAdd(chk2);

}

}

void chk_CheckedChanged(object sender, EventArgs e)

{

CheckBox all = sender as CheckBox;

foreach(Control ctl in PageFormControls)

{

if (ctl is CheckBox)

{

CheckBox chk = ctl as CheckBox;

chkChecked = allChecked;

}

}

}

jQuery(function($){

$("input[name='key']:checkbox")click(function(){

var ids = '';

var flag = 0;

$("#ids")attr("value",ids);

$("input[name='key']:checkbox")each(function(){

if (true == $(this)attr("checked")) {

ids += $(this)attr('value')+',';

flag += 1;

}

});

if(0 < flag) {

$("#ids")attr("value",ids);

return true;

}else {

alert('请至少选择一项!');

return false;

}

});

});

以上就是关于android怎么同时监听在多个checkbox并显示内容全部的内容,包括:android怎么同时监听在多个checkbox并显示内容、如何获取选中的checkbox的value值、怎样才能获取checkbox选择的值等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/9474900.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-28
下一篇2023-04-28

发表评论

登录后才能评论

评论列表(0条)

    保存