虚位以待(AD)
虚位以待(AD)
首页 > 软件编程 > Android编程 > android学习笔记18:ToggleButton

android学习笔记18:ToggleButton
类别:Android编程   作者:码皇   来源:互联网   点击:

上一篇:http: www 2cto com kf 201112 115681 htmlToggleButton也是button的一种,不同体现在togglebutton分为按下和没有按下两种不同的状态。这个程序通过使用togglebutton动态的改变了linearlayout的布局方式

上一篇:http://www.2cto.com/kf/201112/115681.html

 

ToggleButton也是button的一种,不同体现在togglebutton分为按下和没有按下两种不同的状态。这个程序通过使用togglebutton动态的改变了linearlayout的布局方式。

 



 

public class ToggleButtonTest extends Activity 

    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
        ToggleButton toggle = (ToggleButton)findViewById(R.id.toggle); 
        final LinearLayout test = (LinearLayout)findViewById(R.id.test); 
        toggle.setOnCheckedChangeListener(new OnCheckedChangeListener() 
            { 
 
                @Override 
                public void onCheckedChanged(CompoundButton arg0, boolean arg1) 
                { 
                    if(arg1) 
                    { 
                        //设置LinearLayout垂直布局  
                        test.setOrientation(1); 
                    } 
                    else 
                    { 
                        //设置LinearLayout水平布局  
                        test.setOrientation(0); 
                    }            
                } 
            }); 
    } 

public class ToggleButtonTest extends Activity
{
 @Override
 public void onCreate(Bundle savedInstanceState)
 {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  ToggleButton toggle = (ToggleButton)findViewById(R.id.toggle);
  final LinearLayout test = (LinearLayout)findViewById(R.id.test);
  toggle.setOnCheckedChangeListener(new OnCheckedChangeListener()
   {

    @Override
    public void onCheckedChanged(CompoundButton arg0, boolean arg1)
    {
     if(arg1)
     {
      //设置LinearLayout垂直布局
      test.setOrientation(1);
     }
     else
     {
      //设置LinearLayout水平布局
      test.setOrientation(0);
     }   
    }
   });
 }
}布局文件

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 
<!-- 定义一个ToggleButton按钮 --> 
<ToggleButton android:id="@+id/toggle" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textOff="横向排列" 
    android:textOn="纵向排列"  
    android:checked="true" 
    /> 
<!-- 定义一个可以动态改变方向的线性布局 --> 
  <LinearLayout android:id="@+id/test" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"  
    > 
<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> 
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 >
<!-- 定义一个ToggleButton按钮 -->
<ToggleButton android:id="@+id/toggle"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:textOff="横向排列"
 android:textOn="纵向排列"
 android:checked="true"
 />
<!-- 定义一个可以动态改变方向的线性布局 -->
  <LinearLayout android:id="@+id/test"
 android:orientation="vertical"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 >
<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>

 

摘自 hn307165411的专栏
相关热词搜索: android 学习笔记 18:ToggleButton