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 EditText默認(rèn)不彈出輸入法的實(shí)現(xiàn)方法
下面小編就為大家分享一篇Android EditText默認(rèn)不彈出輸入法的實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-01-01
Android組件TabHost實(shí)現(xiàn)頁面中多個(gè)選項(xiàng)卡切換效果
這篇文章主要為大家詳細(xì)介紹了Android組件TabHost實(shí)現(xiàn)頁面中多個(gè)選項(xiàng)卡切換效果的相關(guān)資料,感興趣的小伙伴們可以參考一下2016-05-05
Android開發(fā)實(shí)現(xiàn)圖片圓角的方法
這篇文章主要介紹了Android開發(fā)實(shí)現(xiàn)圖片圓角的方法,涉及Android針對(duì)圖形圖像的相關(guān)操作技巧,需要的朋友可以參考下2016-10-10
Android判斷11位手機(jī)號(hào)碼的方法(正則表達(dá)式)
項(xiàng)目里頭需要做一個(gè)判斷用戶輸入的號(hào)碼是否是正確的手機(jī)號(hào)碼,正確的手機(jī)號(hào)碼應(yīng)該是11位的,這里我們需要用一個(gè)正則表達(dá)式來進(jìn)行判斷,下面我把寫法分享給大家2016-12-12
Android自定義dialog 自下往上彈出的實(shí)例代碼
本文通過實(shí)例代碼給大家介紹了Android自定義dialog 自下往上彈出效果,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2018-08-08

