前面我们学习了怎样创建我们第一个安卓工程,也了解了它基本运作,UI组件+事件处理代码,现在我们来学习Android的基本组件,组件就是你的手机中的按钮,文本框,图片等等,这些是最为基本的组件,看到这里是不是跃跃欲试了呢?现在我们开始认识它们。
所有的布局管理器,显示组件都是View类的子类。view类本身包含大量接口。
- textView
-
可以定义字体长度颜色样式,具体自行查看API,首先我们在配置文件设置自己的文本样式,<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" />
<style name="Mystyle" parent="android:Theme.Light">
<item name="android:textSize">45px</item>
<item name="android:textColor">#FFFF00</item>
<item name="android:autoLink">all</item>
<!--
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
-->
</style>
首先我们新建一个虚拟机用于运行我们创建的app
- Button 是TextView的子类
- 编辑框:EditText 也是TextView的子类
- 单选按钮:RadioGroup
需要注意的是单选按钮RadioButton必须在RadioGroup中使用
<RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="63dp"
android:layout_marginTop="67dp" >
<RadioButton
android:id="@+id/radio0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="男" />
<RadioButton
android:id="@+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女" />
<RadioButton
android:id="@+id/radio2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="其他" />
</RadioGroup>
效果如下:
用户选择后,可以获得选中按钮的ID,或者清空,或者设置要选中的按钮,具体可以参见方法。
- 复选框 复选框不同于单选需要在RadioGroup中使用,它可以直接使用。
- 下拉列表框:Spinner
首先自己新建一个xml文件定义:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="city">
<item >北京</item>
<item>上海</item>
<item>广州</item>
</string-array>
</resources>
然后:
这样运行出来的效果就是: