安卓天天练练(五)CompoundButton

安卓天天练练(五)CompoundButtonToggleButton让我想起了从前jQuery还没有取消toggle方法时是怎么偷懒的。。注意:如果LinearLayout,与RelativeLayout不同,必须有orientation。用可视化顶部的横着隔开或者竖着隔开的方形按钮也可以选择,例如android:orientation=”vertical”三目运算符前面和js一样,那个state是不需要额外带括号的按…

大家好,又见面了,我是你们的朋友全栈君。

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

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/141708.html原文链接:https://javaforall.net

(0)
上一篇 2022年5月25日 上午10:20
下一篇 2022年5月25日 上午10:20


相关推荐

  • lea指令小结

    lea指令小结lea 指令小结对于寄存器来说 第二个操作数是寄存器必须要加 不然报错 这里 lea 就是取 寄存器 的值 如 moveax 2leaebx eax 执行后 ebx 2movebx eax 等同于上句 leaebx eax 编译器报错 errorA2070 invalidinstr 对于变量来说加不加 都是一样的效果 都是取变量的地址 相当于指针如 numdword2lea numleaeax num eax 为

    2026年3月18日
    2
  • clientWidth、clientHeight 在各大浏览器中的获取方法

    clientWidth、clientHeight 在各大浏览器中的获取方法IE 中 nbsp document body clientWidth BODY 对象宽度 nbsp document body clientHeight BODY 对象高度 nbsp document documentElem clientWidth 可见区域宽度 nbsp document documentElem clientHeight 可见区域高度 nbsp FireFox 中

    2026年3月19日
    1
  • Checked异常和Runtime异常的区别_JAVA运行时异常

    Checked异常和Runtime异常的区别_JAVA运行时异常目录一、运行时异常1、什么是RuntimeExceptioin2、运行时异常的特点3、如何运用运行时异常二、运行时异常和ckecked异常的区别1、机制上2、逻辑上一、运行时异常1、什么是运行时异常程序在运行过程中出现的异常,RumtimeException是Exception的一个子类我们可以查看Jav

    2026年4月19日
    7
  • bootstrap的一些方法「建议收藏」

    bootstrap的一些方法「建议收藏」bootstrap就是相当于对标签的id和class的封装,和一些js的封装。

    2022年7月2日
    29
  • system verilog教程(魔方教程图解)

    第一章:Verilog简介1.1Verilog教程1.2Verilog简介1.3Verilog环境搭建1.4Verilog设计方法第二章:…

    2022年4月16日
    639
  • 嵌入式语音识别系统是什么

    嵌入式语音识别系统是什么随着人工智能行业的兴起 对人工智能技术的研发 我们周身慢慢涌现了人工智能的身影 它可以化身为你的爱车 在沙漠 森林或小巷中风驰电掣 它可以是智慧公正的交警 控制红绿灯 缓解交通的拥挤 它可以是给人以贴心照顾的小助理 熟悉你生活中的每一处小怪癖 而语音识别它能满足我们的需求 用更自然的方式与机器 虚拟助理进行交互沟通 实现拟人对话 对设备的操控或者问题答案的获取 还可利用语义建模 对某些业务场景中比

    2026年3月19日
    2

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号