安卓基本控件之Switch
2015-09-11 20:09
232 查看
布局文件
<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>
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;
}
}
<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;
}
}
相关文章推荐
- 安卓基本控件之RadioButton
- 安卓基本控件之TextView
- 安卓基本控件之ToggleButton
- 数据结构与算法(C#版)第二章 C#语言与面向对象技术(中)V1.0
- 第八篇:编译器特性:关键字property与synthesize用法
- 《剑指offer》二叉搜索树与双向链表
- Javascript数据类型
- 画图-CCF测试往届题
- JBOSS简易安装部署文档
- .onion 被批准为专用域名
- Apache POI组件操作Excel,制作报表(一)
- 为什么在C++使用pthread_create()的时候,类成员函数做线程的处理函数必须要定义成static类型的?
- echarts初探:了解模块化
- Wireshark非标准分析port无流量
- Xdebug+phpstorm配置
- nyoj-22 素数求和问题【水】
- Linux学习笔记<基础概念>
- 阿里巴巴消息中间件团队消息和分布式数据层负责人王晶昱:消息系统架构与变迁
- 用程序实现自动的html操作
- 下拉菜单