android控件 ToggleButton的应用

原文链接及附件下载地址:http://www.apkbus.com/android-56422-1-1.html


ToggleButton是android给我们提供的开关按钮,
有两种状态:选中和未选择状态。

以下是代码实例: main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="fill_parent" android:background="#FFF5F5F5"
        android:layout_height="fill_parent">
        <LinearLayout android:layout_width="fill_parent"
                android:layout_height="wrap_content" android:orientation="horizontal">
                <TextView android:textSize="14.0sp" android:id="@+id/tvSound" android:textColor="@android:color/black"
                        android:layout_width="wrap_content" android:layout_height="wrap_content"
                        android:text="已开启" />
                <ToggleButton 
                        android:id="@+id/tglSound" android:background="@drawable/selector_butn_toggle"
                        android:layout_width="wrap_content" android:layout_height="wrap_content"
                        android:checked="true" android:textOn="" android:textOff="" 
                        android:text="" />
        </LinearLayout>
</LinearLayout>


这是主MainActivity

package com.apkbus.toggle;

import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;
import android.widget.ToggleButton;

public class MainActivity extends Activity implements OnCheckedChangeListener{
        private ToggleButton mToggleButton;
        private TextView tvSound;
        @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);//隐藏标题栏
        setContentView(R.layout.main);
        initView();//初始化控件方法
    }

        private void initView() {
                mToggleButton = (ToggleButton) findViewById(R.id.tglSound); //获取到控件
                mToggleButton.setOnCheckedChangeListener(this);//添加监听事件
                tvSound = (TextView) findViewById(R.id.tvSound);
        }

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                        tvSound.setText("已开启");
                }else{
                        tvSound.setText("已关闭");
                }
        }
}

file:///C:\Users\Lenovo\AppData\Local\Temp\~R50PAW(9~CI620%RA5CWLS.jpg 这是效果图
   

未命名1.jpg (11.55 KB, 下载次数: 64)

下载附件  保存到相册

已开启

2012-6-21 10:17 上传


实现起来相当简单, 供新手们参考。
新手上路,横冲路撞, 若有提拔,不胜感激。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值