多选 CheckBox
package com.example.test1;
import android.os.Bundle;
import android.R.integer;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.*;
public class CheckBoxActivity extends Activity {
Button btnok;
LinearLayout layout;
TextView tvshow;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_check_box);
btnok=(Button)findViewById(R.id.btnok);
//放个LinearLayout布局,上面放多个checkbox,然后循环判断
layout=(LinearLayout)findViewById(R.id.likeLayout);
tvshow=(TextView)findViewById(R.id.tvshow);
btnok.setOnClickListener(btnokClickListener);
}
OnClickListener btnokClickListener=new OnClickListener() {
@Override
public void onClick(View v) {
tvshow.setText("");
int ChildCount=layout.getChildCount();
for(int i=0;i<ChildCount;i++){
if(layout.getChildAt(i) instanceof CheckBox)
{
CheckBox cb=(CheckBox)layout.getChildAt(i);
if(cb.isChecked())
{
tvshow.setText(tvshow.getText()+""+cb.getText());
}
}
}
}
};
}

浙公网安备 33010602011771号