android开发之ToggleButton的用法实现


代码根据老罗android开发视频教程中的思路,自己敲了一下,又搞定一个控件的用法。

学到的知识:

1、ToggleButton的用法

2、LinearLayout的嵌套布局方式。

UI_ToggleButtonActivity.java源码文件:

package com.jinhoward.UI_ToggleButton;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.ToggleButton;

public class UI_ToggleButtonActivity extends Activity {
	
	private ToggleButton myToggleButton;
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_ui__toggle_button);
		
		myToggleButton=(ToggleButton)this.findViewById(R.id.togglebutton);
		final LinearLayout linearLayout = (LinearLayout)this.findViewById(R.id.buttonlayout);
		//设置监听器
		myToggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {		
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				// TODO Auto-generated method stub
				if(isChecked){
					//1表示VERTICAL纵向布局
					linearLayout.setOrientation(1);
				}else {
					//0表示VERTICAL横向布局
					linearLayout.setOrientation(0);
				}
			}
		});
		
	}


}

activity_ui__toggle_button.xml文件源代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ToggleButton
        android:id="@+id/togglebutton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="true"
        android:textOff="横向排列按钮"
        android:textOn="纵向排列按钮(默认)" >
    </ToggleButton>

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/buttonlayout"
        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="按钮1" />

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按钮2" />

        <Button
            android:id="@+id/button3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按钮3" />
    </LinearLayout>

</LinearLayout>

效果图2张如下:


          

源码下载:http://download.csdn.net/detail/jinhoward/5672075


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值