安卓天天练练(五)CompoundButton

本文详细介绍了如何在Android应用中利用ToggleButton实现动态状态切换的功能,并通过示例代码展示了如何设置ToggleButton的监听器和状态变化后的响应逻辑。同时,文章还提到了如何使用三目运算符简化代码逻辑,以及如何通过XML布局文件进行组件配置。此外,文章还涉及了CompoundButton组件的复用和单选按钮组的创建,提供了丰富的实践经验和技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ToggleButton 让我想起了从前jQuery还没有取消toggle方法时是怎么偷懒的。。

注意:

  • 如果LinearLayout,与RelativeLayout不同,必须有orientation。用可视化顶部的横着隔开或者竖着隔开的方形按钮也可以选择,例如android:orientation="vertical"
  • 三目运算符前面和js一样,那个state是不需要额外带括号的
  • 按钮右键点上去有Edit TextOff,TextOn
  • 文字右键点上去的Edit Text可以新建string,填好String和R.string.之后自动生成string.xml到/values
  • 图片右键点上去有ScaleType可供选择
  • 如果AVD启动黑屏,又没有报错,尝试删除旧的AVD新建后start再run as
package com.example.android_8_1;

import android.app.Activity;
import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.ToggleButton;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        ToggleButton tg = (ToggleButton) findViewById(R.id.toggleButton1);
        tg.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            // imported from android.widget.CompoundButton
            @Override
            public void onCheckedChanged(CompoundButton buttonView,
                    boolean isChecked) {
                // TODO Auto-generated method stub
                setBulbState(isChecked);
            }
        });
    }

    public void setBulbState(boolean state) {
        ImageView iv = (ImageView) findViewById(R.id.imageView1);
        iv.setImageResource(state? R.drawable.btn : R.drawable.btnhover);

        ToggleButton tg = (ToggleButton) findViewById(R.id.toggleButton1);
        tg.setChecked(state);
    }

}

XML很简单

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.android_8_1.MainActivity" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scaleType="fitXY"
        android:src="@drawable/btn" />

    <ToggleButton
        android:id="@+id/toggleButton1"
        android:layout_width="80dp"
        android:layout_height="40dp"
        android:layout_gravity="center_horizontal"
        android:text="ToggleButton"
        android:textOff="关灯"
        android:textOn="开灯" />

</LinearLayout>

安卓自带的机制很实用,此例结束。

另外,单选框可以用<RadioGroup></RadioGroup>包起来形成一组,实现单选排斥。

改成一组连动式CompoundButton

package com.example.android_8_1;

import com.example.android_8_1.R.id;

import android.app.Activity;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.ToggleButton;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        ToggleButton tg = (ToggleButton) findViewById(R.id.toggleButton1);
        tg.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            // imported from android.widget.CompoundButton
            @Override
            public void onCheckedChanged(CompoundButton buttonView,
                    boolean isChecked) {
                // TODO Auto-generated method stub
                setBulbState(isChecked);
            }
        });
        
        CheckBox cb = (CheckBox) findViewById(R.id.checkBox1);
        cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView,
                    boolean isChecked) {
                // TODO Auto-generated method stub
                setBulbState(isChecked);
            }
        });
        
        RadioButton rb = (RadioButton) findViewById(id.radioButton2);
        rb.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView,
                    boolean isChecked) {
                // TODO Auto-generated method stub
                setBulbState(isChecked);
            }
        });
    }

    public void setBulbState(boolean state) {
        ImageView iv = (ImageView) findViewById(R.id.imageView1);
        iv.setImageResource(state ? R.drawable.btn : R.drawable.btnhover);

        ToggleButton tg = (ToggleButton) findViewById(R.id.toggleButton1);
        CheckBox cb = (CheckBox) findViewById(R.id.checkBox1);
        
        tg.setText(state ? R.string.onn : R.string.offn);
        cb.setText(state ? R.string.offn : R.string.onn);
        tg.setChecked(state);
        cb.setChecked(state);
        RadioButton rb = (RadioButton) findViewById(R.id.radioButton2);
        rb.setChecked(state);
        rb = (RadioButton) findViewById(R.id.radioButton1);
        rb.setChecked(!state);
    }

}

XML不变。需要注意的是,如果逻辑上貌似通顺但是出现checkbox点下去没反应,一定是黑白颠倒弄反鸟!

比如文字设为onn活着offn,checked(!state)在对radioButton1还是radioButton2。

转载于:https://www.cnblogs.com/haimingpro/p/4671239.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值