安卓基本控件之Switch
来源:互联网 发布:postman ubuntu 编辑:程序博客网 时间:2024/06/05 03:22
布局文件
<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:checked="true" 默认选中状态
Switch 4.0之后使用
-->
<Switch
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn="开"
android:textOff="关"
android:checked="true"
android:onClick="OnclickSwitch"
/>
<ImageView
android:id="@+id/iv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/bg1"
android:layout_centerInParent="true"
/>
</RelativeLayout>
Java代码
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.ImageView;
import android.widget.Switch;
public class MainActivity extends Activity {
//声明控件
private ImageView iv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//通过findViewById 找出相应的控件
iv = (ImageView) findViewById(R.id.iv);
}
//switch的点击事件
public void OnclickSwitch(View v){
Switch switch1 = (Switch) v;
boolean isChecked = switch1.isChecked();
if(isChecked){
iv.setImageResource(R.drawable.bg1);
}else{
iv.setImageResource(R.drawable.bg);
}
}
@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;
}
}
- 安卓基本控件之Switch
- 安卓基本控件之Button
- 安卓基本控件之CheckBox
- 安卓基本控件之EditText
- 安卓基本控件之ImageButton
- 安卓基本控件之ImageView
- 安卓基本控件之ProgressBar
- 安卓基本控件之RadioButton
- 安卓基本控件之ToggleButton
- 安卓基本控件之TextView
- 安卓基本控件
- 安卓控件——ToggleButton & Switch
- 安卓入门一之基本控件使用
- ios switch 控件基本操作
- Android控件之Switch
- 初始安卓基本控件_button
- 初始安卓基本控件_TextView
- 初识安卓基本控件_ImageView
- 软件安全学习笔记(7):PE文件总体格式
- 【java】异常处理
- 菜鸟系列——康托展开
- 《剑指offer》二叉搜索树与双向链表
- Angularjs ng-table的使用(可以加button)以及angularjs学习资料
- 安卓基本控件之Switch
- 已知明文破解密码
- 安装NAGIOS
- python 爬虫4 一些编程注意项目
- 技巧:Linux 动态库与静态库制作及使用详解
- iBarn基于PHP MYSQL开源网盘 - 堪比百度网盘
- 网络问题简单排查
- 安卓基本控件之ToggleButton
- iOS开发之 UIAlertView 沙盒 归档