android基礎二之ToggleButton

//activity_main.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="vertical"
    tools:context="com.vincentlin.togglebutton.MainActivity" >
 <!-- textOn:true  textOff:false -->
    <ToggleButton
        android:id="@+id/toggleButton1"
        android:checked="false"
        android:textOn="@string/on"
        android:textOff="@string/off"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />
    <ImageView
        android:id="@+id/p_w_picpathView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/off" />
</LinearLayout>
//strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">ToggleButton</string>
    <string name="hello_world">Hello world!</string>
    <string name="action_settings">Settings</string>
    <string name="on">開</string>
    <string name="off">關</string>
</resources>
MainActivity.java
package com.vincentlin.togglebutton;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ToggleButton;
public class MainActivity extends Activity implements OnCheckedChangeListener{
 private ToggleButton tButton;
 private ImageView iView;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  //初始化組件
  tButton = (ToggleButton) findViewById(R.id.toggleButton1);
  iView = (ImageView) findViewById(R.id.p_w_picpathView1);
  /*
   * 給當前的tButton設置監(jiān)聽器
   */
  tButton.setOnCheckedChangeListener(this);
  
 }
 @Override
 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  /*
   * 
   * 當tButton被點擊的時候,當前的方法會執(zhí)行
   * buttonView---代表被點擊控件的本身,
   * isChecked---代表被點擊的控件的狀態(tài)
   * 當點擊這個tButton的時候,更換img的背景
   */
  iView.setBackgroundResource(isChecked?R.drawable.on:R.drawable.off);
  
 }
}
//效果圖

android基礎二之ToggleButton

創(chuàng)新互聯(lián)主營西區(qū)網(wǎng)站建設的網(wǎng)絡公司,主營網(wǎng)站建設方案,成都App制作,西區(qū)h5成都小程序開發(fā)搭建,西區(qū)網(wǎng)站營銷推廣歡迎西區(qū)等地區(qū)企業(yè)咨詢

android基礎二之ToggleButton

附件:http://down.51cto.com/data/2365305

文章題目:android基礎二之ToggleButton
瀏覽地址:http://muchs.cn/article0/pdgcoo.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供全網(wǎng)營銷推廣品牌網(wǎng)站建設、企業(yè)建站網(wǎng)站導航、微信小程序企業(yè)網(wǎng)站制作

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

小程序開發(fā)