zoukankan      html  css  js  c++  java
  • android中ToggleButton的使用

    这个小例子主要用来演示ToggleButton的基本使用。效果大致是一开始界面是垂直布局的,当点击ToggleButton按钮的时候,布局变为水平方向的

    大致的代码贴一下吧,其中main.xml代码如下:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
    
        <ToggleButton
            android:id="@+id/toggleButton1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:textOff="横向排列"
            android:textOn="纵向排列" />
    
        <LinearLayout
            android:id="@+id/lLayout"
            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="Button" />
    
            <Button
                android:id="@+id/button2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Button" />
    
            <Button
                android:id="@+id/button3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Button" />
        </LinearLayout>
    
    
    </LinearLayout>
    

      AndroidDemo4Activity.java代码如下:

    package android.demo;
    
    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 AndroidDemo4Activity extends Activity {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            ToggleButton toggle=(ToggleButton)findViewById(R.id.toggleButton1);
            final LinearLayout layout=(LinearLayout)findViewById(R.id.lLayout);
            toggle.setOnCheckedChangeListener(new OnCheckedChangeListener() {
    			
    			@Override
    			public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
    				if(arg1){
    					//设置垂直布局
    					layout.setOrientation(1);
    				}else{
    					//设置水平布局
    					layout.setOrientation(0);
    				}
    				
    			}
    		});
        }
    }
    

      

  • 相关阅读:
    经典算法之七大排序
    Memcached在Asp.NET中的使用
    利用memcached构建高性能的Web应用程序(转)
    C# 反射机制
    数据库事务设置保存点
    C# 串口通信总结
    某投注网站的BUG
    浙江电信网上营业厅的一个BUG(有更新)
    解决DESCryptoServiceProvider加解密时弱密钥异常
    ASP.NET自定义控件开发示例(二)
  • 原文地址:https://www.cnblogs.com/rollenholt/p/2506021.html
Copyright © 2011-2022 走看看