android中的控件可谓应有尽有,从简单的Button到复杂的WebView,无所不能,无所不有。

在开发的早期阶段,这些控件应该完全能够满足开发者的要求.随着知识的不断深入,开发者完全可以根据自己的需要,自定义控件.这篇博文主要和大家介绍一下android内置的这些控件的简单用法.

本案例通过一个注册界面尽可能多的为大家展示一些控件的使用。本案例涉及的控件有:TextView、EditText、RadioButton、Button、ToggleButton、CheckBox、Spinner等。

本案例整体的布局结构如下图:

注册成功后的信息显示如下图:

下面就布局的方法,控件的声明以及使用和大家做一个简单的介绍:

主界面中MainActivity.java中main.xml的代码如下:

ps:这里和大家讲一下android:layout_gravity="center_vertical"android:gravity="center"的区别:

前者是控件本身在布局中居中,而后者是控件中的文本在控件中居中.注意两者的区别.

以下就是详细的代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width
="fill_parent"
android:layout_height
="fill_parent"
>
<TextView
android:id="@+id/tvTitleRegister"
android:layout_width
="fill_parent"
android:layout_height
="wrap_content"
android:text
="用户注册界面"
android:textColor
="#EEE"
android:textStyle
="bold"
android:textSize
="28sp"
android:layout_gravity
="center_vertical"
android:gravity
="center"
/>
<TextView
android:id="@+id/tvUserName"
style
="@style/TextViewTitleWidgetStyle"
android:text
="用户名称:"
android:layout_below
="@+id/tvTitleRegister"
android:layout_alignParentLeft
="true"
android:layout_marginTop
="8dp"
/>
<EditText
android:id="@+id/etUserName"
style
="@style/EditTextInputWidgetStyle"
android:layout_toRightOf
="@+id/tvUserName"
android:layout_alignParentRight
="true"
android:layout_below
="@+id/tvTitleRegister"
android:layout_marginLeft
="2dp"
android:layout_marginRight
="2dp"
/>
<TextView
android:id="@+id/tvUserPassword"
style
="@style/TextViewTitleWidgetStyle"
android:text
="用户密码:"
android:layout_below
="@+id/tvUserName"
android:layout_alignParentLeft
="true"
android:layout_marginTop
="26dp"
/>
<EditText
android:id="@+id/etUserPassword"
style
="@style/EditTextInputWidgetStyle"
android:layout_toRightOf
="@+id/tvUserPassword"
android:layout_alignParentRight
="true"
android:layout_below
="@+id/etUserName"
android:layout_marginLeft
="2dp"
android:layout_marginRight
="2dp"
android:layout_marginTop
="5dp"
/>
<TextView
android:id="@+id/tvSex"
style
="@style/TextViewTitleWidgetStyle"
android:text
="性 别:"
android:layout_below
="@+id/tvUserPassword"
android:layout_alignParentLeft
="true"
android:layout_marginTop
="25dp"
/>
<RadioGroup
android:id="@+id/radioSexGroup"
android:layout_height
="wrap_content"
android:layout_width
="wrap_content"
android:layout_alignParentRight
="true"
android:layout_below
="@+id/etUserPassword"
android:layout_marginLeft
="2dp"
android:layout_marginRight
="2dp"
android:layout_toRightOf
="@+id/tvSex"
android:orientation
="horizontal"
>
<RadioButton
android:id="@+id/radioMale"
android:text
="男"
android:layout_height
="wrap_content"
android:layout_width
="wrap_content"
android:layout_marginLeft
="5dp"
/>
<RadioButton
android:id="@+id/radioFemale"
android:text
="女"
android:layout_height
="wrap_content"
android:layout_width
="wrap_content"
android:layout_marginLeft
="15dp"
/>
</RadioGroup>
<TextView
android:id="@+id/tvMarriage"
style
="@style/TextViewTitleWidgetStyle"
android:text
="婚 否:"
android:layout_below
="@+id/tvSex"
android:layout_alignParentLeft
="true"
android:layout_marginTop
="20dp"
/>
<ToggleButton
android:id="@+id/tbIsGetMarried"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content"
android:layout_toRightOf
="@+id/tvMarriage"
android:layout_below
="@+id/radioSexGroup"
android:layout_marginLeft
="5dp"
/>
<TextView
android:id="@+id/tvhobbies"
style
="@style/TextViewTitleWidgetStyle"
android:text
="爱 好:"
android:layout_below
="@+id/tvMarriage"
android:layout_alignParentLeft
="true"
android:layout_marginTop
="20dp"
/>
<CheckBox
android:id="@+id/cb_basketball"
style
="@style/CheckBoxWidgetStyle"
android:layout_below
="@+id/tbIsGetMarried"
android:layout_toRightOf
="@+id/tvhobbies"
android:text
="篮球"
android:layout_marginLeft
="5dp"
/>
<CheckBox
android:id="@+id/cb_Reading"
style
="@style/CheckBoxWidgetStyle"
android:layout_below
="@+id/tbIsGetMarried"
android:layout_toRightOf
="@+id/cb_basketball"
android:text
="阅读"
android:layout_marginLeft
="5dp"
/>
<CheckBox
android:id="@+id/cb_pc"
style
="@style/CheckBoxWidgetStyle"
android:layout_below
="@+id/cb_basketball"
android:layout_toRightOf
="@+id/tvhobbies"
android:text
="电脑"
android:layout_marginLeft
="5dp"
/>
<CheckBox
android:id="@+id/cb_Sing"
style
="@style/CheckBoxWidgetStyle"
android:layout_below
="@+id/cb_Reading"
android:layout_toRightOf
="@+id/cb_pc"
android:text
="唱歌"
android:layout_marginLeft
="5dp"
/>
<TextView
android:id="@+id/tvTitle"
style
="@style/TextViewTitleWidgetStyle"
android:text
="职 务:"
android:layout_below
="@+id/tvhobbies"
android:layout_alignParentLeft
="true"
android:layout_marginTop
="60dp"
/>
<Spinner
android:id="@+id/position"
android:layout_width
="wrap_content"
android:layout_height
="wrap_content"
android:layout_toRightOf
="@+id/tvTitle"
android:layout_marginLeft
="5dp"
android:layout_below
="@+id/cb_Sing"
/>
<Button
android:id="@+id/bt_OK"
android:layout_width
="80dp"
android:layout_height
="wrap_content"
android:layout_alignParentBottom
="true"
android:text
="注册"
android:layout_marginLeft
="65dp"
/>
<Button
android:id="@+id/bt_Cancel"
android:layout_width
="80dp"
android:layout_height
="wrap_content"
android:layout_alignParentBottom
="true"
android:layout_toRightOf
="@+id/bt_OK"
android:text
="取消"
android:layout_marginLeft
="5dp"
/>
</RelativeLayout>

values/style.xml文件

这里建议一下大家,以后尽量将一些相同控件的通用属性集中起来放在style.xml文件中。这样做有两个好处:

第一:提高代码复用率     第二:便于后期的维护

以下是style.xml文件的详细代码:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<!--TextView-->
<style name="TextViewTitleWidgetStyle">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textColor">#EEE</item>
<item name="android:textStyle">bold</item>
<item name="android:textSize">18sp</item>
</style>

<!--EditText-->
<style name="EditTextInputWidgetStyle">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:lines">1</item>
</style>

<!--CheckBox-->
<style name="CheckBoxWidgetStyle">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
</style>
</resources>

在MainActivity中各个控件的使用情况如下:

#1.往Spinner中添加值,需要一个String数组和一个Spinner的对象

Spinner中选取的值的获取:b.putString("position",position.getSelectedItem().toString());

public void setValuesForSpinner(String[] str,Spinner p) {
ArrayAdapter<Object> aa_spinner = new ArrayAdapter<Object> (this, android.R.layout.simple_spinner_item, str);
p.setAdapter(aa_spinner);
}

#2.radiobutton的取值,只需要判断isChecked(),两个RadioButton放在RadioGroup中,单选

if(radioMale.isChecked()) {
b.putString("gender", "性别: 男");
} else {
b.putString("gender", "性别: 女");
}

#3.CheckBox的取值

String temp = "爱好:";
if(cb_basketball.isChecked()) {
temp+="篮球";
}
if(cb_Reading.isChecked()) {
temp+=" ";
temp+="阅读";
}
if(cb_pc.isChecked()) {
temp+=" ";
temp+="电脑";
}
if(cb_Sing.isChecked()) {
temp+=" ";
temp+="唱歌";
}
b.putString("hobby", temp);

#4.ToggleButton的取值

if(tbIsGetMarried.isChecked()) {
b.putString("marriged", "婚否:已婚");
} else {
b.putString("marriged", "婚否:未婚");
}

最后只需要将Bundle的对象通过Intent发送到另一个Activity列表化显示即可.

 

2011.10.19.pm

jack.li

QQ:523072842
Email:523072842@qq.com

posted on 2011-10-19 17:12  jack.li  阅读(1929)  评论(0编辑  收藏  举报