Android金背大刀之ToggleButton之稍息立正

来源:互联网 发布:python pip是什么 编辑:程序博客网 时间:2024/04/30 00:23

传送门 ☞ 轮子的专栏 ☞ 转载请注明 ☞ http://blog.csdn.net/leverage_1229


金背大刀 

        《书剑恩仇录》老英雄周仲英:安健刚把周仲英的金背大刀递给师父。。。周仲英斜刺里窜出,拦在当路,金背大刀一立,喝道:“你这小子竟敢到铁胆庄拿人,不把老夫放在眼里,这笔帐咱们今日来算算!”张召重见他白发飘动,精神矍铄,听他言语,知是西北武林的领袖人物铁胆周仲英,不敢怠慢,挺剑疾刺。

        今天我们学习如何利用Android平台“金背大刀”ToggleButton来演示带有“开/关”功能的按钮用来控制一组普通按钮的布局方向,下面给出该情景的案例:

1案例技术要点

(1)ToggleButton布局设置
android:checked="true":默认为“打开”状态
android:textOff="@string/off":“关闭”状态显示的文本
android:textOn="@string/on":“打开”状态显示的文本
(2)为ToggleButton设置状态变化的监听OnCheckedChangeListener,并分别处理“打开”和“关闭”状态事件。
(3)layout.setOrientation(1):设置垂直布局;layout.setOrientation(0):设置水平布局。

2案例代码陈列

2.1AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.android.togglebutton"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk        android:minSdkVersion="8"        android:targetSdkVersion="15" />    <application        android:icon="@drawable/ic_launcher"        android:label="@string/app_name" >        <activity            android:name=".ToggleButtonMainActivity"            android:label="@string/app_name" >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>    </application></manifest>

2.2strings.xml

<resources>    <string name="app_name">ToggleButton表示“开/关”状态</string>    <string name="on">纵向排列</string>    <string name="off">横向排列</string>    <string name="button">按钮</string></resources>

2.3main.xml

<?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_tb"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:checked="true"        android:textOff="@string/off"        android:textOn="@string/on"        android:layout_gravity="center" />    <LinearLayout        android:id="@+id/my_ll"        xmlns:android="http://schemas.android.com/apk/res/android"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:orientation="vertical" >        <Button             android:id="@+id/button1"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="@string/button"/>        <Button             android:id="@+id/button2"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="@string/button"/>        <Button             android:id="@+id/button3"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="@string/button"/>    </LinearLayout></LinearLayout>

2.4ToggleButtonMainActivity.java

package com.android.togglebutton;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;/** * ToggleButton案例:表示“开/关”状态 * @author lynnli1229 */public class ToggleButtonMainActivity extends Activity {    private ToggleButton toggleButton;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        toggleButton = (ToggleButton) findViewById(R.id.toggle_tb);        final LinearLayout layout = (LinearLayout) findViewById(R.id.my_ll);        toggleButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {            @Override            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {                if(isChecked) {                    layout.setOrientation(1);//设置垂直布局                } else {                    layout.setOrientation(0);//设置水平布局                }            }        });    }}

3案例效果展示

 
原创粉丝点击