欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android控件ToggleButton多狀態(tài)按鈕使用詳解

 更新時(shí)間:2017年06月02日 10:51:02   作者:朱衛(wèi)東  
這篇文章主要為大家詳細(xì)介紹了Android控件ToggleButton多狀態(tài)按鈕的使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

什么是ToggleButton?

ToggleButton一般有兩種狀態(tài):選中和未選中
并且需要為不同狀態(tài)設(shè)置不同的文本

ToggleButton屬性

android:checked=”true”——當(dāng)前按鈕狀態(tài),選中為”true”,未選中為”false”
android:textOn=”開(kāi)”
android:checked=”true”的時(shí)候,顯示 取決于checked的狀態(tài),即當(dāng)checked=”true”的時(shí)候,顯示textOn=”開(kāi)”,當(dāng)checked=”false”的時(shí)候,顯示checked=”true”

先來(lái)看一下實(shí)現(xiàn)效果:

具體代碼

 <ToggleButton
 android:checked="false"
 android:id="@+id/toggleButton"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:textOn="開(kāi)"
 android:textOff="關(guān)" />
 <ImageView
 android:id="@+id/imageView1"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:src="@drawable/off" />
package com.example.admin.demo;

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

public class MainActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {

 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.toggleButton);
 img = (ImageView) findViewById(R.id.imageView1);

 //設(shè)置監(jiān)聽(tīng)器
 tb.setOnCheckedChangeListener(this);
 }

 @Override
 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
 img.setImageResource(isChecked?R.drawable.on:R.drawable.off);
 }
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論