android studio Switch按钮

1.添加按钮

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <TextView
        android:id="@+id/tv1"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:layout_gravity="start"
        android:padding="5dp"
        android:text="Switch开关:">
    </TextView>
    <Switch
        android:id="@+id/sw1"
        android:layout_width="80dp"
        android:layout_height="30dp"
        android:layout_gravity="end">
    </Switch>


</LinearLayout>

    <TextView
        android:id="@+id/tv2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="start"
        android:layout_marginTop="10dp">
    </TextView>

2.监听按钮变化

private TextView tv2;
tv2 = findViewById(R.id.tv2);
Switch sw1 = findViewById(R.id.sw1);
sw1.setOnCheckedChangeListener(this::onCheckedChange);
private void onCheckedChange(CompoundButton compoundButton, boolean b) {

    String ss = String.format("Switch按钮的状态是%s",b?"开":"关");

    if(b){
        tv2.setText(ss);
    }else {
        tv2.setText(ss);
    }

}

3.自定义

定义选择器:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_checked="true"
        android:drawable="@drawable/s1">
    </item>
    <item android:drawable="@drawable/s2"></item>
</selector>
引用选择器:
<CheckBox
    android:layout_width="80dp"
    android:layout_height="30dp"
    android:button="@null"
    android:background="@drawable/switch_selector">
</CheckBox>

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android Studio中,要实现Switch开关定时开启的功能,可以按照以下步骤进行操作: 1. 首先,在你的布局文件中添加一个Switch控件。例如,在MainActivity的布局文件中添加如下代码: ```xml <Switch android:id="@+id/sendSwitch" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="false" android:text="定时开启" /> ``` 2. 在MainActivity类中,找到你想要使用Switch控件的位置,并进行以下操作: - 创建一个Switch对象,并绑定到布局中的Switch控件。例如: ```java private Switch sendSwitch; sendSwitch = findViewById(R.id.sendSwitch); ``` - 设置Switch的默认状态为关闭。例如: ```java sendSwitch.setChecked(false); ``` - 定义一个boolean类型的变量mPageOpen,用于存储开关状态。例如: ```java final boolean[] mPageOpen = {false}; ``` - 实例化一个Handler类的对象sendHandler,并创建一个Runnable类的对象sendRunnable。在run方法中编写开关打开时需要执行的操作,并设置循环执行的延迟时间。例如: ```java Handler sendHandler = new Handler(); Runnable sendRunnable = new Runnable() { @Override public void run() { if (mPageOpen = true; } else { mPageOpen = false; } sendHandler.postDelayed(sendRunnable, 1000); // 开关控制执行 } }); ``` 通过以上步骤,你就可以在Android Studio中实现Switch定时开启的功能了。当你点击Switch开关打开时,定时任务会循环执行所需的操作。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值