在Android应用中实现同意游戏条款界面,实现时使用垂直线性布局管理器,并且借助Android中的TextView组件,CheckBox组件和ImageButton组件。其中,TextView组件用于显示游戏条款;CheckBox组件用来作为我同意复选框;ImageButton组件用来作为“进入”图片按钮,需要设置图片按钮默认为不显示,以及透明背景。
实现过程:
1.界面布局代码修改如下
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/background"
android:gravity="center">
<TextView
android:id="@+id/textView1"
style="@style/artclestyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxWidth="700px"
android:paddingTop="120px"
android:text="@string/artcle" />
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="@drawable/check_box"
android:text="我同意"
android:textSize="22px" />
<ImageButton
android:id="@+id/start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#0000"
android:paddingTop="30px"
android:src="@drawable/button_state"
android:visibility="invisible" >
</ImageButton>
"
</LinearLayout>
2.由于复选框默认的效果显示到实例的背景上时,看不到前面的方块,所以需要改变复选框的默认效果。首先编写Drawable资源对应的XML文件check_box.xml,用于设置复选框没有被选中时显示的图片和被选中时的图片。
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="false"
android:drawable="@drawable/check_f"/>
<item android:state_checked="true"
android:drawable="@drawable/check_t"/>
</selector>
3.在main.xml布局文件中的复选框设置android:button属性,属性值是Drawable资源
android:button="@drawable/check_box"
4.由于ImageButton组件设置背景透明后,将不再显示鼠标单击效果,所以通过Drawable资源来设置图片的android:src属性。编写一个Drawable资源对应的XML文件button_state.xm,用于设置当鼠标按下时显示的图片,以及鼠标没有按下时显示的图片。
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@drawable/start_b"/>
<item android:state_pressed="false" android:drawable="@drawable/start_a"/>
</selector>
5.为布局文件中的图片按钮设置android:src属性。
6.在res/value/strings.xml文件中添加字符串变量artcle,用于保存游戏条款
<string name="artcle">         温馨提示:本游戏适合各年龄段的玩家,请您合理安排游戏时间,不要沉迷游戏!
当您连续在线2小时间后,系统将自动结束游戏。如果同意该条款请勾选“我同意”复选框,方可进入游戏。</string>
在Android中, 表示空格
7.在MainActivity中的OnCreate()方法中,获取布局文件中添加的进入图片按钮和复选框,并且为复选框添加状态改变监听器,用于实现当复选按钮被按下时显示进入图片按钮,否则不显示。
final ImageButton imageButton=(ImageButton)findViewById(R.id.start);
CheckBox checkBox=(CheckBox)findViewById(R.id.checkBox1);
checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked)
{
imageButton.setVisibility(View.VISIBLE);
}
else
{
imageButton.setVisibility(View.INVISIBLE);
}
imageButton.invalidate();
}
});
8.为进入图片按钮添加单击事件监听器,用于实现当用户单击进入按钮时,显示一个消息提示框。
imageButton.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "进入游戏...", Toast.LENGTH_SHORT).show();
}
});
Android:padding和android:layout_margin
Padding 为内边框,指该控件内部内容,如文本/图片距离该控件的边距
Margin 为外边框,指该控件距离边父控件的边距
android:layout_margin=”10dip”
android:padding=”5dip”
当按钮分别设置以上两个属性时,得到的效果是不一样的。
android:paddingLeft=”30px”:
按钮上设置的内容(例如图片)离按钮左边边界30个像素。
android:layout_marginLeft=”30px”
整个按钮离左边设置的内容30个像素