按钮 / 操作 | 作用(简单理解) | 特点 |
|---|---|---|
Run ‘app’(Shift+F10) | 重新安装并运行整个 App | - 耗时较长(要重新打包、安装、启动)- 相当于 “重启 App 并覆盖安装”- 优点:彻底清除旧代码,问题少(“妖魔鬼怪少”) |
Apply Changes and Restart Activity(Ctrl+F10) | 热更新代码,重启 Activity | - 耗时短(只更新改动部分,不用重装整个 App)- 相当于 “局部替换代码,重启页面”- 缺点:不是所有修改都支持(比如改了清单文件、架构代码,可能失效),偶尔会有奇怪问题(“妖魔鬼怪多”) |

<RadioGroup
android:id="@+id/radio_group"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:orientation="vertical"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginTop="30sp"
app:layout_constraintTop_toTopOf="parent">
<RadioButton
android:id="@+id/rb_bd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"
android:text="百度">
</RadioButton>
<RadioButton
android:id="@+id/rb_gg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="谷歌">
</RadioButton>
<RadioButton
android:id="@+id/rb_360"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"
android:text="360">
</RadioButton>
<RadioButton
android:id="@+id/rb_sg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"
android:text="搜狗">
</RadioButton>
</RadioGroup>RadioGroup会保证内部的单选按钮button只能选择一个,如果有多个checked被设置为true,会以最后一个button为主

译为:单选按钮
因为继承LinearLayout所以可以直接使用orientation去设置水平排列,它的高度去适应子控件,宽度去适应屏幕

这两句代码神来之笔~
可以理解成:
总结:最终效果就是让 RadioGroup 控件在父布局中 “左上角对齐” 父布局的左上角

app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="30sp"666太形象了,单选按钮继承复杂按钮,复杂按钮又继承按钮


<Button
android:id="@+id/btn_setting"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="设置默认搜索引擎"
app:layout_constraintTop_toBottomOf="@id/radio_group"
app:layout_constraintStart_toStartOf="parent" />
可以理解为Button的最上方在radio_group的最下方
app:layout_constraintTop_toBottomOf="@id/radio_group" //最上部顶着radio_group
android:layout_marginTop="30sp" //控制上边距//按钮
Button loginButton = findViewById(R.id.btn_login);
loginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//这里按钮的监听器的相关代码
}
});
//CheckBox复选框
CheckBox cbArgreement = findViewById(R.id.cb_agreement);
cbArgreement.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
}
});
//单选按钮
RadioGroup radioGroup = findViewById(R.id.radio_group);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
/**
* @param group 指当前的radioGroup
* @param checkedId 指的是被选中的id
*/
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
}
}); RadioGroup radioGroup = findViewById(R.id.radio_group);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
/**
*
* @param group 指当前的radioGroup
* @param checkedId 指的是被选中的id
*/
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
String index = "www.baidu.com";
if(checkedId == R.id.rb_bd){
index = "www.baidu.com";
}else if(checkedId == R.id.rb_gg){
index = "www.google.com";
}else if(checkedId == R.id.rb_360){
index = "www.360.com";
}else if(checkedId == R.id.rb_sg){
index = "www.sogou.com";
}
Toast.makeText(RadioButtonActivity.this,index,Toast.LENGTH_SHORT).show();
}
}); RadioGroup radioGroup = findViewById(R.id.radio_group);
radioGroup.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int checkedId = radioGroup.getCheckedRadioButtonId();
String index = "www.google.com";
if(checkedId == R.id.rb_bd){
index = "已将默认搜索引擎设置为:百度";
}else if(checkedId == R.id.rb_gg){
index = "已将默认搜索引擎设置为:谷歌";
}else if(checkedId == R.id.rb_360){
index = "已将默认搜索引擎设置为:360";
}else if(checkedId == R.id.rb_sg){
index = "已将默认搜索引擎设置为:搜狗";
}
}
});