android之常用控件大集合

android之常用控件大集合android中的控件可谓应有尽有,从简单的Button到复杂的WebView,无所不能,无所不有。在开发的早期阶段,这些控件应该完全能够满足开发者的要求.随着知识的不断深入,开发者完全可以根据自己的

大家好,又见面了,我是你们的朋友全栈君。

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

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

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

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

<span role="heading" aria-level="2">android之常用控件大集合

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

<span role="heading" aria-level="2">android之常用控件大集合

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

主界面中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

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/155141.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Centos 7安装nginx并配置https[通俗易懂]

    Centos 7安装nginx并配置https[通俗易懂]1.更新yum源yumupdate2.安装nginx的依赖环境yuminstall-ygcc-c++pcrepcre-develzlibzlib-developensslopenssl-develgcc-c++:安装nginx需要先将官网下载的源码进行编译,编译依赖gcc环境。pcrepcre-devel:PCRE(PerlCompatible…

    2022年5月26日
    43
  • Eclipse配置tomcat[通俗易懂]

    1.打开Eclipse,Window–>Preference–>Server–>RuntimeEnvironments2.点击Add,选择相应的tomcat版本,本文以tomcat-8.0.53为例3.选择ApacheTomcatv8.0,点击Next4.配置好Tomcat名和Tomcat路径,点击finish5.在Eclipse控制台…

    2022年4月8日
    39
  • 越来越多的Java程序员转行Java大数据…[通俗易懂]

    越来越多的Java程序员转行Java大数据…[通俗易懂]JAVA的精密,强大,拥有其它语言不可替代的性能和可维护性,早已经是成为最受欢迎的编程语言之一,很多人想进入IT行业,首选的第一门语言就是JAVA。  但是,在未来肯定是大数据的天下,人工智能的爆发,将会有大量企业会进入大数据领域,从而产生大量的大数据人才需求。据最新发布的《大数据人才报告》显示,目前全国的大数据人才仅46万,未来3-5年内大数据人才的缺口将高达150万。领英报…

    2022年7月9日
    163
  • shell编程入门_unix编程

    shell编程入门_unix编程1.Shell的概念shell是一个命令行解释器,它为客户提供了一个Linux内核发送请求一边运行程序界面系统级程序,用汉语可以通过shell启动、挂起、停止甚至编写一些程序。shell还是一个功能强大的编程语言,易于编辑,易于调试,灵活性强,shell是结识知识性的脚本语言,在我们shell中直接调用Linux的系统命令操作即可。2.Shell的分类(1)BourneShell(bshell)从1979年在unix系统就开始使用了。它的主要…

    2025年6月19日
    2
  • JRebel热部署

    JRebel热部署

    2021年7月11日
    77
  • 密码学与网络安全第七版部分课后习题答案[通俗易懂]

    密码学与网络安全第七版部分课后习题答案[通俗易懂]第0章序言1.课后题汇总(仅部分)第一章思考题:1、2、4、5第二章习题:10、12、16第三章习题:9第四章思考题:4、5、6第五章习题:11第六章习题:2、6第七章思考题:2、3、4习题:4、7、8第八章习题:2第九章思考题:5、6习题:2、3第十章习题:1、2第十一章思考题:1、2、3第十二章思考题:1、3、4、7第十三章思考题:…

    2022年5月21日
    39

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号