输入控件
输入控件是应用中用户界面的一种交互式组件。Android提供了大量的可供在UI中使用的控件,例如按钮、文本框、滑块进度条、复选框、缩放按钮和开关按钮等等。
在UI中添加输入控件就像在 XML layout 中添加XML元素一样简单。例如,下面是一个带有文本框和按钮的布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<EditText android:id="@+id/edit_message"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="@string/edit_message" />
<Button android:id="@+id/button_send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_send"
android:onClick="sendMessage" />
</LinearLayout>
每个输入控件都支持一套特定的输入事件,所以你可以在用户进入文本框或触碰按钮时捕获这些事件。
常用控件
下面是一些常用控件的列表,你可以在你的应用中使用它们。通过这些链接学习更多关于使用控件的知识。
注解:Android提供的控件比这里列出来的要多。浏览 android.widget
包可以发现更多控件。如果你的应用需要特殊的输入控件,你还可以构建自己的 custom components。
Control Type | Description | Related Classes |
---|---|---|
Button | 可以被用户按下或点击来执行某个操作的按钮。 | Button |
Text field | 可编辑的文本框。你可以使用AutoCompleteTextView 部件来创建一个可以提供自动完成建议的文本输入部件。 | EditText ,AutoCompleteTextView |
Checkbox | 可以由用户切换on/off的开关。当提供给用户一组不互斥的可选项时,你应该使用复选框。 | CheckBox |
Radio button | 与复选框类似,但一组里只能选择一个选项。 | RadioGroup RadioButton |
Toggle button | 带有亮度亮度指示器的on/off按钮。 | ToggleButton |
Spinner | 允许用户从一组数据中选择一个的下拉列表。 | Spinner |
Pickers | 供用户通过上/下按钮或滑动手势从一系列值中选择一个的对话框。使用DatePicker 部件输入日期(月,天,年)或使用TimePicker 部件输入时间(时,分,上午/下午),这些值会自动格式化为用户所在区域的格式。 | DatePicker ,TimePicker |