Button btn_login; //定义登录按钮
CheckBox checkBox1, checkBox2, checkBox3; //定义复选框
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_login = (Button) findViewById(R.id.btn_login); //通过ID获取布局确认登录按钮
checkBox1 = (CheckBox) findViewById(R.id.checkbox1); //通过ID获取布局复选框1
checkBox2 = (CheckBox) findViewById(R.id.checkbox2); //通过ID获取布局复选框2
checkBox3 = (CheckBox) findViewById(R.id.checkbox3); //通过ID获取布局复选框3
btn_login.setOnClickListener(new View.OnClickListener() { //为确认登录按钮
@Override
public void onClick(View v) {
String checked = ""; //保存选中的值
if (checkBox1.isChecked()) { //当第一个复选框被选中
checked += checkBox1.getText().toString() ; //输出第一个复选框内信息
}
if (checkBox2.isChecked()) { //当第二个复选框被选中
checked += checkBox2.getText().toString() ; //输出第二个复选框内信息
}
if (checkBox3.isChecked()) { //当第三个复选框被选中
checked += checkBox3.getText().toString() ; //输出第三个复选框内信息
}
//显示被选中复选框对应的信息
Toast.makeText(MainActivity.this, checked, Toast.LENGTH_LONG).show();
}
});
}
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="登录后该应用将获得以下权限"
android:textSize="14sp"
/>
<CheckBox
android:id="@+id/checkbox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="获得你的公开信息(昵称、头像等)"
android:checked="true"
android:textSize="12sp"
android:textColor="#BDBDBD"/>
<CheckBox
android:id="@+id/checkbox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="寻找与你共同使用该应用的好友"
android:checked="true"
android:textSize="12sp"
android:textColor="#BDBDBD"/>
<CheckBox
android:id="@+id/checkbox3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="帮助你通过该应用向好友发送消息"
android:checked="true"
android:textSize="12sp"
android:textColor="#BDBDBD"/>
<Button
android:id="@+id/btn_login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#009688"
android:text="确认登录"/>
<Button
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF" android:text="取消"/>
