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


相关推荐

  • 国内期货软件开发_正规期货外盘平台

    国内期货软件开发_正规期货外盘平台期货软件搭建-期货软件开发-期货平台搭建,要想做期货软件开发定制,网站的安全性和稳定性非常重要,要搭建一款安全有保障的期货软件,找专业的金融软件搭建公司是必不可少的,那么期货软件开发,期货平台搭建的流程特点有哪些呢?  1.要搭建好用的软件,软件的部署成本和软件的易用性要好一款成熟稳重的期货软件,要经得起时间的推敲,很多公司通过购买软件给到用户,其实他们满足不了二次开发的需求,导致软件更新和后期维护上面根本维护不了。2.要搭建期货平台,价格一定要合理,不要贪图小便宜好的期货平台搭建很多人贪图小便宜,

    2022年9月10日
    0
  • 4、条件和循环语句

    一、if 条件if…else 语句 (1) 单分支: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if 条件: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;满足条件后要执行的代码 &amp;nbsp;&amp;nbsp;&amp

    2021年11月30日
    42
  • iOS键盘布局_ios布局方式

    iOS键盘布局_ios布局方式根据需求更换以下类型,例如:self.textField.keyboardType=UIKeyboardTypeDefault;1.UIKeyboardTypeDefault常用于文本输入2.UIKeyboardTypeASCIICapable常用于密码输入3.UIKeyboardTypeNumbersAndPunctuation主键盘显示如上图,次键盘显示字母4.UIKeyboar

    2022年10月27日
    0
  • 软件工程保研浙江大学、南京大学、同济大学、中山大学经验分享

    软件工程保研浙江大学、南京大学、同济大学、中山大学经验分享一、个人基本情况学校:某末流985专业:信息管理与信息系统排名:1平均成绩:91.23科研与比赛:一个国家级大创,互联网+国家级银奖,一篇论文(未发表),还有一堆没用什么用的水奖二、保研经验分享1.中山大学数据科学与计算机学院我参加的是中山大学的夏令营,最后的结果是考核合格(拟录取)。由于疫情原因,所以这次让我们做了一个进行自我介绍的PPT,面试时间总共为20分钟,有8—10分钟的时间进行PPT自述(中文),讲解完后老师会用英文提问,当时问我的问题是:介绍你最喜欢的城市、介绍

    2022年7月25日
    48
  • 老男孩python课程_老男孩python课程[通俗易懂]

    老男孩python课程_老男孩python课程[通俗易懂]网页编程基础知识07w.avi–570.61MB网页编程基础知识06w.avi–305.27MB网页编程基础知识05w.avi–240.27MB网页编程基础知识04w.avi–313.23MB网页编程基础知识03w.avi–238.87MB网页编程基础知识02w.avi–184.95MB网页编程基础知识01w(此课程无声,后期补录).avi–868.18MBpython…

    2022年10月21日
    0
  • vuethink 在本地没问题,在服务器报错 , php5.6与php5.5之间的大坑

    vuethink 在本地没问题,在服务器报错 , php5.6与php5.5之间的大坑

    2021年10月13日
    46

发表回复

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

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