大家好,又见面了,我是你们的朋友全栈君。
android中的控件可谓应有尽有,从简单的Button到复杂的WebView,无所不能,无所不有。
在开发的早期阶段,这些控件应该完全能够满足开发者的要求.随着知识的不断深入,开发者完全可以根据自己的需要,自定义控件.这篇博文主要和大家介绍一下android内置的这些控件的简单用法.
本案例通过一个注册界面尽可能多的为大家展示一些控件的使用。本案例涉及的控件有:TextView、EditText、RadioButton、Button、ToggleButton、CheckBox、Spinner等。
本案例整体的布局结构如下图:
注册成功后的信息显示如下图:
下面就布局的方法,控件的声明以及使用和大家做一个简单的介绍:
主界面中MainActivity.java中main.xml的代码如下:
ps:这里和大家讲一下android:layout_gravity=”center_vertical”和android:gravity=”center”的区别:
前者是控件本身在布局中居中,而后者是控件中的文本在控件中居中.注意两者的区别.
以下就是详细的代码:
代码语言:javascript代码运行次数:0运行复制
android:layout_width="fill_parent" android:layout_height="fill_parent" > 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" /> android:id="@+id/tvUserName" style="@style/TextViewTitleWidgetStyle" android:text="用户名称:" android:layout_below="@+id/tvTitleRegister" android:layout_alignParentLeft="true" android:layout_marginTop="8dp" /> 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" /> android:id="@+id/tvUserPassword" style="@style/TextViewTitleWidgetStyle" android:text="用户密码:" android:layout_below="@+id/tvUserName" android:layout_alignParentLeft="true" android:layout_marginTop="26dp" /> 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" /> android:id="@+id/tvSex" style="@style/TextViewTitleWidgetStyle" android:text="性 别:" android:layout_below="@+id/tvUserPassword" android:layout_alignParentLeft="true" android:layout_marginTop="25dp" /> 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" > android:id="@+id/radioMale" android:text="男" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_marginLeft="5dp" /> android:id="@+id/radioFemale" android:text="女" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_marginLeft="15dp" /> android:id="@+id/tvMarriage" style="@style/TextViewTitleWidgetStyle" android:text="婚 否:" android:layout_below="@+id/tvSex" android:layout_alignParentLeft="true" android:layout_marginTop="20dp" /> 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" /> android:id="@+id/tvhobbies" style="@style/TextViewTitleWidgetStyle" android:text="爱 好:" android:layout_below="@+id/tvMarriage" android:layout_alignParentLeft="true" android:layout_marginTop="20dp" /> android:id="@+id/cb_basketball" style="@style/CheckBoxWidgetStyle" android:layout_below="@+id/tbIsGetMarried" android:layout_toRightOf="@+id/tvhobbies" android:text="篮球" android:layout_marginLeft="5dp" /> 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" /> 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" /> 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" /> android:id="@+id/tvTitle" style="@style/TextViewTitleWidgetStyle" android:text="职 务:" android:layout_below="@+id/tvhobbies" android:layout_alignParentLeft="true" android:layout_marginTop="60dp" /> 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" />
这里建议一下大家,以后尽量将一些相同控件的通用属性集中起来放在style.xml文件中。这样做有两个好处:
第一:提高代码复用率 第二:便于后期的维护
以下是style.xml文件的详细代码:
代码语言:javascript代码运行次数:0运行复制
在MainActivity中各个控件的使用情况如下:
#1.往Spinner中添加值,需要一个String数组和一个Spinner的对象
Spinner中选取的值的获取:b.putString(“position”,position.getSelectedItem().toString());
代码语言:javascript代码运行次数:0运行复制public void setValuesForSpinner(String[] str,Spinner p) {
ArrayAdapter
p.setAdapter(aa_spinner);
}#2.radiobutton的取值,只需要判断isChecked(),两个RadioButton放在RadioGroup中,单选
代码语言:javascript代码运行次数:0运行复制if(radioMale.isChecked()) {
b.putString("gender", "性别: 男");
} else {
b.putString("gender", "性别: 女");
}#3.CheckBox的取值
代码语言:javascript代码运行次数:0运行复制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的取值
代码语言:javascript代码运行次数:0运行复制if(tbIsGetMarried.isChecked()) {
b.putString("marriged", "婚否:已婚");
} else {
b.putString("marriged", "婚否:未婚");
}最后只需要将Bundle的对象通过Intent发送到另一个Activity列表化显示即可.
2011.10.19.pm
jack.li
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/155141.html原文链接:https://javaforall.cn