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("已关闭");
                }
        }
}


 这是效果图

 


实现起来相当简单, 供新手们参考。
源码下载:http://pan.baidu.com/share/link?shareid=413709&uk=1796216265

  • 4
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值