初识安卓小程序(开关灯——单选多选按钮控制)
2014-05-09 20:43
169 查看
如图:
点击单选按钮"开灯",多选按钮就会显示"关灯"且方块里有对勾;反之,点多选按钮,单选按钮也自动改变。
首先,先创建一个安卓项目(我的版本是4.4.2的),名字为"bulb",把两张图片:开灯与关灯状态的图片放入"drawable-"随意一个文件夹下
然后在res文件夹下找到layout文件夹,找到activity_main.xml或fragment_main.xml,在里面输入或拖拽按钮
最后在src下的java文件里MainActivity.java
效果自己检验吧!
点击单选按钮"开灯",多选按钮就会显示"关灯"且方块里有对勾;反之,点多选按钮,单选按钮也自动改变。
首先,先创建一个安卓项目(我的版本是4.4.2的),名字为"bulb",把两张图片:开灯与关灯状态的图片放入"drawable-"随意一个文件夹下
然后在res文件夹下找到layout文件夹,找到activity_main.xml或fragment_main.xml,在里面输入或拖拽按钮
<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" tools:context=".MainActivity" > <ImageView android:id="@+id/image" android:layout_width="120dp" android:layout_height="120dp" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:src="@drawable/off" /> <RadioGroup android:id="@+id/radioGroup1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignRight="@+id/image" android:layout_below="@+id/image" android:layout_marginRight="35dp" android:layout_marginTop="20dp" android:orientation="horizontal" > <RadioButton android:id="@+id/on" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="开灯" /> <RadioButton android:id="@+id/off" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="true" android:text="关灯" /> </RadioGroup> <CheckBox android:id="@+id/checkBulb11" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/radioGroup1" android:layout_centerHorizontal="true" android:layout_marginTop="34dp" android:text="开灯" /> </RelativeLayout>
最后在src下的java文件里MainActivity.java
package com.example.bulb; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.CompoundButton.OnCheckedChangeListener; import android.widget.ImageView; import android.widget.RadioButton; public class MainActivity extends Activity implements OnCheckedChangeListener{ private ImageView image; private RadioButton on,off; private CheckBox checkBulb; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.fragment_main); image=(ImageView) this.findViewById(R.id.image); on=(RadioButton) this.findViewById(R.id.on); off=(RadioButton) this.findViewById(R.id.off); checkBulb=(CheckBox) this.findViewById(R.id.checkBulb11); on.setOnCheckedChangeListener(this); checkBulb.setOnCheckedChangeListener(this); } @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; } public void setBulbState(boolean state){ if(state==true){ //改变图片 image.setImageResource(R.drawable.on); //改变checkbox文本 checkBulb.setText("关灯"); }else{ //改变图片 image.setImageResource(R.drawable.off); //改变checkbox文本 checkBulb.setText("开灯"); } //改变radiobutton状态 on.setChecked(state); off.setChecked(!state); //改变chackbox的状态 checkBulb.setChecked(state); } @Override public void onCheckedChanged(CompoundButton arg0, boolean arg1) { setBulbState(arg1); } }
效果自己检验吧!
相关文章推荐
- 自动链接、自动完成输入框、开关按钮和单选、多选按钮的简单使用
- JS控制根据所传数据选中多选、单选按钮
- android学习总结(16.08.29)单选按钮(RadioButton),多选按钮(CheckBox)和开关按钮(Switch)
- Android程序开发中单选按钮(RadioGroup)的使用详解
- 如何在程序里控制按钮的不可见?
- android中的单选和多选按钮的使用
- Android之复选按钮、单选按钮、开关按钮
- 获取单选按钮方法不对导致程序闪退
- iOS - (几个 button 按钮之间的单选与多选)
- cocos2d-x JS 复选按钮checkBox的单选与多选
- 解决datalist中单选按钮可以多选的问题(Asp.Net)
- Jquery模拟单选按钮与多选按钮
- 单选按钮和多选按钮的样式设置
- 在cocos2d里面如何制作按钮:简单按钮、单选按钮和开关按钮
- 我收集的一些资源III:开关状态按钮、万年历程序、自定义圆角动画按钮、通讯录
- 单选按钮、多选按钮用图片实现
- 解决datalist中单选按钮可以多选的问题(Asp.Net)
- 使用按钮控制HTML5背景音乐开关
- 微信小程序--单选复选按钮组的实现
- 通过单选按钮控制页面之间的切换