Android用户界面基础之ToggleButton学习

  • 列表内容
    ToggleButton,状态按钮控件。该控件扩展自CompoundButton类,具有选中和非选中两种状态。可以通过单击在两种状态间切换。主要的xml属性包括:android:textOn和android:textOff分别用于设置当按钮处于选中和非选中状态时显示的文本内容。可以通过isChecked()方法获取状态按钮的当前状态。
    ·状态按钮
    • 继承自CompoundButton
      ·主要属性
    • Android:textOn
    • Android:textOf
      checked:默认是否选中
      ·主要方法
    • isChecked();
      ·主要事件
    • setOnClickListener(onClickListener l)
      实例:
      效果图:
      这里写图片描述

这里写图片描述

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="horizontal"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/tvInfo"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1.0"
        android:textSize="20sp"
        android:text="状态按钮被打开"
        android:textColor="#fff000aa" />

        <ToggleButton 
            android:id="@+id/tbText"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:textOff="关闭"
            android:textOn="打开"
            android:checked="true"
            android:onClick="doClick"/>
</LinearLayout>

activity:

package com.ecainiao.togglebutton;

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.TextView;
import android.widget.ToggleButton;

public class MainActivity extends Activity {
    private TextView tvInfo;
    private ToggleButton tbText;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tvInfo = (TextView) findViewById(R.id.tvInfo);
        tbText = (ToggleButton) findViewById(R.id.tbText);
    }

    public void doClick(View  v){
        if(tbText.isChecked()){
        tvInfo.setText("状态按钮打开");
        }else{
        tvInfo.setText("状态按钮关闭");
        }
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值