settext用法_Android常见控件用法

1.switch用法

aSwitch = findViewById(R.id.switch1);
aSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (isChecked){
            //开关打开时的操作
        }else {
            //开关关闭时的操作
        }
    }
});
aSwitch.setText("设置switch前面显示的文字");

2.radioGroup、radioButton用法

radioGroup = findViewById(R.id.radioGroup);
radioButton1 = findViewById(R.id.radioButton);
radioButton2 = findViewById(R.id.radioButton2);
radioButton3 = findViewById(R.id.radioButton3);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId){
            case R.id.radioButton:
                //选中radioButton时进行的操作,radiobutton是控件在xml文件中的id
                break;
            case R.id.radioButton2:
                break;
            case R.id.radioButton3:
                break;
        }
    }
});

3.radiobutton和switch结合

java

aSwitch = findViewById(R.id.switch1);
radioGroup = findViewById(R.id.radioGroup);
radioButton1 = findViewById(R.id.radioButton);
radioButton2 = findViewById(R.id.radioButton2);
radioButton3 = findViewById(R.id.radioButton3);
textView = findViewById(R.id.textView);

textView.setText("-1");
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId){
            case R.id.radioButton:
                //选中radioButton时进行的操作,radiobutton是控件在xml文件中的id
                textView.setText("-1");
                break;
            case R.id.radioButton2:
                textView.setText("-2");
                break;
            case R.id.radioButton3:
                textView.setText("-3");
                break;
        }
    }
});
aSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (isChecked){
            radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(RadioGroup group, int checkedId) {
                    switch (checkedId){
                        case R.id.radioButton:
                            textView.setText("1");
                            break;
                        case R.id.radioButton2:
                            textView.setText("2");
                            break;
                        case R.id.radioButton3:
                            textView.setText("3");
                            break;
                    }
                }
            });
        }else{
            radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(RadioGroup group, int checkedId) {
                    switch (checkedId){
                        case R.id.radioButton:
                            textView.setText("-1");
                            break;
                        case R.id.radioButton2:
                            textView.setText("-2");
                            break;
                        case R.id.radioButton3:
                            textView.setText("-3");
                            break;
                    }
                }
            });
        }
        int buttonId =radioGroup.getCheckedRadioButtonId();
        radioGroup.clearCheck();
        radioGroup.check(buttonId);
    }
});

xml

<Switch
    android:id="@+id/switch1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Switch"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="@+id/guideline" />

<RadioGroup
    android:id="@+id/radioGroup"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="52dp"
    android:layout_marginBottom="75dp"
    app:layout_constraintBottom_toTopOf="@+id/textView"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/horizontalScrollView">

    <RadioButton
        android:id="@+id/radioButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="RadioButton"
        android:checked="true"/>

    <RadioButton
        android:id="@+id/radioButton2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="RadioButton" />

    <RadioButton
        android:id="@+id/radioButton3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="RadioButton" />
</RadioGroup>

<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="116dp"
    android:layout_marginBottom="97dp"
    android:text="TextView"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/radioGroup" />

效果图

797492c9d7a1a01576b75f784bfef1f8.gif
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值