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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • webpack配置文件_reconfig

    webpack配置文件_reconfig前言上一篇文章我们使用webpack打包成功了,但是每次都要自己手动输入打包的文件地址和打包到哪里去的地址,非常麻烦,所以这里介绍使用配置文件进行打包webpack.config.js首先我们创

    2022年7月31日
    4
  • java redis模糊查询_Redis模糊查询「建议收藏」

    java redis模糊查询_Redis模糊查询「建议收藏」最近使用Redis优化项目功能,其中有一部分为模糊查询,找了很多帖子,也没有找到很好的解决方案和思路,最终皇天不负有心人啊,终于让我找到了!!!可以通过Redis中keys命令进行获取key值,具体命令格式:keyspattern文中提到redis中允许模糊查询的有3个通配符,分别是:*,?,[]其中:*:通配任意多个字符?:通配单个字符[]:通配括号内的某一个字符===============…

    2022年5月29日
    40
  • java运行时异常和非运行时异常区别_常用的运行时异常

    java运行时异常和非运行时异常区别_常用的运行时异常1.Java异常机制Java把异常当做对象来处理,并定义一个基类java.lang.Throwable作为所有异常的超类。Java中的异常分为两大类:错误Error和异常Exception,Java异常体系结构如下图所示:图片来源:http://blog.csdn.net/wuwenxiang91322/article/details/103463372.Throwab

    2022年9月30日
    0
  • docker-compose教程(安装,使用, 快速入门)

    docker-compose教程(安装,使用, 快速入门)教程基于ubuntu下最新的compose版本3参考:https://docs.docker.com/compose/overview/1.Compose介绍DockerCompose是一个用来定义和运行复杂应用的Docker工具。一个使用Docker容器的应用,通常由多个容器组成。使用DockerCompose不再需要使用shell脚本来启动容器。Compose通过一个配…

    2022年6月14日
    26
  • java获取时间戳单位秒,如何测试给定的时间戳是以秒或毫秒为单位?「建议收藏」

    java获取时间戳单位秒,如何测试给定的时间戳是以秒或毫秒为单位?「建议收藏」Assumeagivenvariable,itiscontainingaUNIXtime-stamp,butwhetheritisinsecondsormillisecondsformatisunknown,IwanttoassigntoavariablewhichisinsecondsformatForExample:unkno…

    2022年9月4日
    2
  • libtorch-resnet18

    libtorch-resnet18与大家分享一下自己在学习使用libtorch搭建神经网络时学到的一些心得和例子,记录下来供大家参考首先我们要参考着pytorch版的resnet来搭建,这样我们可以省去不必要的麻烦,上代码:1、首先是pytorch版残差模块classResidualBlock(nn.Module):def__init__(self,inchannel,outchannel,stride=1,shortcut=None):super(ResidualBlock,self).__

    2022年5月23日
    32

发表回复

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

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