摘要:如何利用切換圖片文件給當前的設置監(jiān)聽器實現(xiàn)接口來設置監(jiān)聽,當點擊按鈕的時候開始執(zhí)行方法代表被點擊的控件按鈕被點擊控件的狀態(tài)當點擊開關的時候,更換的背景布局文件關開效果圖
如何利用ToggleButton切換圖片
java文件
package com.example.administrator.app; import android.app.Activity; import android.os.Bundle; import android.widget.CompoundButton; import android.widget.ImageView; import android.widget.ToggleButton; public class toggleBtn extends Activity implements CompoundButton.OnCheckedChangeListener { private ToggleButton tb; private ImageView img; protected void onCreate(Bundle saveInstanceState){ super.onCreate(saveInstanceState); setContentView(R.layout.togglebutton); tb = (ToggleButton)findViewById(R.id.toggle_button); img = (ImageView) findViewById(R.id.imageView); /**
給當前的tb設置監(jiān)聽器
*/
tb.setOnCheckedChangeListener(this);
}
//Called when the checked state of a compound button has changed. //@param buttonView The compound button view whose state has changed. //@param isChecked The new checked state of buttonView.
@Override
void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//實現(xiàn)接口CompoundButton.OnCheckedChangeListener 來設置tb監(jiān)聽,當點擊 按鈕tb的時候開始執(zhí)行 // onCheckedChanged方法 //buttonView 代表被點擊的控件按鈕 // isChecked 被點擊控件的狀態(tài) //當點擊tb開關的時候,更換img的背景 img.setBackgroundResource(isChecked?R.drawable.on:R.drawable.off); }
}
布局文件
android:layout_height="match_parent"
android:orientation="vertical">
效果圖:
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉載請注明本文地址:http://m.hztianpu.com/yun/64729.html
摘要:前言今天群里面有很多都在問關于組件之間是如何通信的問題,之前自己寫的時候也遇到過這類問題。英文能力有限,如果有不對的地方請跟我留言,一定修改原著序處理組件之間的交流方式,主要取決于組件之間的關系,然而這些關系的約定人就是你。 前言 今天群里面有很多都在問關于 React 組件之間是如何通信的問題,之前自己寫的時候也遇到過這類問題。下面是我看到的一篇不錯英文版的翻譯,看過我博客的人都知道...
摘要:有很多的控件,比如按鈕,單選框,滑動條,復選框等等。要設置窗口標題,我們就要檢查單選框的狀態(tài)。我們創(chuàng)建了一個水平的進度條和一個按鈕,這個按鈕控制進度條的開始和停止。這個例子有日期組件和標簽組件組成,標簽顯示被選中的日期。 控件1 控件就像是應用這座房子的一塊塊磚。PyQt5有很多的控件,比如按鈕,單選框,滑動條,復選框等等。在本章,我們將介紹一些很有用的控件:QCheckBox,Tog...
閱讀 1961·2023-04-26 02:14
閱讀 3862·2021-11-23 09:51
閱讀 1471·2021-10-13 09:39
閱讀 4055·2021-09-24 10:36
閱讀 3076·2021-09-22 15:55
閱讀 3598·2019-08-30 12:57
閱讀 2098·2019-08-29 15:30
閱讀 2051·2019-08-29 13:19