Android控件之ToggleButton的使用方法
ToggleButton的狀態(tài)只能是選中和未選中,并且需要為不同的狀態(tài)設(shè)置不同的顯示文本。
以下案例為ToggleButton的用法
目錄結(jié)構(gòu)
main.xml布局文件 import android.app.Activity; public class ToggleButtonActivity extends Activity { public void onCheckedChanged(CompoundButton buttonView,
<?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:layout_height="fill_parent">
<ImageView android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/bulb_off"
android:layout_gravity="center_horizontal" />
<ToggleButton android:id="@+id/toggleButton"
android:layout_width="140dip"
android:layout_height="wrap_content"
android:textOn="開燈"
android:textOff="關(guān)燈"
android:layout_gravity="center_horizontal" />
</LinearLayout>
ToggleButtonActivity類
package com.ljq.tb;
import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.ToggleButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
private ImageView imageView=null;
private ToggleButton toggleButton=null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imageView=(ImageView) findViewById(R.id.imageView);
toggleButton=(ToggleButton)findViewById(R.id.toggleButton);
toggleButton.setOnCheckedChangeListener(new OnCheckedChangeListener(){
boolean isChecked) {
toggleButton.setChecked(isChecked);
imageView.setImageResource(isChecked?R.drawable.bulb_on:R.drawable.bulb_off);
}
});
}
}
相關(guān)文章
Android編程調(diào)用Camera和相冊功能詳解
這篇文章主要介紹了Android編程調(diào)用Camera和相冊功能,結(jié)合實例形式分析了Android的拍照及相冊調(diào)用功能相關(guān)實現(xiàn)技巧與操作注意事項,需要的朋友可以參考下2017-02-02Android實現(xiàn)獲取未接來電和未讀短信數(shù)量的方法
這篇文章主要介紹了Android實現(xiàn)獲取未接來電和未讀短信數(shù)量的方法,是Android程序開發(fā)中非常常見的重要功能,需要的朋友可以參考下2014-08-08Android使用SQLite數(shù)據(jù)庫的示例
本篇文章主要介紹了Android使用SQLite數(shù)據(jù)庫的示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-01-01Android開發(fā)必備:秒殺真機超快模擬器Genymotion介紹
這篇文章主要介紹了Android開發(fā)必備:秒殺真機超快模擬器Genymotion介紹,本文直接用圖片說明Genymotion的安裝和模擬效果,并提供官網(wǎng),需要的朋友可以參考下2015-04-04Android 實現(xiàn)沉浸式狀態(tài)欄的方法
沉浸式狀態(tài)欄的來源就是很多手機用的是實體按鍵,沒有虛擬鍵,于是開了沉浸模式就只有狀態(tài)欄消失了。下面腳本之家小編給大家介紹Android 實現(xiàn)沉浸式狀態(tài)欄,需要的朋友可以參考下2015-09-09詳解Android Libgdx中ScrollPane和Actor事件沖突問題的解決辦法
這篇文章主要介紹了詳解Android Libgdx中ScrollPane和Actor事件沖突問題的解決辦法的相關(guān)資料,希望通過本文能幫助到大家,需要的朋友可以參考下2017-09-09