一、LinearLayout 视图页面
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_app10"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<CheckBox
android:id="@+id/checkbox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="篮球" />
<CheckBox
android:id="@+id/checkbox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="足球" />
<CheckBox
android:id="@+id/checkbox3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="羽毛球" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center">
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="获取复选框数据"
android:layout_marginTop="20dp"
android:padding="10dp"
android:textSize="18sp"
android:textColor="#fff"
android:background="@drawable/button_shape"/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton
android:id="@+id/man"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="男"/>
<RadioButton
android:id="@+id/woman"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女"/>
</RadioGroup>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center">
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/button_shape"
android:textSize="18sp"
android:textColor="#fff"
android:padding="10dp"
android:layout_marginTop="20dp"
android:text="获取单选框的值"/>
</LinearLayout>
</LinearLayout>
二、入口文件
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.CheckBox
import android.widget.RadioButton
import android.widget.Toast
import java.util.ArrayList
import java.util.List
public class App10 extends AppCompatActivity {
//定义按钮与复选框及List
private Button button1, button2
private CheckBox checkbox1, checkbox2, checkbox3
private RadioButton man, woman
private List<CheckBox> checkBoxList = new ArrayList<>()
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_app10)
//初始化控件
checkbox1 = (CheckBox) findViewById(R.id.checkbox1)
checkbox2 = (CheckBox) findViewById(R.id.checkbox2)
checkbox3 = (CheckBox) findViewById(R.id.checkbox3)
button1 = (Button) findViewById(R.id.button1)
button2 = (Button) findViewById(R.id.button2)
man = (RadioButton) findViewById(R.id.man)
woman = (RadioButton) findViewById(R.id.woman)
//追加到checkBoxList中
checkBoxList.add(checkbox1)
checkBoxList.add(checkbox2)
checkBoxList.add(checkbox3)
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
StringBuffer sb = new StringBuffer()
for (CheckBox checkbox : checkBoxList) {
if (checkbox.isChecked()) {
sb.append(checkbox.getText().toString() + "-")
}
}
if (sb != null && "".equals(sb.toString())) {
Toast.makeText(getApplicationContext(), "至少选择一个", Toast.LENGTH_LONG).show()
} else {
Toast.makeText(getApplicationContext(), "你选择的是:" + sb.toString(), Toast.LENGTH_LONG).show()
}
}
})
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(man.isChecked()){
Toast.makeText(getApplicationContext(),"你选择的是男",Toast.LENGTH_LONG).show()
}else if(woman.isChecked()){
Toast.makeText(getApplicationContext(),"你选择的是女",Toast.LENGTH_LONG).show()
}else {
Toast.makeText(getApplicationContext(),"你还没选择",Toast.LENGTH_LONG).show()
}
}
})
}
}
三、边框圆角
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#f00"/>
<stroke android:width="1dp" android:color="#630"/>
<corners android:radius="5dp"/>
</shape>