zoukankan      html  css  js  c++  java
  • android studio开发笔记三

    1.ToggleButton:有两种状态:选中和未被选中状态,并且需要为不同的状态设置不同的显示文本
    2.ToggleButton属性:android:checked="true" android:textOff="关" android:textOn="开"
    activity_main:
    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout 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: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.administrator.demo2.MainActivity">

    <ToggleButton
    android:id="@+id/toggleButton1"
    android:checked="false"
    android:textOff="关闭"
    android:textOn="打开"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />
    <ImageView
    android:id="@+id/image1"
    android:background="@drawable/off"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
    </RelativeLayout>

    MainActivity:
    package com.example.administrator.demo2;

    import android.media.Image;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.widget.CompoundButton;
    import android.widget.ImageView;
    import android.widget.ToggleButton;

    public class MainActivity extends AppCompatActivity{
    private ToggleButton tb;
    private ImageView img;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //初始化控件
    tb=(ToggleButton)findViewById(R.id.toggleButton1);
    img=(ImageView)findViewById(R.id.image1);
    //给当前的tb设置监听器
    tb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    /*
    当tb被点击时,当前方法会被执行
    buttonView代表被点击控件的本身
    ischecked代表被点击控件的状态
    当点击tb时,更换img的背景
    */
    img.setBackgroundResource(isChecked?R.drawable.on:R.drawable.off);
    }
    });
    }
    }

  • 相关阅读:
    可视化工具D3.js教程 入门 (第十三章)—— 树状图
    可视化工具D3.js教程 入门 (第十二章)—— 力导向图
    可视化工具D3.js教程 入门 (第十一章)—— 饼图
    可视化工具D3.js教程 入门 (第十章)—— 交互式操作
    vue滑动页面选中标题,选中标题滚动到指定区域
    Vue样式穿透
    操作系统:进程和线程+进程的通讯方式
    客户端与服务端长连接的几种方式
    前端性能优化的 24 条建议(2020)-收藏
    idea中修改git提交代码的用户名
  • 原文地址:https://www.cnblogs.com/xy95/p/5836690.html
Copyright © 2011-2022 走看看