搜档网
当前位置:搜档网 › 【Android开发API】用户界面-文本区域 - Text Fields

【Android开发API】用户界面-文本区域 - Text Fields

【Android开发API】用户界面-文本区域 - Text Fields
【Android开发API】用户界面-文本区域 - Text Fields

负责?:Monica

主任务原文链接:https://www.sodocs.net/doc/323821951.html,/guide/topics/ui/controls/text.html

A text field allows the user to type text into your app. It can be either single line or multi-line. Touching a text field places the cursor and automatically displays the keyboard. In addition to typing, text fields allow for a variety of other activities, such as text selection (cut, copy, paste) and data look-up via auto-completion.

文本框允许用户在应用程序中输?文本。它们可以是单行的,也可以是多行的。点击文本框后显示光标,并自动显示键盘。除了输?,文本框还包含其它操作,比如文本选择(剪切,复制,粘贴)以及数据的自动查找功能。

You can add a text field to you layout with the EditText object. You should usually do so in your XML layout with a element.

你可以使用EditText对象在布局中添加?个文本字段, android里的写法通常是在XML布局文件中添加元素

https://www.sodocs.net/doc/323821951.html,/images/ui/edittext-noextract.png

Specifying the Keyboard Type-指定键盘类型

Text fields can have different input types, such as number, date, password, or email address. The type determines what kind of characters are allowed inside the field, and may prompt the virtual keyboard to optimize its layout for frequently used characters.

文本字段可以有不同的输?类型,如数字,日期,密码,或电?邮件地址。类型确定文本框内允许输?什么样的字符,可能会提示虚拟键盘调整其布局来显示最常用的字符。

You can specify the type of keyboard you want for your EditText object with the android:inputType attribute. For example, if you want the user to input an email address, you should use the textEmailAddress input type:

你可以在EditText对象使用Android:inputType属性指定输?类型的键盘,例如:你想输??个电?邮件地址上的用户,inputType属性应为textEmailAddress:

https://www.sodocs.net/doc/323821951.html,/images/ui/edittext-text.png

图1:默认的文字输?类型。

There are several different input types available for different situations. You can find them all listed with the documentation for android:inputType

针对不同的情况有?种不同的输?类型。你可以找到所有的文件中列出的android:inputType属性

https://www.sodocs.net/doc/323821951.html,/images/ui/edittext-email.png

图2:textEmailAddress输?类型。

Tip: To allow users to input long strings of text with line breaks, use the "textMultiLine" input type. By default, an EditText object is restricted to one line of text and scrolls horizontally when the text exceeds the available width.

提示:为了让用户输?长文本字符串时换行,使用的“textMultiLine”属性。默认情况下,?个编辑文本对象仅限于?行文本和水平滚动文本时超过可用宽度。

https://www.sodocs.net/doc/323821951.html,/images/ui/edittext-phone.png

图3:手机输?类型。

Controlling other behaviors-控制其他行为

The android:inputType also allows you to specify certain keyboard behaviors, such as whether to capitalize all new words or use features like auto-complete and spelling suggestions.

android:inputType还允许您指定操作行为,如在某此键盘上是否要利用所有新词,或使用自动完成和拼写建议功能。

The android:inputType attribute allows bitwise combinations so you can specify both a keyboard layout and one or more behaviors at once. For example, here's how you can collect a postal address, capitalize each word, and disable text suggestions:

在android:inputType的属性允许按位组合,让您可以?次指定?个键盘布局和?个或多个操作行为。例如,你如何收集邮政地址,利用每?个字,并禁用文字的行为:

All behaviors are also listed with the android:inputType documentation.

还列出了所有的行为与Android的:inputType相关的文件。

Specifying Keyboard Actions-指定键盘操作

In addition to changing the keyboard's input type, Android allows you to specify an action to be made when users have completed their input. The action specifies the button that appears in place of the carriage return key and the action to be made, such as "Search" or "Send."

除了改变键盘的输?类型,当用户完成输?时,android允许你指定特殊的按钮进行相应的操作,如把回车键作为“搜索”或 “发送”操作。

https://www.sodocs.net/doc/323821951.html,/images/ui/edittext-actionsend.png

图4:如果你声明的Android imeOptions =“actionSend” ,键盘包括发送的行动。

You can specify the action by setting the android:imeOptions attribute. For example, here's how you can specify the Send action:

您可以通过android:imeOptions属性设置指定的动作。例如,这里你可以指定发送的行为:

If you do not explicitly specify an input action then the system attempts to determine if there are any subsequent android:focusable fields. If any focusable fields are found following this one, the system applies the (@code actionNext} action to the current EditText so the user can select Next to move to the next field. If there's no subsequent focusable field, the system applies the "actionDone" action. You can override this by setting the android:imeOptions attribute to any other value such as "actionSend" or "actionSearch" or suppress the default behavior by using the "actionNone" action.

如果你不明确指定?个输?动作,然后系统将尝试确定是否有任何后续的android:focusable属性动作。如果发现了有android:focusable属性动作,那么这个系统适用于在当前的EditText的(@code actionNext}行动,使用户可以选择“下?步”或移动到下?个字段。如果是没有后续的focusable属性,那该系统适用“actionDone “动作,你也可以通过设置Android:imeOptions属性使系统更改到其它值,如“actionSend”或“actionSearch”或禁止使

用“actionNone”动作的默认行为。

Responding to action button events-响应按钮事件

If you have specified a keyboard action for the input method using android:imeOptions attribute (such as"actionSend"), you can listen for the specific action event using an TextView.OnEditorActionListener. The TextView.OnEditorActionListener interface provides a callback method called onEditorAction()that indicates the action type invoked with an action ID such as IME_ACTION_SEND or IME_ACTION_SEARCH.

如果您已指定键盘采用Android:imeOptions属性(“actionSend”等)的操作方法,你可以使用TextView.OnEditorActionListener监听事件行为。TextView.OnEditorActionListener接?提供了?个回调方法onEditorAction(),它通过输?的动作ID,如IME_ACTION_SEND或IME_ACTION_SEARCH行为调用相关的动作类型方法。

For example, here's how you can listen for when the user clicks the Send button on the keyboard:

例如,你可以监听用户点击键盘上的发送按钮:

EditText editText = (EditText) findViewById(R.id.search); editText.setOnEditorActionListener(new OnEditorActionListener() { @Override public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { boolean handled = false; if (actionId == EditorInfo.IME_ACTION_SEND) { // Send the user message handled = true; } return handled; } });

Setting a custom action button label-设置文本框标签

If the keyboard is too large to reasonably share space with the underlying application (such as when a

handset device is in landscape orientation) then fullscreen ("extract mode") is triggered. In this mode, a labeled action button is displayed next to the input. You can customize the text of this button by setting the android:imeActionLabel attribute:

如果键盘太?,系统将会合理分担余下的应用程序(例如,当手机设备是横向的)空间,然后全屏(“提取模式”)被触发。在这种模式下,将在输?框旁边显示按钮,你可以通过设置imeActionLabel属性来定制按钮的文本。

https://www.sodocs.net/doc/323821951.html,/images/ui/edittext-actionlabel.png

Figure 5. A custom action label with android:imeActionLabel

android:imeActionLabel属性案例

Adding Other Keyboard Flags-添加其他键盘标志

In addition to the actions you can specify with the android:imeOptions attribute, you can add additional flags to specify other keyboard behaviors. All available flags are listed along with the actions in the android:imeOptions documentation.

你可以指定Android:imeOptions属性添加另外的行为,你可以添加额外的标志,以指定其他键盘行为。在Android:imeOptions文件列出所有可用的标志。

For example, figure 5 shows how the system enables a fullscreen text field when a handset device is in landscape orientation (or the screen space is otherwise constrained for space). You can disable the fullscreen input mode with flagNoExtractUi in the android:imeOptions attribute, as shown in figure 6.

例如,图5显示的是当手机设备在横向屏幕上使用全屏文本字段时(或在屏幕上限制键盘空间)。您可以在Android:imeOptions属性设置flagNoExtractUi去禁用全屏输?模式,如图6所示。

https://www.sodocs.net/doc/323821951.html,/images/ui/edittext-noextract.png

Figure 6. The fullscreen text field ("extract mode") is disabled with android:imeOptions="flagNoExtractUi". android:imeOptions="flagNoExtractUi"的提取模式

Providing Auto-complete Suggestions-提供自动完成建议

If you want to provide suggestions to users as they type, you can use a subclass of EditText called AutoCompleteTextView. To implement auto-complete, you must specify an (@link android.widget.Adapter) that provides the text suggestions. There are several kinds of adapters available, depending on where the data is coming from, such as from a database or an array.

如果你想向用户键?提供建议,您可以使用EditText的?类AutoCompleteTextView控件。为了实现自动完成,你必须指定?组(@link adndroid.widget.Adapter)文字提供建议。有?种可用的适配器,匹配数据项,如从数据库或?个数组获取所需要匹配值。

https://www.sodocs.net/doc/323821951.html,/images/ui/edittext-autocomplete.png

Figure 7. Example of AutoCompleteTextView with text suggestions

AutoCompleteTextView控件案例

The following procedure describes how to set up an AutoCompleteTextView that provides suggestions from an array, using ArrayAdapter:

下面的过程介绍了如何设置?个AutoCompleteTextView并使用ArrayAdapter适配器配置里面的所需的数组:

1.Add the AutoCompleteTextView to your layout. Here's a layout with only the text field:

添加AutoCompleteTextView到您的布局。这里是只是?个文本字段的布局

2.Define the array that contains all text suggestions. For example, here's an array of country names that's defined in an XML resource file (res/values/strings.xml):

定义?个数组,里面包含数组所需的?值。例如,这里是?个国家的名字,这是定义在?个XML资源文件

(res/values/strings.xml)数组:

Afghanistan Albania Algeria American Samoa Andorra Angola Anguilla Antarctica ...

3.In your Activity or Fragment, use the following code to specify the adapter that supplies the suggestions:

在Acitivity中或Fragment中,建议使用下面的代码操作适配器

// Get a reference to the AutoCompleteTextView in the layout AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autocomplete_country); // Get the string array String[] countries = getResources().getStringArray(R.array.countries_array); // Create the adapter and set it to the AutoCompleteTextView ArrayAdapter adapter = new ArrayAdapter(this, https://www.sodocs.net/doc/323821951.html,yout.simple_list_item_1, countries); textView.setAdapter(adapter);

Here, a new ArrayAdapter is initialized to bind each item in the COUNTRIES string array to a TextView that exists in the simple_list_item_1 layout (this is a layout provided by Android that provides a standard appearance for text in a list).

代码中,创建?个countries的数组,并将ArrayAdapter适配器初始化,且将其绑定到simple_list_item_1文件布局中(这是由Android提供?个文本框绑定数据的文本列表)。

Then assign the adapter to the AutoCompleteTextView by calling setAdapter().

然后AutoCompleteTextView属性调用setAdapter()方法,将适配器添加其中。

?eoe移动开发者社区

本文链接: https://www.sodocs.net/doc/323821951.html,/page/Text_Fields

Android人机界面(UI)设计规范(带目录)

Android 人机界面设计规范 1Android 设计的依据 1.1 框架结构及流程 是什么使得android 有着独特的用户体验? 后台处理支持多任务功能 正在进行和事件驱动的提示信息 通过Widgets 和live folders 来实现实时信息的预览 用户想用时,任一应用程序都可以挑选和选择 android 不是关于程序的,它是关于活动,把任务分层, 1.2 架构基础 硬件平台 android 设备代表的是硬件和软件的完美组合。硬件辅助导航操作,并给android 提供更多更好的功能。当菜单没有开启,要把屏幕最大化时,菜单按钮可以在屏幕上提供更多的内容。返回按钮允许使用返回堆(back stack)。 竖屏与横屏 一般来说,用户界面开发竖屏与横屏。在新横屏也仍存在于新的Android 手机中。99%的android 布局支持横屏。 焦点和菜单 在触摸模式里没有焦点,只有轨迹球。Android 平台里没有鼠标焦点。确定你从未显示焦点。主菜单应该包括全部功能;它们与活动联系一起形成整体。菜单上的图标按重要性排序。如果有多于5 个图标,使用点击more menu 菜单来查看那些不太重要的菜单项。上下文菜单(长按)集中在一个特定对象。 总是把那些与所选项最相关的行为放在长按菜单的顶部。 需要记住的几点: 设计时要考虑速度和简洁 尽量分层来分等级 屏幕上的活动尽量最小 使用下载进度条,下载数据时,而不是让用户等待去看一个加载完全的页面。 考虑活动流而不是线性行为 1.3 屏幕上的行为

android 设计了特定的行为方式。在你的应用程序里利用好这一点。应该坚持android 行为的标准,避免混淆用户。 1.4 表达 细节使得产品集中在细节。程序的美学会帮助你集中注意在那些应用体验核心的关键任务上。API DEMO 是开始你的工具包的好地方。 2 用户界面原则 这部分试图讲述创造一个好的用户界面的一些基本的交互设计原则。这些原则是基本的,不止能应用于android 的用户界面设计,也可以应用于其他。苹果建议开发者花费60%的开发时间来进行设计工作。下面的用户界面原则将为好的设计提供一个基础。 2.1 隐喻 隐喻是构建一个基于操作任务心智模型的模块;用它们来传递应用程序的概念和功能。基于真实世界的应用对象可以帮助用户很快的理解该应用程序。当你设计你的应用程序时,要注意andriod 中存在的隐喻,不要重新定义它们。同时,检查你的应用程序执行的任务,看是否有些自然隐喻你可以使用。 2.2 反映用户的心智模型 用户已经有了一个来描述你的程序正在进行的任务的心智模型。这个心智模型产生于真实世界经验、其它软件和一般电脑基本知识的结合。比如说,用户在真实世界里有写字、寄信的经验,也会产生特定的期待,像写一封新的信,选一个接受者,然后寄出信。一个忽略用户心智模型的电子邮件程序用起来会很困难和不舒服。这是因为程序强加给用户一个不熟悉的概念模型,而不是建立一个用户已有的知识经验模式。 在设计程序用户界面之前,试着去发现你的用户的心智模型,这样帮助用户去执行任务。心智模型中内在的隐喻,它代表了任务的概念组成。在写信这个例子中,隐喻包括信件、邮包和信封。在涉及到照片的任务的思考模式中,隐喻包括照片、照相机和专辑。我们要努力地发现用户的期望,包括任务组成、组织、窗口布局的工作流、菜单和工具栏组织、控制面板的使用。 要通过努力地何必把个下面的特征与用户心智模型相融合: 熟悉性 用户的心智模型主要是建立在经验的基础上 简单化 一项任务的心智模型通常是流线型,关注任务的基本组成部分。尽管对于一个给定的任务有很多可选的细节,但是基本的组成部分占大部分,并且不会占用用户的注意。 可利用性Availability

Android简单的登陆界面的设计开发

通信实训报告 -Android移动平台开发 学院:信息工程学院 班级: 学号: 姓名:

实训内容: 一.1.Andriod的简介 Android一词的本义指“机器人”,同时也是Google于2007年11月5日宣布的基于Linux平台的开源手机操作系统的名称,该平台由操作系统、中间件、用户界面和应用软件组成,号称是首个为移动终端打造的真正开放和完整的移动软件。目前,最新版本为Android 2.4 Gingerbread 和Android 3.0 Honeycomb。 Android是基于Linux开放性内核的操作系统,是Google公司在2007年11月5日公布的手机操作系统。 Android早期由原名为"Android"的公司开发,谷歌在2005年收购"Android.Inc"后,继续对Android系统开发运营,它采用了软件堆层(software stack,又名软件叠层)的架构,主要分为三部分。底层Linux内核只提供基本功能,其他的应用软件则由各公司自行开发,部分程序以Java编写。2011年初数据显示,仅正式上市两年的操作系统Android已经超越称霸十年的塞班系统,使之跃居全球最受欢迎的智能手机平台。现在,Android系统不但应用于智能手机,也在平板电脑市场急速扩张,在智能MP4方面也有较大发展。采用Android系统主要厂商包括台湾的HTC,(第一台谷歌的手机G1由HTC生产代工)美国摩托罗拉,SE等,中国大陆厂商如:魅族(M9),华为、中兴、联想、蓝魔等。 2.Android构架图 二.1软件下载 Android SDK,网址是https://www.sodocs.net/doc/323821951.html,. JDK的下载地址https://www.sodocs.net/doc/323821951.html,/javase/downloads/widget/jdk6.jsp。Eclipse的下载网址是https://www.sodocs.net/doc/323821951.html,/downloads/ 2.Android开发环境搭建

第四章Android用户界面

第四章Android用户界面 P3 Android系统为我们提供了丰富的可视化用户界面组件,包括菜单、对话框、按钮、下列列表等。Android系统借用了Java里的UI设计思想,包括事件响应机制和布局管理,所以有过Java UI开发经验的学生,学些这一章会很轻松。 Android系统中所有UI类都是建立在View和ViewGroup这两个类的基础之上的。所有View 的子类称为Widget,所有ViewGroup的子类称为Layout。 P5 View和ViewGroup之间采用了组合设计模式(Composite)。 P6 ViewGroup作为布局容器类在最上层,布局容器里面又可以有View和ViewGroup。 P16 为了更好地管理Android应用的用户界面里的各组件,Android提供了布局管理器。通过使用布局管理器,Android应用的图形用户界面具有良好的平台无关性。通常来说,推荐使用布局管理器来管理组件的分布、大小,而不是直接设置组件的位置和大小。这样可以让组件在不同的手机屏幕上都能运行良好—不同手机屏幕的分辨率、尺寸并不完全相同。如果让程序手动控制每个组件的大小、位置、则将给编程带来巨大的困难。为了解决这个问题,Android 提供了布局管理器。布局管理器可以根据运行平台来调整组件的大小,程序员要做的,只是为组件选择合适的布局管理器。 与Swing不同的是,Android的布局管理器本身就是一个UI组件,所有布局管理器都是ViewGroup的子类。 P18 线性布局由LinearLayout来代表,线性布局有点像AWT编程里的FlowLayout,他们都会将容器里的组件一个挨着一个地排列起来。LinearLayout不仅可以控制组件横向排列,也可控制各组件纵向排列。 线性布局与AWT中的FlowLayout的最大区别在于:Android的线性布局不会换行:当组件一个挨着一个地排列到头后,剩下的组件将不会被现实出来;在AWT中FlowLayout则会另起一行排列多出来的组件。

第4章 Android用户界面设计

视图组件的使用模式 常用组件 高级组件 提示框与警告对话框

就是Android应用程序的开发过程。一般过程是先通过XML布局文件或Java代码创建界面布局,设定组件显示样式,随后获取UI组件对象,并处理组件事件响应。 视图组件的定义 资源的访问 生成视图组件资源标识 视图组件的引用 视图组件的事件响应 组件的常用属性

1.1视图组件的定义 使用XML布局文件定义视图组件 使用Java代码定义视图组件(不推荐)

1.1视图组件的定义 使用XML布局文件定义视图组件 Android平台为大多数视图组件以及其子类提供了XML标记,可通过XML布局文件中的标记来定义视图组件。XML中的每个元素代表了一个组件,即元素名称对应相应的Java类。

1.1视图组件的定义