**## 单选框
RadioButton
- RadioButton放入RadioGroup
- 点击事件在Onclick中
public class MainActivity extends AppCompatActivity {
Button bt;
RadioGroup rg;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt=findViewById(R.id.btn1);
rg=findViewById(R.id.rg);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
for (int i = 0; i < rg.getChildCount(); i++) {
RadioButton rbutton=(RadioButton) rg.getChildAt(i);
if (rbutton.isChecked()){
if (rbutton.getText().equals("101")){
Toast.makeText(MainActivity.this,"回答正确",Toast.LENGTH_LONG).show();
}else {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setMessage("错误");
builder.setPositiveButton("确定",null).show();
}
break;
}
}}
复选框
- 页面布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/strawberry"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="登陆后该引用获得一下权限"
/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/checkbox1"
android:text="获得你的公开信息"
android:checked="true"
android:textSize="12sp"
android:textColor="#BDBDBD"
/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/checkbox1"
android:text="获得你的公开信息"
android:checked="true"
android:textSize="12sp"
android:textColor="#BDBDBD"
/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/checkbox2"
android:text="寻找共同好友"
android:checked="true"
android:textSize="12sp"
android:textColor="#BDBDBD"
/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/checkbox3"
android:text="通过引用发送该信息"
android:checked="true"
android:textSize="12sp"
android:textColor="#BDBDBD"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#009688"
android:text="确认登录"
android:id="@+id/btn_login"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF"
android:text="取消"
/>
</LinearLayout>
-
页面样式
-
后台代码
package top.vanblog.androiddemo0510;
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.Toast;
public class MainActivity extends AppCompatActivity {
Button btn_login;
CheckBox chk1,chk2,chk3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_login=findViewById(R.id.btn_login);
chk1=findViewById(R.id.checkbox1);
chk2=findViewById(R.id.checkbox2);
chk3=findViewById(R.id.checkbox3);
btn_login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String checked="";
if (chk1.isChecked()) {
checked += chk1.getText().toString();
}
if ( chk2.isChecked()){
checked += chk2.getText().toString();
}
if ( chk3.isChecked()){
checked += chk3.getText().toString();
}
Toast.makeText(MainActivity.this,checked,Toast.LENGTH_LONG).show();
}
});
}
}