Android成長之路-Button、ImageButton、ToggleButton按鈕的功能和用法

來源:互聯網
上載者:User

ToggleButton按鈕的功能和用法:(實現動態控制布局)

定義一個layout布局檔案:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <ToggleButton        android:id="@+id/toggle"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:checked="true"        android:textOn="縱向排列"        android:textOff="橫向排列" />    <!-- android:checked  如果是true,運行時顯示的是 android:textOn 的值                          如果是false,運行是顯示的是 android:textOff 的值                                              所以:            1.如果checked=true 且 textOn=縱向排列                 那麼  下面的orientation="vertical"              如果checked=false                  那麼  下面的orientation="horizontal"            2.如果checked=true 且 textOn=橫向排列                 那麼  下面的orientation="horizontal"              如果checked=false                  那麼  下面的orientation="vertical"                                                                                                    就像語文課文中說的:上下文要匹配     -->    <LinearLayout        android:id="@+id/test"        android:layout_width="fill_parent"        android:layout_height="fill_parent"        android:orientation="vertical" >        <Button            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="按鈕一" />        <Button            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="按鈕二" />        <Button            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="按鈕三" />    </LinearLayout></LinearLayout>

 

 

寫一個activity檔案實現功能:

package cn.csdn.activity;import android.app.Activity;import android.os.Bundle;import android.widget.CompoundButton;import android.widget.CompoundButton.OnCheckedChangeListener;import android.widget.LinearLayout;import android.widget.ToggleButton;public class ToggleButtonActivity extends Activity{@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.state_layout);ToggleButton toggle = (ToggleButton) this.findViewById(R.id.toggle);final LinearLayout test = (LinearLayout) this.findViewById(R.id.test);toggle.setOnCheckedChangeListener(new OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {/**傳一個布爾型的值,來判斷是true還是false,然後區分是縱向還是橫向 * ToggleButton按鈕是在不斷的按下時true和false相互切換的,也就是xml定義時的checked的值*/if(isChecked){//true時test.setOrientation(1);// 1 的話,設定為縱向}else{//false時test.setOrientation(0);// 0 的話,設定為橫向}}});}}

 

 

點擊按鈕切換布局

 

 

 

Button和ImageButton按鈕的用法:

定義一個布局:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <!-- 普通文字按鈕 -->    <Button         android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="普通文字按鈕"        />        <!-- 普通圖片按鈕 -->    <ImageButton         android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:src="@drawable/play"        />        <!-- 帶有圖片的文字按鈕 -->    <Button         android:id="@+id/button"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:background="@drawable/stop"        android:text="圖片文字按鈕"        />    <!-- 按下時顯示不同圖片的按鈕          android:src="@drawable/button_selector" 引用一個Drawable資源    -->    <ImageButton         android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:src="@drawable/button_selector"        />    </LinearLayout>

 

 

定義Drawable資源:(實現圖片按鈕按下和鬆開時顯示不同圖片)

<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android" >    <!-- 指定按鈕按下時候顯示的圖片 -->    <item    android:state_pressed="true"    android:drawable="@drawable/pic"    />        <!-- 指定按鈕鬆開時候顯示的圖片 -->    <item    android:state_pressed="false"    android:drawable="@drawable/ic_launcher"    /></selector>

 

 

是第四個按鈕在按下時的狀態:

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.