android開關控制項Switch和ToggleButton

來源:互聯網
上載者:User

標籤:layout   背景   .com   color   androi   end   rac   extends   line   

序:今天項目中用到了開關按鈕控制項,查閱了一些資料特地寫了這篇部落格記錄下。

1.Switch

        <Switch            android:id="@+id/bt"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:textOn="開啟"            android:layout_marginLeft="20dp"            android:textOff="關閉"            android:showText="true"            android:thumb="@drawable/shape_thum"            android:track="@drawable/select_bg_switch"            />

 

 

 

這裡layout_width:這能設定整個布局的寬度,不能設定具體的Switch的大小,需要使用switchMinWidth屬性來設定。

    thumb:文字所攜帶的背景,設定為背景色進行隱藏。不設定會出現一個背景框。

    track:設定開關的背景圖片,類似於button的background。

    textoff、texton:設定開關時的文字顯示。

 

 

2.ToggleButton

 

<ToggleButton            android:layout_width="80dp"            android:layout_height="20dp"            android:id="@+id/toggle"            android:layout_centerHorizontal="true"            android:background="@drawable/shape_track_on"            android:textOff="off"            android:textOn="on"            android:layout_marginLeft="20dp"            />

 

 

3.

 

 

 1.布局

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    android:layout_width="match_parent"    android:layout_height="match_parent">    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:gravity="center"        android:orientation="horizontal"        android:layout_marginTop="30dp"        >        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="開關"            />        <ToggleButton            android:layout_width="80dp"            android:layout_height="20dp"            android:id="@+id/toggle"            android:layout_centerHorizontal="true"            android:background="@drawable/shape_track_on"            android:textOff="off"            android:textOn="on"            android:layout_marginLeft="20dp"            />    </LinearLayout>    <LinearLayout        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:orientation="horizontal"        android:layout_centerInParent="true"        >        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="開啟狀態"            />        <Switch            android:id="@+id/bt"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:textOn="開啟"            android:layout_marginLeft="20dp"            android:textOff="關閉"            android:showText="true"            android:thumb="@drawable/shape_thum"            android:track="@drawable/select_bg_switch"            />    </LinearLayout></RelativeLayout>

 

shape_thum.xml

<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval">    <solid android:color="#0f0"/>    <size android:height="30dp" android:width="30dp"/></shape>

 

shape_track_off.xml

<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">    <corners android:radius="30dp"/>    <solid android:color="@android:color/darker_gray"/></shape>

 

shape_track_on,xml

<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">    <corners android:radius="30dp"/>    <solid android:color="@android:color/holo_red_light"/></shape>

 

select_bg_switch.xml

<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android" >    <item android:state_checked="true" android:drawable="@drawable/shape_track_on"/>    <item android:state_checked="false" android:drawable="@drawable/shape_track_off"/></selector>

 

 

Mainactivity

public class MainActivity extends AppCompatActivity {    private Switch aSwitch;    private ToggleButton toggle;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        aSwitch=findViewById(R.id.bt);        toggle=findViewById(R.id.toggle);        //狀態改變監聽        aSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {            @Override            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {                if(isChecked){                    Toast.makeText(MainActivity.this,"open",Toast.LENGTH_SHORT).show();                }else{                    Toast.makeText(MainActivity.this,"close",Toast.LENGTH_SHORT).show();                }            }        });        //狀態改變監聽        toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {            @Override            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {                if(isChecked){                    Toast.makeText(MainActivity.this,"open",Toast.LENGTH_SHORT).show();                }else{                    Toast.makeText(MainActivity.this,"close",Toast.LENGTH_SHORT).show();                }            }        });    }}

 

android開關控制項Switch和ToggleButton

相關文章

聯繫我們

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