Android学习笔记之Switch的使用案例
来源:互联网 发布:网络打印机如何设置ip 编辑:程序博客网 时间:2024/06/15 18:50
(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="开启震动" android:textOff="关闭" 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 {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}public void onToggleClicked(View view) {/* * 强转为Switch类型的 */boolean isChecked = ((Switch) view).isChecked();if (isChecked == true) {Toast.makeText(MainActivity.this, "打开", 1).show();} else {Toast.makeText(MainActivity.this, "关闭", 1).show();}}@Overridepublic 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;}}
2 0
- Android学习笔记之Switch的使用案例
- Android学习笔记之Spinner下拉列表使用案例
- Android学习笔记之AutoCompleteTextView自动填充功能的案例
- Android学习笔记之ProgressBar案例分析
- android学习笔记之ProgressDialog的使用
- android学习笔记之RadioGroup的使用
- Android学习之笔记---WebView的使用
- Android学习之笔记---Animation的使用
- android学习笔记 之 HttpUrlconnection的使用
- android学习笔记之nofication的使用
- Android学习笔记之Retrofit2的使用
- C#之ArrayList的使用学习案例
- Android之TelephonyManager类的使用案例
- android之数据库的使用案例
- Android开发案例之单元测试的使用
- PHP Switch 语句之学习笔记
- Android学习之RatingBar,SeekBar,ToggleButton,Switch
- switch语句的穿透案例
- C++STL模板容器(一)
- 【数据结构】SJTU OJ 1237
- HttpClient的”javax.net.ssl.SSLPeerUnverifiedException: peer not authenticated”异常
- C++STL模板容器(二)
- 微信管家平台修改目录
- Android学习笔记之Switch的使用案例
- OC-沙盒
- aapt这个烂工具的一些乱七八糟的bug
- SOJ--4393: LaoB's problem1
- BroadcastReceiver那些事儿
- this指针和 *this
- android之粘滞广播
- HBase 官方文档
- Android程序进行混淆,在导出签名apk包时出错!