android學習筆記18:ToggleButton

來源:互聯網
上載者:User

上一篇:http://www.bkjia.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的專欄

聯繫我們

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