提不起兴趣做作业的话将来也提不起兴趣做项目需求。
所以,不要畏难,不要懒。
- 制作两组红黄绿交通灯,分别通过状态开关可打开和关闭交通灯
- 在打开状态下,设置单选按钮可设置红、黄、绿灯之间的切换时间,这个对全局有效
- 通过多选按钮来设置是否可打开第一组还是打开第二组交通灯的权限,只有当选择为打开时才允许通过状态开关打开交通灯,反之则不允许操作
拖几个checkbox拖到现在,我很惭愧。
不过终于对eclipse的可视化界面更熟悉了,右边像chrome developer tools一样的属性列表以及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" android:orientation="vertical" 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="com.narumi.android_8_lights.MainActivity"> <ImageView android:id="@+id/imageView2" android:layout_width="115dp" android:layout_height="68dp" android:layout_gravity="left" android:src="@drawable/none" /> <ImageView android:id="@+id/imageView1" android:layout_width="117dp" android:layout_height="68dp" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:src="@drawable/none" /> <CheckBox android:id="@+id/checkBox1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="95dp" android:layout_marginLeft="10dp" android:text="左灯" /> <RadioGroup android:id="@+id/group1" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_alignParentLeft="true" android:layout_alignRight="@+id/imageView2" android:layout_alignTop="@+id/group2" android:layout_marginLeft="10dp" android:orientation="horizontal" > <RadioButton android:id="@+id/radioButton1" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <RadioButton android:id="@+id/radioButton2" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <RadioButton android:id="@+id/radioButton3" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </RadioGroup> <RadioGroup android:id="@+id/group2" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_alignLeft="@+id/checkBox2" android:layout_alignParentRight="true" android:layout_below="@+id/imageView1" android:layout_marginRight="10dp" android:orientation="horizontal" > <RadioButton android:id="@+id/radioButton6" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <RadioButton android:id="@+id/radioButton5" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <RadioButton android:id="@+id/radioButton4" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </RadioGroup> <CheckBox android:id="@+id/checkBox2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/checkBox1" android:layout_alignBottom="@+id/checkBox1" android:layout_alignLeft="@+id/imageView1" android:layout_marginLeft="10dp" android:text="右灯" /> </RelativeLayout>
四张图片
在java文件中处理好事件,要注意几点
- Java中声明类的代码块中只能写声明变量,不能写赋值语句,赋值语句请写在方法内部。。
- Java中需要数组可以使用ArrayList,它不是用push方法,而是用add,取出来用get,取的时候要类型转换,也作类型检查,所以我取出来的是RadioButton就转成RadioButton
- 监听CheckdChange的时候,打了断点就会看出,同一组的radio中,一次点击会触发两次事件,一个是选中的,一个取消原来选中的,这样就会有两个isChecked值
- Java中for循环的写法
-
int[] integers = { 0, 1, 2 }这就是最基本的定义数组的方法,定义了一个int类型的数组叫integers
- Java中final类型的变量可以在内类中访问到
package com.narumi.android_8_lights; import java.util.ArrayList; import android.app.Activity; import android.os.Bundle; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.ImageView; import android.widget.CompoundButton.OnCheckedChangeListener; import android.widget.RadioButton; public class MainActivity extends Activity { boolean flagL = false; boolean flagR = false; ArrayList<RadioButton> arr = new ArrayList<RadioButton>(); ArrayList<RadioButton> brr = new ArrayList<RadioButton>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); final RadioButton r1 = (RadioButton) findViewById(R.id.radioButton1); final RadioButton r2 = (RadioButton) findViewById(R.id.radioButton2); final RadioButton r3 = (RadioButton) findViewById(R.id.radioButton3); arr.add(r1); arr.add(r2); arr.add(r3); CheckBox cleft = (CheckBox) findViewById(R.id.checkBox1); cleft.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { // TODO Auto-generated method stub flagL = isChecked; if (isChecked != true) { int[] integers = { 0, 1, 2 }; for (int i : integers) { ((RadioButton) arr.get(i)).setChecked(false); } ImageView img2 = (ImageView) findViewById(R.id.imageView2); img2.setImageResource(R.drawable.none); } } }); final RadioButton r4 = (RadioButton) findViewById(R.id.radioButton4); final RadioButton r5 = (RadioButton) findViewById(R.id.radioButton5); final RadioButton r6 = (RadioButton) findViewById(R.id.radioButton6); brr.add(r4); brr.add(r5); brr.add(r6); CheckBox cright = (CheckBox) findViewById(R.id.checkBox2); cright.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { // TODO Auto-generated method stub flagR = isChecked; if (isChecked != true) { int[] integers = { 0, 1, 2 }; for (int i : integers) { ((RadioButton) brr.get(i)).setChecked(false); } ImageView img1 = (ImageView) findViewById(R.id.imageView1); img1.setImageResource(R.drawable.none); } } }); r1.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { // TODO Auto-generated method stub if (flagL == true && isChecked == true) { lightRadio(true, 1); } else{ r1.setChecked(false); } } }); r2.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { // TODO Auto-generated method stub if (flagL == true && isChecked == true) { lightRadio(true, 2); } else{ r2.setChecked(false); } } }); r3.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { // TODO Auto-generated method stub if (flagL == true && isChecked == true) { lightRadio(true, 3); } else{ r3.setChecked(false); } } }); r4.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { // TODO Auto-generated method stub if (flagR == true && isChecked == true) { lightRadio(true, 4); } else{ r4.setChecked(false); } } }); r5.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { // TODO Auto-generated method stub if (flagR == true && isChecked == true) { lightRadio(true, 5); } else{ r5.setChecked(false); } } }); r6.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { // TODO Auto-generated method stub if (flagR == true && isChecked == true) { lightRadio(true, 6); } else{ r6.setChecked(false); } } }); } public void lightRadio(boolean side, int color) { final ImageView img1 = (ImageView) findViewById(R.id.imageView1); final ImageView img2 = (ImageView) findViewById(R.id.imageView2); switch (color) { case 1: img2.setImageResource(R.drawable.red); break; case 2: img2.setImageResource(R.drawable.yellow); break; case 3: img2.setImageResource(R.drawable.green); break; case 4: img1.setImageResource(R.drawable.green); break; case 5: img1.setImageResource(R.drawable.yellow); break; case 6: img1.setImageResource(R.drawable.red); break; default: break; } } }
此例结束了。