Android開關(guān)控件Switch的使用案例
在很多app的設(shè)置頁面,或者是一些功能的開關(guān)界面,我們常常用到 Switch(開關(guān)) 來展示狀態(tài),今天說說Switch控件。
(1)布局文件代碼
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Switch
android:id="@+id/switch1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="32dp"
android:layout_marginTop="94dp"
android:text="開啟震動(dòng)"
android:textOff="關(guān)閉"
android:onClick="onToggleClicked"
android:textOn="打開" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/switch1"
android:layout_alignParentTop="true"
android:layout_marginTop="26dp"
android:text="Switch的使用"
android:textSize="30dp" />
</RelativeLayout>
(2)控制的類
package com.example.android_switch;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Switch;
import android.widget.Toast;
import android.widget.ToggleButton;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onToggleClicked(View view) {
/*
* 強(qiáng)轉(zhuǎn)為Switch類型的
*/
boolean isChecked = ((Switch) view).isChecked();
if (isChecked == true) {
Toast.makeText(MainActivity.this, "打開", 1).show();
} else {
Toast.makeText(MainActivity.this, "關(guān)閉", 1).show();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
實(shí)現(xiàn)效果如下:

總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接
- Android CheckBox中設(shè)置padding無效解決辦法
- Android開發(fā)之CheckBox的簡(jiǎn)單使用與監(jiān)聽功能示例
- Android 中CheckBox多項(xiàng)選擇當(dāng)前的position信息提交的示例代碼
- Android 中CheckBox的isChecked的使用實(shí)例詳解
- Android開發(fā)手冊(cè)自定義Switch開關(guān)按鈕控件
- Android 自定義Switch開關(guān)按鈕的樣式實(shí)例詳解
- Android UI控件Switch的使用方法
- Android單選按鈕RadioButton的使用方法
- Android復(fù)選框CheckBox與開關(guān)按鈕Switch及單選按鈕RadioButton使用示例詳解
相關(guān)文章
Android中多個(gè)ContentProvider的初始化順序詳解
在日常Android開發(fā)中經(jīng)常會(huì)寫一些sdk來供他人或者自己調(diào)用,一般這些sdk都涉及到初始化,下面這篇文章主要給大家介紹了關(guān)于Android中多個(gè)ContentProvider的初始化順序的相關(guān)資料,需要的朋友可以參考下2022-04-04
Android自定義View實(shí)現(xiàn)鐘擺效果進(jìn)度條PendulumView
這篇文章主要介紹了Android自定義View實(shí)現(xiàn)鐘擺效果進(jìn)度條PendulumView,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09
Android引導(dǎo)頁面的簡(jiǎn)單實(shí)現(xiàn)
這篇文章主要為大家詳細(xì)介紹了Android引導(dǎo)頁面的簡(jiǎn)單實(shí)現(xiàn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-02-02
Android實(shí)現(xiàn)雙向滑動(dòng)特效的實(shí)例代碼
這篇文章主要介紹了Android實(shí)現(xiàn)雙向滑動(dòng)特效的實(shí)例代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,一起跟隨小編過來看看吧2018-05-05
android使用webwiew載入頁面使用示例(Hybrid App開發(fā))
Hybrid App 融合 Web App 的原理就是嵌入一個(gè)WebView組件,可以在這個(gè)組件中載入頁面,相當(dāng)于內(nèi)嵌的瀏覽器,下面是使用示例2014-03-03
Android 6.0開發(fā)實(shí)現(xiàn)關(guān)機(jī)菜單添加重啟按鈕的方法
這篇文章主要介紹了Android 6.0開發(fā)實(shí)現(xiàn)關(guān)機(jī)菜單添加重啟按鈕的方法,涉及Android6.0針對(duì)相關(guān)源碼的修改與功能添加操作技巧,需要的朋友可以參考下2017-09-09
利用Kotlin的協(xié)程實(shí)現(xiàn)簡(jiǎn)單的異步加載詳解
這篇文章主要給大家介紹了關(guān)于利用Kotlin的協(xié)程實(shí)現(xiàn)簡(jiǎn)單的異步加載的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2018-03-03
Android自定義View實(shí)現(xiàn)遙控器按鈕
這篇文章主要為大家詳細(xì)介紹了Android自定義View實(shí)現(xiàn)遙控器按鈕,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08
Android實(shí)現(xiàn)歌詞滾動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)歌詞滾動(dòng)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-11-11

