一、复选框和单选按钮
1、静态创建方式
1)、复选框(CheckBox)
第一步:设置布局:
<TextView android:id="@+id/TextView01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"/>
<CheckBox android:id="@+id/CheckBox01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CheckBox01" >
</CheckBox>
<CheckBox android:id="@+id/CheckBox02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CheckBox02" >
</CheckBox>
第二步:初始化组件(onCreate()):
final TextView textView = (TextView)findViewById(R.id.TextView01);
//(1)CheckBox
final CheckBox checkBox1= (CheckBox)findViewById(R.id.CheckBox01);
final CheckBox checkBox2= (CheckBox)findViewById(R.id.CheckBox02);
第三步:事件响应方法:
OnClickListener checkboxListener = new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.CheckBox01:
textView.setText
("CheckBox01是否选中:" + String.valueOf(checkBox1.isChecked()));
break;
case R.id.CheckBox02:
textView.setText
("CheckBox02是否选中:" + String.valueOf(checkBox2.isChecked()));
break;
}
}
};
第四步:配置监听器(Listener)
checkBox1.setOnClickListener(checkboxListener);
checkBox2.setOnClickListener(checkboxListener);
2)、单选按钮(RadioButton)
第一步:设置布局:
RadioGroup:按钮组
<RadioGroup android:id="@+id/RadioGroup01"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton android:id="@+id/RadioButton01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioButton01" >
</RadioButton>
<RadioButton android:id="@+id/RadioButton02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioButton02" >
</RadioButton>
</RadioGroup>
第二步:初始化组件(onCreate()):
final RadioButton radioButton1 = (RadioButton)findViewById(R.id.RadioButton01);
final RadioButton radioButton2 = (RadioButton)findViewById(R.id.RadioButton02);
第三步:事件响应方法:
OnClickListener radiolistener = new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.RadioButton01:
textView.setText
("选中1:");
break;
case R.id.RadioButton02:
textView.setText
("选中2:");
break;
}
}
};
第四步:配置监听器(Listener)
radioButton1.setOnClickListener(radiolistener);
radioButton2.setOnClickListener(radiolistener);
2、动态创建方式:
第一步:设置布局:
1)、main.xml主配置文件:
<Button android:id="@+id/button" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="确定" />
2)、checkbox.xml复选框布局文件:
<CheckBox android:id="@+id/checkbox" android:layout_width="fill_parent"
android:layout_height="wrap_content" />
第二步:初始化组件(onCreate()):
//全局变量(CheckBox集合)
private List<CheckBox> checkboxs = new ArrayList<CheckBox>();
1)、设置字符串数组
String[] checkboxText = new String[]
{ "是学生吗?", "是否从事过Android方面的工作?", "会开车吗?", "打算创业吗?" };
2)、在LinearLayout容器中添加复选框
//获取LinearLayout
// //注意没有setContentView(R.layout.main)
//inflate:膨胀
LinearLayout linearLayout = (LinearLayout) getLayoutInflater().inflate(
R.layout.main, null);
//根据字符数组长度添加复选框
for (int i = 0; i < checkboxText.length;i++)
{
//获取复选框布局
LinearLayout checkboxLinearLayout =
(LinearLayout) getLayoutInflater()
.inflate(R.layout.checkbox, null);
//将初始化好的Checkbox组件放入集合中
checkboxs.add((CheckBox) checkboxLinearLayout
.findViewById(R.id.checkbox));
//获取CheckBox组件并设置显示文本
checkboxs.get(i).setText(checkboxText[i]);
//将设置好的CheckBox放入LinearLayout容器中
linearLayout.addView(checkboxLinearLayout, i);
}
3)、设置Activity的布局文件
setContentView(linearLayout);
第三步:设置按钮监听器并配置事件响应方法
1)、事件响应方法:
@Override
public void onClick(View view)
{
String s = "";
for (CheckBox checkbox : checkboxs)
{
if (checkbox.isChecked())
s += checkbox.getText() + "\n";
}
if ("".equals(s))
s = "您还没选呢!";
Toast.makeText(this, s, Toast.LENGTH_LONG).show();
}
2)、设置监听器
public class MainActivity extends Activity implements OnClickListener
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(this);
1、静态创建方式
1)、复选框(CheckBox)
第一步:设置布局:
<TextView android:id="@+id/TextView01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"/>
<CheckBox android:id="@+id/CheckBox01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CheckBox01" >
</CheckBox>
<CheckBox android:id="@+id/CheckBox02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CheckBox02" >
</CheckBox>
第二步:初始化组件(onCreate()):
final TextView textView = (TextView)findViewById(R.id.TextView01);
//(1)CheckBox
final CheckBox checkBox1= (CheckBox)findViewById(R.id.CheckBox01);
final CheckBox checkBox2= (CheckBox)findViewById(R.id.CheckBox02);
第三步:事件响应方法:
OnClickListener checkboxListener = new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.CheckBox01:
textView.setText
("CheckBox01是否选中:" + String.valueOf(checkBox1.isChecked()));
break;
case R.id.CheckBox02:
textView.setText
("CheckBox02是否选中:" + String.valueOf(checkBox2.isChecked()));
break;
}
}
};
第四步:配置监听器(Listener)
checkBox1.setOnClickListener(checkboxListener);
checkBox2.setOnClickListener(checkboxListener);
2)、单选按钮(RadioButton)
第一步:设置布局:
RadioGroup:按钮组
<RadioGroup android:id="@+id/RadioGroup01"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton android:id="@+id/RadioButton01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioButton01" >
</RadioButton>
<RadioButton android:id="@+id/RadioButton02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RadioButton02" >
</RadioButton>
</RadioGroup>
第二步:初始化组件(onCreate()):
final RadioButton radioButton1 = (RadioButton)findViewById(R.id.RadioButton01);
final RadioButton radioButton2 = (RadioButton)findViewById(R.id.RadioButton02);
第三步:事件响应方法:
OnClickListener radiolistener = new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.RadioButton01:
textView.setText
("选中1:");
break;
case R.id.RadioButton02:
textView.setText
("选中2:");
break;
}
}
};
第四步:配置监听器(Listener)
radioButton1.setOnClickListener(radiolistener);
radioButton2.setOnClickListener(radiolistener);
2、动态创建方式:
第一步:设置布局:
1)、main.xml主配置文件:
<Button android:id="@+id/button" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="确定" />
2)、checkbox.xml复选框布局文件:
<CheckBox android:id="@+id/checkbox" android:layout_width="fill_parent"
android:layout_height="wrap_content" />
第二步:初始化组件(onCreate()):
//全局变量(CheckBox集合)
private List<CheckBox> checkboxs = new ArrayList<CheckBox>();
1)、设置字符串数组
String[] checkboxText = new String[]
{ "是学生吗?", "是否从事过Android方面的工作?", "会开车吗?", "打算创业吗?" };
2)、在LinearLayout容器中添加复选框
//获取LinearLayout
// //注意没有setContentView(R.layout.main)
//inflate:膨胀
LinearLayout linearLayout = (LinearLayout) getLayoutInflater().inflate(
R.layout.main, null);
//根据字符数组长度添加复选框
for (int i = 0; i < checkboxText.length;i++)
{
//获取复选框布局
LinearLayout checkboxLinearLayout =
(LinearLayout) getLayoutInflater()
.inflate(R.layout.checkbox, null);
//将初始化好的Checkbox组件放入集合中
checkboxs.add((CheckBox) checkboxLinearLayout
.findViewById(R.id.checkbox));
//获取CheckBox组件并设置显示文本
checkboxs.get(i).setText(checkboxText[i]);
//将设置好的CheckBox放入LinearLayout容器中
linearLayout.addView(checkboxLinearLayout, i);
}
3)、设置Activity的布局文件
setContentView(linearLayout);
第三步:设置按钮监听器并配置事件响应方法
1)、事件响应方法:
@Override
public void onClick(View view)
{
String s = "";
for (CheckBox checkbox : checkboxs)
{
if (checkbox.isChecked())
s += checkbox.getText() + "\n";
}
if ("".equals(s))
s = "您还没选呢!";
Toast.makeText(this, s, Toast.LENGTH_LONG).show();
}
2)、设置监听器
public class MainActivity extends Activity implements OnClickListener
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(this);