1. Radiobutton(单选)
以组的方式管理
若要多选一需要放置一个Radiogroup
定义变量
private RadioGroup rGroup;
private static TextView tView;
赋值
rGroup=(RadioGroup) findViewById(R.id.radiogroup1);
tView=(TextView) findViewById(R.id.textview1);
用匿名内部类的方法监听
rGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup arg0, int arg1) {
// TODO Auto-generated method stub
switch(arg1)
{
case R.id.radiobutton1: tView.setText("男被选中");break;
case R.id.radiobutton2: tView.setText("女被选中");
}
显示组件 listview
定义变量
private ListView lView;
获取组件
lView=(ListView) findViewById(R.id.listview1);
生成数据源
String data[]={"苹果","三星","华为","小米","火腿肠"};
将数据源封装到适配器中
ArrayAdapter arrayAdapter =new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,data);
将适配器绑定给列表控件
lView.setAdapter(arrayAdapter);
lView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
String msg=data[arg2];
Toast.makeText(MainActivity.this,msg,4000).show();
}
});
}
第五周笔记
网友评论