關於安卓開發實現雙狀態按鈕

來源:互聯網
上載者:User

標籤:android   style   blog   http   io   ar   color   os   sp   

雙狀態按鈕(Toggle)

xml檔案代碼

 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3     android:layout_width="match_parent" 4     android:layout_height="match_parent" 5     android:orientation="vertical" > 6     <ImageView android:layout_width="wrap_content" 7         android:layout_height="wrap_content" 8         android:src="@drawable/bulb_off" 9         android:id="@+id/imageView"10         android:layout_gravity="center_horizontal"11         />12     <ToggleButton android:layout_width="140dip"13         android:layout_height="wrap_content"14         android:textOn="開啟"15         android:textOff="關閉"16         android:id="@+id/toggleButton"17         android:layout_gravity="center_horizontal"18         />19 20 </LinearLayout>
xml檔案


java檔案

 1 package lianxi; 2  3 import java.security.PublicKey; 4  5 import com.example.jichu_lianxi.R; 6  7 import android.app.Activity; 8 import android.os.Bundle; 9 import android.widget.CompoundButton;10 import android.widget.CompoundButton.OnCheckedChangeListener;11 import android.widget.ImageView;12 import android.widget.ToggleButton;13 /*14  * 雙按鈕15  */16 public class ToggleButton_lianxi extends Activity{17     private ImageView imageView = null;                  //圖片標籤18     private ToggleButton toggleButton = null;            //雙狀態按鈕19     20     @Override21     protected void onCreate(Bundle savedInstanceState) {22         // TODO Auto-generated method stub23         super.onCreate(savedInstanceState);24     setContentView(R.layout.togglebutton_lianxi);25     26     imageView = (ImageView) findViewById(R.id.imageView);27     toggleButton = (ToggleButton) findViewById(R.id.toggleButton);28     29     //設定按鈕響應30     toggleButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {31         32         @Override33         public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {34             // TODO Auto-generated method stub35             toggleButton.setChecked(isChecked);36             //設定圖片37             imageView.setImageResource(isChecked?R.drawable.bulb_on:R.drawable.bulb_off);38         }39     });40     41     }42         43 }
ToggleButton_lianxi.java


關於安卓開發實現雙狀態按鈕

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.