当前位置:  编程技术>移动开发
本页文章导读:
    ▪【通译】(4)How To        【翻译】(4)How To ----------------- 英文文档见android-ndk-r5b的documentation.html 属于Android Native Development Kit (NDK)的一部分 见http://developer.android.com/sdk/ndk/(需要代理) 翻译仅个人见解 ----------------- Andro.........
    ▪ 【通译】(11)NDK Build        【翻译】(11)NDK Build ----------------- 英文文档见android-ndk-r5b的documentation.html 属于Android Native Development Kit (NDK)的一部分 见http://developer.android.com/sdk/ndk/(需要代理) 翻译仅个人见解 ----------------- '.........
    ▪ EditText使用详解-包含很多教程下看不到的功能演示       EditText使用详解-包含很多教程上看不到的功能演示 一:新建HelloEditText工程  新建一个Hello world详细步骤可以参见 Android教程之三:第一个Android应用,HelloWorld 创建设置如下: 1.Project name: Hel.........

[1]【通译】(4)How To
    来源: 互联网  发布时间: 2014-02-18
【翻译】(4)How To

-----------------

英文文档见android-ndk-r5b的documentation.html

属于Android Native Development Kit (NDK)的一部分

见http://developer.android.com/sdk/ndk/(需要代理)

翻译仅个人见解

-----------------

Android NDK How-To:

 

Android NDK指南:

 

===================

 

A collection of tips and tricks for NDK users

 

一些给NDK用户的提示和技巧

 

How to force the display of build commands:

 

如何强制显示构建命令

-------------------------------------------

 

Do "ndk-build V=1" and actual build commands will be displayed. This can be used to verify that things are compiled as you expect them to, and check for bugs in the NDK build system.

 

执行“ndk-build V=1”则实际构建命令将会被显示。可用于验证那些东西正如你所预期的那样被编译,以及检查NDK构建系统的缺陷。

 

(The V=1 trick comes from the Linux kernel build system)

 

(V=1的技巧来自Linux内核构建系统)

 

How to force a rebuild of all your sources:

 

如何强制你的所有代码的重新构建:

-------------------------------------------

 

Use GNU Make's "-B" option, as in:

 

使用GNU Make的-B选项,如下所示:

 

   ndk-build -B

 

How to store your native sources in a location other than $PROJECT/jni:

 

如何让你的原生源代码放在不同于$PROJECT/jni的地方(注:jni是放置jni的C文件的约定目录)

-----------------------------------

 

First, you can simply tell your $PROJECT/jni/Android.mk to include another Android.mk that are located in different places.

 

首先,你可以简单地让你的$PROJECT/jni/Android.mk包含位于不同位置的另一个Android.mk。(注:也就是说$PROJECT/jni/Android.mk必须存在)

 

Alternatively, you can define APP_BUILD_SCRIPT in your Application.mk to point to an alternative Android.mk file.

 

另一种方法是,你可以在Application.mk中定义APP_BUILD_SCRIPT的值指向一个可选的Android.mk的文件。

 

How to build a project's native files without cd-ing to it:

如何不使用cd命令来构建一个工程的原生文件

-----------------------

 

Sometimes, you may need to rebuild a project's native file without being able to cd to its top-level path from the command-line. This is do-able by using the GNU-Make '-C <path>' option, as in:

 

有时,你可能需要重新编译一个工程的原生文件但不能在命令行上用cd命令切换到它的顶级路径。可以使用GNU-Make的-C <路径>选项,如下所示:

 

    ndk-build -C <project-path>

 

How to store your Application.mk in a location other than $PROJECT/jni:

 

如何保存你的Application.mk在不同于$PROJECT/jni的位置:

-----------------------------------

 

Starting with NDK r4, you can simply place the file under $PROJECT/jni/ and launch the 'ndk-build' script from your project tree.

 

从NDK r4开始,你可以简单地放置文件在$PROJECT/jni/目录下并且在你的工程树上运行ndk-build脚本。

 

If you want to use 'ndk-build' but place the file to a different location, use a GNU Make variable override as:

 

如果你想使用ndk-build但放置文件到一个不同的位置,使用一个GNU MAKE变量重载为:

 

    ndk-build NDK_APPLICATION_MK=/path/to/your/Application.mk

 

If you're using the legacy $NDK/apps/<name> build method, you can create a symbolic link to your final Application.mk there. For example, imagine that you wrote:

 

如果你使用旧有的$NDK/apps/<名称>构建方法,你可以创建一个符号链接到你的最终Application.mk那里。例如,假设你这样写:

 

  $PROJECT/foo/Application.mk

 

You can create a symlink like with a command like:

 

你可以创建一个符号链接,用像这样的命令行:

 

  ln -s $PROJECT/foo  $NDK/apps/<name>

 

This will make $NDK/apps/<name>/Application.mk point directly to $PROJECT/jni/Application.mk

 

这将使$NDK/apps/<名称>/Application.mk直接指向$PROJECT/jni/Application.mk

 

Note that generated files will still go under $NDK/out/apps/<name> though.

 

不过注意,生成的文件将仍旧跑到$NDK/out/apps/<名称>。(注:out目录而非apps目录)

 

Windows users: The NDK is only supported on Cygwin, which implements symbolic links through the "ln -s" command, as in:

 

Windows用户:NDK只在Cygwin上被支持,实现符号链接是通过ln -s命令,如下所示:

 

    ln -s  <target>  <link>

 

 

How to properly add include directories to your module declaration:

 

如何合适地添加头文件目录到你的模块声明:

-------------------------------

 

If you define several modules, it is common to need to include one module's header while compiling another one. For example, consider the following example:

 

如果你定义几个模块, 当编译一个模块时一般需要包含另一个模块的头文件。例如,考虑下面的示例:

 

  $PROJECT/jni/foo/

    Android.mk

    foo.h

    foo.c

 

  $PROJECT/jni/bar/

    Android.mk

    bar.c

 

(注:上面是说foo和bar目录下有哪些文件)

 

Where the 'bar.c' uses '#include <foo.h>'. You will need to add the path to the 'foo' module in jni/bar/Android.mk to build it properly.

 

这里bar.c使用#include <foo.h>语句。你将需要在jni/bar/Android.mk中添加路径到foo模块以正确地构建它。(注:在C中,#include <...>是用于包含.h头文件。.h头文件仅用于预编译,生成.o文件实际上只需要.c文件。由于C的模块编译是分布式的,需要借助#include来事先声明模块外的函数。头文件的目录可以在编译命令行中用选项来指定)

 

One is tempted to use the following:

 

可以尝试使用以下方法:

 

  LOCAL_C_INCLUDES := ../foo

 

However this will not work because all compilation happens from the directory where 'ndk-build' is invoked, and include files must be relative to it.

 

然而,这将不能工作,因为所有编译发生在执行ndk-build的地方,而包含文件必然相对于这个地方。(注:前面提到,有可能不是在Android.mk的目录执行ndk-build)

 

The correct line is instead:

 

正确的命令行应该是:(注:在Android的工程文件中经常用$(LOCAL_PATH)取得Android.mk所在目录)

 

  LOCAL_C_INCLUDES := $(LOCAL_PATH)/../foo

 

Which uses a path relative to $(LOCAL_PATH), in the case where you would need to move 'foo' and 'bar' to a deeper level in the 'sources' hierarchy.

 

它使用相对于$(LOCAL_PATH)的路径,通常用在你需要把foo和bar移动到源代码树的更深层次的情况下。(注:实际上在Cygwin下可以用/cygdrive/<盘符>/...指定绝对路径也可,不过有点麻烦而已)

 

In case you absolutely need it, you can also use NDK_APP_PROJECT_PATH to point to your project directory:

 

一旦你一定需要这样,你还可以使用NDK_APP_PROJECT_PATH指向你的工程目录:

 

  LOCAL_C_INCLUDES := $(NDK_APP_PROJECT_PATH)/jni/foo

 

However, we don't recommend using this, paths relative to $(LOCAL_PATH) being better.

 

然而,我们不建议使用这种方法,使用相对于$(LOCAL_PATH)的路径较好。

 

 

 


    
[2] 【通译】(11)NDK Build
    来源: 互联网  发布时间: 2014-02-18
【翻译】(11)NDK Build

-----------------

英文文档见android-ndk-r5b的documentation.html

属于Android Native Development Kit (NDK)的一部分

见http://developer.android.com/sdk/ndk/(需要代理)

翻译仅个人见解

-----------------

'ndk-build' Overview

 

ndk-build概述

 

I. Usage:

 

一、使用方法:

---------

 

The Android NDK r4 introduced a new tiny shell script, named 'ndk-build', to simplify building machine code.

 

Android NDK r4引入一个新的小型shell脚本,名字是ndk-build,以简化机器码的构建。

 

The script is located at the top-level directory of the NDK, and shall be invoked from the command-line when in your application project directory, or any of its sub-directories. For example:

 

脚本位于NDK的顶级目录,并且将在你的应用程序工程目录,或它的任意子目录中的命令行调用。例如:

 

    cd $PROJECT

    $NDK/ndk-build

 

Where $NDK points to your NDK installation path. You can also create an alias or add $NDK to your PATH to avoid typing it every time.

 

这里$NDK指向你的NDK安装目录。你还可以创建一个别名或者添加$NDK到你的PATH环境变量以避免每次都键入它(注:PATH区分大小写)。

 

II. Options:

 

二、选项:

------------

 

All parameters to 'ndk-build' are passed directly to the underlying GNU Make command that runs the NDK build scripts. Notable uses include:

 

所有给ndk-build的参数被直接传递到运行NDK构建脚本的底层GNU Make命令。值得注意的使用方法包括:

 

  ndk-build                  --> rebuild required machine code.

 

  ndk-build                  --> 重新构建所需的机器代码。

 

  ndk-build clean            --> clean all generated binaries.

 

  ndk-build clean            --> 清除所有生成的二进制文件。

 

  ndk-build NDK_DEBUG=1      --> generate debuggable native code.

 

  ndk-build NDK_DEBUG=1      --> 生产可调试的本地代码。

 

  ndk-build V=1              --> launch build, displaying build commands.

 

  ndk-build V=1              --> 启动构建,显示构建命令。

 

  ndk-build -B               --> force a complete rebuild.

 

  ndk-build -B               --> 强制完全重新构建。

 

  ndk-build -B V=1           --> force a complete rebuild and display build commands.

 

  ndk-build -B V=1           --> 强制完全重新构建并且显示构建命令。

 

  ndk-build NDK_LOG=1        --> display internal NDK log messages (used for debugging the NDK itself).

 

  ndk-build NDK_LOG=1        --> 显示内部NDK日志消息(用于调试NDK自身)。

 

  ndk-build NDK_DEBUG=1      --> force a debuggable build (see below)

 

  ndk-build NDK_DEBUG=1      --> 强制调试版构建(见下)(注:调试版指带调试信息,可以用gdb调试的二进制文件)

 

  ndk-build NDK_DEBUG=0      --> force a release build (see below)

 

  ndk-build NDK_DEBUG=0      --> 强制发布版构建(见下)(注:发布版指不带调试信息的二进制文件)

 

  ndk-build NDK_APP_APPLICATION_MK=<file> --> rebuild, using a specific Application.mk pointed to by the NDK_APP_APPLICATION_MK command-line variable.

 

  ndk-build NDK_APP_APPLICATION_MK=<文件名> --> 重新构建,通过NDK_APP_APPLICATION_MK命令行变量指向使用特定的Application.mk.

 

  ndk-build -C <project>     --> build the native code for the project path located at <project>. Useful if you don't want to 'cd' to it in your terminal.

 

  ndk-build -C <工程目录>     --> 构建位于<工程目录>的工程的本地代码。当你不想在终端上用cd切换到那个目录时有用。

 

III. Debuggable versus Release builds:

 

三、调试版和发布版构建:

--------------------------------------

 

In NDK r5, ndk-build has been modified to make it easier to switch between release and debug builds. This is done by using the NDK_DEBUG variable. 

 

在NDK r5,ndk-build已经被修改成可以更容易地在发布版和调试版构建之间切换。通过使用NDK_DEBUG变量来做到。

 

For example:

 

例如:

 

  $NDK/ndk-build NDK_DEBUG=1  => forces the generation of debug binaries

 

  $NDK/ndk-build NDK_DEBUG=1  => 强制生成调试版二进制文件。

 

  $NDK/ndk-build NDK_DEBUG=0  => forces the generation of release binaries

 

  $NDK/ndk-build NDK_DEBUG=0  => 强制生成发布版二进制文件。

 

If you don't specify NDK_DEBUG, ndk-build will keep its default behaviour, which is to inspect the AndroidManifest.xml, if any, and see if its <application> element has android:debuggable="true".

 

如果你不指定NDK_DEBUG,ndk-build将保持它的默认行为,即检查AndroidManifest.xml,如果有,检查它的<application>元素是否有android:debuggable="true"属性。

 

IMPORTANT: If you use the build tools of SDK r8 (or higher), you won't need to touch your AndroidManifest.xml file at all!

 

重要事项:如果你使用SDK r8(或更高)的构建工具,你将完全不必修改你的AndroidManifest.xml文件!

 

That's because if you build a debug package (e.g. with "ant debug" or the corresponding option of the ADT plugin), the tool will  automatically pick the native debug files generated with NDK_DEBUG=1.

 

那是因为如果你构建一个调试包(例如,使用“ant debug”或ADT插件相应选项),工具将自动选择用NDK_DEBUG=1生成原生调试版文件。(注:也就是说,Java代码为调试版,则JNI的C代码也自动切换为调试版)

 

Also, as a convenience, the release and debug object files generated by the NDK are now stored in different directories (e.g. obj/local/<abi>/objs and obj/local/<abi>/objs-debug). This avoids having to recompile all your sources when you switch between these two modes (even when you only modified one or two source files).

 

同样,方便起见,有NDK生成的发布版和调试版对象文件现在被存放在不同的目录中(例如obj/local/<abi>/objs和obj/local/<abi>/objs-debug)。(注:abi是应用二进制接口的缩写,这里指代某类交叉编译器,例如EABI,嵌入式应用二进制接口)。这避免当你在这两种模式之间切换时必须重新编译所有源代码(即便你仅仅修改一个或两个源文件)。(注:Makefile的策略导致,Makefile认为xxx.c一般只会生成xxx.o,也就是说调试版和发布版的.o不能共存,上面的方法类似Windows,使用不同的目录输出不同版本的.o,以避免覆盖)

 

 

IV. Requirements:

 

四、要求:

-----------------

 

You need GNU Make 3.81 or later to use 'ndk-build' or the NDK in general. The build scripts will detect that you're using a non-compliant Make tool and will complain with an error message.

 

一般你需要GNU Make 3.81或更高以使用ndk-build或NDK(注:因为GNU的make有两种)。构建脚本将检测到你正在使用不兼容的Make工具并且用一个错误消息解释。

 

If you have GNU Make 3.81 installed, but that it is not launched by the default 'make' command, define GNUMAKE in your environment to point to it before launching 'ndk-build'. For example:

 

如果你已经安装GNU Make 3.81以上,但不能用默认的make命令启动,可以在运行ndk-build之前在你的环境变量中定义GNUMAKE指向它的路径。例如:

 

    GNUMAKE=/usr/local/bin/gmake ndk-build

 

(注:环境变量的键值对出现在命令之前,常用于临时变更环境变量,等效于下面的export命令)

 

Or to make the change more permanent:

 

或更永久地改变:

 

    export GNUMAKE=/usr/local/bin/gmake

    ndk-build

 

Adapt to your shell and GNU Make 3.81 installation location.

 

请适配你的shell和GNU Make 3.81的安装位置。

 

V. Internals:

 

五、内幕:

-------------

 

'ndk-build' itself is a tiny wrapper around GNU Make, its purpose is simply to invoke the right NDK build script, it is equivalent to;

 

ndk-build本身是对GNU Make的一个小型封装,它的目的是简化正确NDK构建脚本调用,它等效于:

 

    $GNUMAKE -f $NDK/build/core/build-local.mk [parameters]

 

Where '$GNUMAKE' points to GNU Make 3.81 or later, and $NDK points to your NDK installation directory.

 

这里$GNUMAKE指向GNU Make 3.81或更新,而$NDK指向你的NDK安装目录。

 

Use this knowledge if you want to invoke the NDK build script from other shell scripts (or even your own Makefiles).

 

如果你想从其它shell脚本中调用NDK构建脚本(或者甚至是你自己的Makefile文件)(注:Makefile是make的默认配置文件,类似于Ant的build.xml)时可以使用这种技巧。

 


    
[3] EditText使用详解-包含很多教程下看不到的功能演示
    来源: 互联网  发布时间: 2014-02-18
EditText使用详解-包含很多教程上看不到的功能演示

一:新建HelloEditText工程
 

新建一个Hello world详细步骤可以参见

Android教程之三:第一个Android应用,HelloWorld

创建设置如下:

1.Project name: HelloEditText
2.Build Target :android 2.2
3.Application name:HelloEditText
4.Package name:com.flysnow
5.create Activity: HelloEditText
6.min SDK 8
 这时候运行还看不到EditText,因为我们还没有加上,修改main.xml如下:

Xml代码
1.<? xml   version = "1.0"   encoding = "utf-8" ?>   
2.< LinearLayout   xmlns:android = "http://schemas.android.com/apk/res/android"   
3.    android:orientation = "vertical"   
4.    android:layout_width = "fill_parent"   
5.    android:layout_height = "fill_parent"   
6.    >   
7.< EditText   
8.    android:id = "@+id/edit_text"     
9.    android:layout_width = "fill_parent"    
10.    android:layout_height = "wrap_content"    
11.    android:text = "这是一个EditText" />   
12.</ LinearLayout >   
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<EditText
 android:id="@+id/edit_text" 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="这是一个EditText"/>
</LinearLayout>
 这里添加了一个id为"edit_text"的EditText,设置默认显示为本为“这是一个EditText”。。运行效果如下:


 

二:EditText简介
EditText是一个非常重要的组件,可以说它是用户和Android应用进行数据传输窗户,有了它就等于有了一扇和Android应用传输的门,通过它用户可以把数据传给Android应用,然后得到我们想要的数据。

EditText是TextView的子类,所以TextView的方法和特性同样存在于EditText中,具体的TextView的介绍可以参考上一节Android系列教程之六:TextView小组件的使用--附带超链接和跑马灯效果

 

三:长度和空白提示文字,提示文字颜色,是否可编辑等
EditText有一些属性可以设置EditText的特性,比如最大长度,空白提示文字等。

1.有时候我们有一些特属的需要,要求只能在EditText中输入特定个数的字符,比如身份证号、手机号吗等。这时候就可以通过android:maxLength属性来设置最大输入字符个数,比如 android:maxLength=“4”就表示最多能输入4个字符,再多了就输入不进去了。
2.空白提示文字。有时候我们需要说明你定义的这个EditText是做什么用的,比如让输入“用户名”,或者输入“电话号码”等,但是你又不想在EditText前面加一个TextView来说明这是输入“用户名”的,因为这会使用一个 TextView,那么怎么办呢?EditText为我们提供了android:hint来设置当EditText内容为空时显示的文本,这个文本只在 EditText为空时显示,你输入字符的时候就消失了,不影响你的EditText的文本。。修改main.xml如下:

Xml代码
1.<? xml   version = "1.0"   encoding = "utf-8" ?>   
2.< LinearLayout   xmlns:android = "http://schemas.android.com/apk/res/android"   
3.    android:orientation = "vertical"   
4.    android:layout_width = "fill_parent"   
5.    android:layout_height = "fill_parent"   
6.    >   
7.< EditText   
8.    android:id = "@+id/edit_text"     
9.    android:layout_width = "fill_parent"    
10.    android:layout_height = "wrap_content"   
11.    android:maxLength = "40"   
12.    android:hint = "请输入用户名..." />   
13.</ LinearLayout >   
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<EditText
 android:id="@+id/edit_text" 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:maxLength="40"
    android:hint="请输入用户名..."/>
</LinearLayout> 运行应用就会看到如下的效果:
 
 看看吧,简洁明了还不用新增一个TextView说明,也不影响用户操作。
3.上面列出了空白时的提示文字,有的人说了,我不想要这个灰色的提示文字,和我的应用整体风格不协调,那也行啊,我们可以换颜色,怎么换呢,就是通过android:textColorHint属性设置你想要的颜色。修改main.xml如下:
Xml代码
1.<? xml   version = "1.0"   encoding = "utf-8" ?>   
2.< LinearLayout   xmlns:android = "http://schemas.android.com/apk/res/android"   
3.    android:orientation = "vertical"   
4.    android:layout_width = "fill_parent"   
5.    android:layout_height = "fill_parent"   
6.    >   
7.< EditText   
8.    android:id = "@+id/edit_text"     
9.    android:layout_width = "fill_parent"    
10.    android:layout_height = "wrap_content"   
11.    android:maxLength = "40"   
12.    android:hint = "请输入用户名..."   
13.    android:textColorHint = "#238745" />   
14.</ LinearLayout >   
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<EditText
 android:id="@+id/edit_text" 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:maxLength="40"
    android:hint="请输入用户名..."
    android:textColorHint="#238745"/>
</LinearLayout> 运行程序效果如下:
 
 看到了吧,颜色已经变了。。
4.还有一个比较实用的功能,就是设置EditText的不可编辑。设置android:enabled="false"可以实现不可编辑,可以获得焦点。这时候我们看到EditText和一个TextView差不多:
 
5.实现类似html中Textarea的文本域。在Android中没有专门的文本域组件,但是可以通过设置EditText的高来实现同样的文本域功能。修改main.xml如下:

Xml代码
1.<? xml   version = "1.0"   encoding = "utf-8" ?>   
2.< LinearLayout   xmlns:android = "http://schemas.android.com/apk/res/android"   
3.    android:orientation = "vertical"   
4.    android:layout_width = "fill_parent"   
5.    android:layout_height = "fill_parent"   
6.    >   
7.< EditText   
8.    android:id = "@+id/edit_text"     
9.    android:layout_width = "fill_parent"    
10.    android:layout_height = "200dip" />   
11.</ LinearLayout >   
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<EditText
 android:id="@+id/edit_text" 
    android:layout_width="fill_parent"
    android:layout_height="200dip"/>
</LinearLayout> 运行程序效果如下:
 
四:输入特殊格式的字符
在我们开发程序的时候不免会输入一些特属个数的字符,比如密码(输入框的字符要加密显示),电话号码(比如数字和-),数字等,这些都算是一些特属格式的字符,强大的EditText同样为我们提供了输入这些特属格式字符的设置。

1.密码文本框。密码输入也是Android应用常用的功能,通过配置EditText的android:password="true"就可以实现这一密码输入功能,修改main.xml如下:

Xml代码
1.<? xml   version = "1.0"   encoding = "utf-8" ?>   
2.< LinearLayout   xmlns:android = "http://schemas.android.com/apk/res/android"   
3.    android:orientation = "vertical"   
4.    android:layout_width = "fill_parent"   
5.    android:layout_height = "fill_parent"   
6.    >   
7.< EditText   
8.    android:id = "@+id/edit_text"     
9.    android:layout_width = "fill_parent"    
10.    android:layout_height = "wrap_content"   
11.    android:password = "true" />   
12.</ LinearLayout >   
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<EditText
 android:id="@+id/edit_text" 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:password="true"/>
</LinearLayout> 运行效果如下:
 
 可以看到我们输入的字符已经被“.”这样的掩码所代替。
2.手机中发短信打电话是必不可少的,所以用于专门输入电话号码的文本框也是大有用途,有了他我们对是否是电话号码的校验就容易的多了(因为字符是正确的,只要校验格式 ).通过设置android:phoneNumber="true"就可以把EditText变成只接受电话号码输入的文本框,连软键盘都已经变成拨号专用软键盘了,所以不用再担心输入其他字符了。修改main.xml如下:

Xml代码
1.<? xml   version = "1.0"   encoding = "utf-8" ?>   
2.< LinearLayout   xmlns:android = "http://schemas.android.com/apk/res/android"   
3.    android:orientation = "vertical"   
4.    android:layout_width = "fill_parent"   
5.    android:layout_height = "fill_parent"   
6.    >   
7.< EditText   
8.    android:id = "@+id/edit_text"     
9.    android:layout_width = "fill_parent"    
10.    android:layout_height = "wrap_content"   
11.    android:phoneNumber = "true" />   
12.</ LinearLayout >   
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<EditText
 android:id="@+id/edit_text" 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:phoneNumber="true"/>
</LinearLayout> 运行程序效果如下:
 
 注意看软键盘,已经变成拨号专用的啦.
3.有时候我们只想输入数字,不想输入字母,EditText为我们提供了 android:numeric来控制输入的数字类型,一共有三种分别为integer(正整数)、signed(带符号整数)和decimal(浮点数)。这里以signed类型的为例,修改main.xml如下:

Xml代码
1.<? xml   version = "1.0"   encoding = "utf-8" ?>   
2.< LinearLayout   xmlns:android = "http://schemas.android.com/apk/res/android"   
3.    android:orientation = "vertical"   
4.    android:layout_width = "fill_parent"   
5.    android:layout_height = "fill_parent"   
6.    >   
7.< EditText   
8.    android:id = "@+id/edit_text"     
9.    android:layout_width = "fill_parent"    
10.    android:layout_height = "wrap_content"   
11.    android:numeric = "signed" />   
12.</ LinearLayout >   
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<EditText
 android:id="@+id/edit_text" 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:numeric="signed"/>
</LinearLayout> 运行效果如下:
 
 注意这里的软键盘变成“数字键盘”的变化.
五:为文本指定特定的软键盘类型
前面我们通过指定为电话号码特定格式,然后键盘类型变成了拨号专用的键盘,这个是自动变的,其实我们也可以通 过android:inputType来设置文本的类型,让输入法选择合适的软键盘的。。android:inputType有很多类型,这里使用date类型来演示,修改main.xml如下:


Xml代码
1.<? xml   version = "1.0"   encoding = "utf-8" ?>   
2.< LinearLayout   xmlns:android = "http://schemas.android.com/apk/res/android"   
3.    android:orientation = "vertical"   
4.    android:layout_width = "fill_parent"   
5.    android:layout_height = "fill_parent"   
6.    >   
7.< EditText   
8.    android:id = "@+id/edit_text"     
9.    android:layout_width = "fill_parent"    
10.    android:layout_height = "wrap_content"   
11.    android:inputType = "date" />   
12.</ LinearLayout >   
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<EditText
 android:id="@+id/edit_text" 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:inputType="date"/>
</LinearLayout>  运行效果如下:
 

六:Enter键图标的设置
软键盘的Enter 键默认显示的是“完成”文本,我们知道按Enter建表示前置工作已经准备完毕了,要去什么什么啦。比如,在一个搜索中,我们输入要搜索的文本,然后按 Enter表示要去搜索了,但是默认的Enter键显示的是“完成”文本,看着不太合适,不符合搜索的语义,如果能显示“搜索”两个字或者显示一个表示搜索的图标多好。事实证明我们的想法是合理的,Android也为我们提供的这样的功能。通过设置android:imeOptions来改变默认的“完成”文本。这里举几个常用的常量值:

1.actionUnspecified  未指定,对应常量EditorInfo.IME_ACTION_UNSPECIFIED.效果:
2.actionNone 没有动作,对应常量EditorInfo.IME_ACTION_NONE 效果:
3.actionGo 去往,对应常量EditorInfo.IME_ACTION_GO 效果:
4.actionSearch 搜索,对应常量EditorInfo.IME_ACTION_SEARCH 效果: 
5.actionSend 发送,对应常量EditorInfo.IME_ACTION_SEND 效果:
6.actionNext 下一个,对应常量EditorInfo.IME_ACTION_NEXT 效果:
7.actionDone 完成,对应常量EditorInfo.IME_ACTION_DONE 效果:
 下面已搜索为例,演示一个实例,修改main.xml如下:

Xml代码
1.<? xml   version = "1.0"   encoding = "utf-8" ?>   
2.< LinearLayout   xmlns:android = "http://schemas.android.com/apk/res/android"   
3.    android:orientation = "vertical"   
4.    android:layout_width = "fill_parent"   
5.    android:layout_height = "fill_parent"   
6.    >   
7.< EditText   
8.    android:id = "@+id/edit_text"     
9.    android:layout_width = "fill_parent"    
10.    android:layout_height = "wrap_content"   
11.    android:imeOptions = "actionSearch" />   
12.</ LinearLayout >   
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<EditText
 android:id="@+id/edit_text" 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:imeOptions="actionSearch"/>
</LinearLayout>  修改HelloEditText如下:

Java代码
1.package  com.flysnow;  
2.  
3.import  android.app.Activity;  
4.import  android.os.Bundle;  
5.import  android.view.KeyEvent;  
6.import  android.widget.EditText;  
7.import  android.widget.TextView;  
8.import  android.widget.Toast;  
9.import  android.widget.TextView.OnEditorActionListener;  
10.  
11.public   class  HelloEditText  extends  Activity {  
12.    /** Called when the activity is first created. */   
13.    @Override   
14.    public   void  onCreate(Bundle savedInstanceState) {  
15.        super .onCreate(savedInstanceState);  
16.        setContentView(R.layout.main);  
17.        EditText editText=(EditText)findViewById(R.id.edit_text);  
18.        editText.setOnEditorActionListener(new  OnEditorActionListener() {  
19.            @Override   
20.            public   boolean  onEditorAction(TextView v,  int  actionId, KeyEvent event) {  
21.                Toast.makeText(HelloEditText.this , String.valueOf(actionId), Toast.LENGTH_SHORT).show();  
22.                return   false ;  
23.            }  
24.        });  
25.    }  
26.}  
package com.flysnow;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.TextView.OnEditorActionListener;

public class HelloEditText extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        EditText editText=(EditText)findViewById(R.id.edit_text);
        editText.setOnEditorActionListener(new OnEditorActionListener() {
   @Override
   public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
    Toast.makeText(HelloEditText.this, String.valueOf(actionId), Toast.LENGTH_SHORT).show();
    return false;
   }
  });
    }
} 运行程序,点击回车(也就是搜索图标软键盘按钮)会显示该actionId.我们上面的每一个设置都会对应一个常量,这里的actionId就是那个常量值。
 

七:EditText的取值、全选、部分选择、获取选中文本
       下面通过一个例子来演示EditText的取值、全选、部分选择和获取选中文本.main.xml修改如下:

Xml代码
1.<? xml   version = "1.0"   encoding = "utf-8" ?>   
2.< LinearLayout   xmlns:android = "http://schemas.android.com/apk/res/android"   
3.    android:orientation = "vertical"   
4.    android:layout_width = "fill_parent"   
5.    android:layout_height = "fill_parent"   
6.    >   
7.< EditText   
8.    android:id = "@+id/edit_text"     
9.    android:layout_width = "fill_parent"    
10.    android:layout_height = "wrap_content"   
11.    android:imeOptions = "actionSearch" />   
12.< Button    
13.    android:id = "@+id/btn_get_value"   
14.    android:text = "取值"   
15.    android:layout_width = "wrap_content"   
16.    android:layout_height = "wrap_content" />   
17.< Button    
18.    android:id = "@+id/btn_all"   
19.    android:text = "全选"   
20.    android:layout_width = "wrap_content"   
21.    android:layout_height = "wrap_content" />   
22.< Button    
23.    android:id = "@+id/btn_select"   
24.    android:text = "从第2个字符开始选择"   
25.    android:layout_width = "wrap_content"   
26.    android:layout_height = "wrap_content" />   
27.< Button    
28.    android:id = "@+id/btn_get_select"   
29.    android:text = "获取选中文本"   
30.    android:layout_width = "wrap_content"   
31.    android:layout_height = "wrap_content" />   
32.</ LinearLayout >   
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<EditText
 android:id="@+id/edit_text" 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:imeOptions="actionSearch"/>
<Button
 android:id="@+id/btn_get_value"
 android:text="取值"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"/>
<Button
 android:id="@+id/btn_all"
 android:text="全选"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"/>
<Button
 android:id="@+id/btn_select"
 android:text="从第2个字符开始选择"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"/>
<Button
 android:id="@+id/btn_get_select"
 android:text="获取选中文本"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"/>
</LinearLayout>
 
HelloEditText修改如下:

Java代码
1.package  com.flysnow;  
2.  
3.import  android.app.Activity;  
4.import  android.os.Bundle;  
5.import  android.text.Editable;  
6.import  android.text.Selection;  
7.import  android.view.KeyEvent;  
8.import  android.view.View;  
9.import  android.view.View.OnClickListener;  
10.import  android.widget.Button;  
11.import  android.widget.EditText;  
12.import  android.widget.TextView;  
13.import  android.widget.Toast;  
14.import  android.widget.TextView.OnEditorActionListener;  
15./**  
16. * EditText演示  
17. * @author 飞雪无情  
18. * @since 2010-11-29  
19. */   
20.public   class  HelloEditText  extends  Activity {  
21.    /** Called when the activity is first created. */   
22.    @Override   
23.    public   void  onCreate(Bundle savedInstanceState) {  
24.        super .onCreate(savedInstanceState);  
25.        setContentView(R.layout.main);  
26.        final  EditText editText=(EditText)findViewById(R.id.edit_text);  
27.        //监听回车键   
28.        editText.setOnEditorActionListener(new  OnEditorActionListener() {  
29.            @Override   
30.            public   boolean  onEditorAction(TextView v,  int  actionId, KeyEvent event) {  
31.                Toast.makeText(HelloEditText.this , String.valueOf(actionId), Toast.LENGTH_SHORT).show();  
32.                return   false ;  
33.            }  
34.        });  
35.        //获取EditText文本   
36.        Button getValue=(Button)findViewById(R.id.btn_get_value);  
37.        getValue.setOnClickListener(new  OnClickListener() {  
38.            @Override   
39.            public   void  onClick(View v) {  
40.                Toast.makeText(HelloEditText.this , editText.getText().toString(), Toast.LENGTH_SHORT).show();  
41.            }  
42.        });  
43.        //让EditText全选   
44.        Button all=(Button)findViewById(R.id.btn_all);  
45.        all.setOnClickListener(new  OnClickListener() {  
46.            @Override   
47.            public   void  onClick(View v) {  
48.                editText.selectAll();  
49.            }  
50.        });  
51.        //从第2个字符开始选择EditText文本   
52.        Button select=(Button)findViewById(R.id.btn_select);  
53.        select.setOnClickListener(new  OnClickListener() {  
54.            @Override   
55.            public   void  onClick(View v) {  
56.                Editable editable=editText.getText();  
57.                Selection.setSelection(editable, 1 ,editable.length());  
58.            }  
59.        });  
60.      //获取选中的文本   
61.        Button getSelect=(Button)findViewById(R.id.btn_get_select);  
62.        getSelect.setOnClickListener(new  OnClickListener() {  
63.            @Override   
64.            public   void  onClick(View v) {  
65.                int  start=editText.getSelectionStart();  
66.                int  end=editText.getSelectionEnd();  
67.                CharSequence selectText=editText.getText().subSequence(start, end);  
68.                Toast.makeText(HelloEditText.this , selectText, Toast.LENGTH_SHORT).show();  
69.            }  
70.        });  
71.    }  
72.    /**  
73.     * 交换两个索引  
74.     * @param start 开始索引  
75.     * @param end 结束索引  
76.     */   
77.    protected   void  switchIndex( int  start,  int  end) {  
78.        int  temp=start;  
79.        start=end;  
80.        end=temp;  
81.    }  
82.}  
package com.flysnow;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.Selection;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.TextView.OnEditorActionListener;
/**
 * EditText演示
 * @author 飞雪无情
 * @since 2010-11-29
 */
public class HelloEditText extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        final EditText editText=(EditText)findViewById(R.id.edit_text);
        //监听回车键
        editText.setOnEditorActionListener(new OnEditorActionListener() {
   @Override
   public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
    Toast.makeText(HelloEditText.this, String.valueOf(actionId), Toast.LENGTH_SHORT).show();
    return false;
   }
  });
        //获取EditText文本
        Button getValue=(Button)findViewById(R.id.btn_get_value);
        getValue.setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View v) {
    Toast.makeText(HelloEditText.this, editText.getText().toString(), Toast.LENGTH_SHORT).show();
   }
  });
        //让EditText全选
        Button all=(Button)findViewById(R.id.btn_all);
        all.setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View v) {
    editText.selectAll();
   }
  });
        //从第2个字符开始选择EditText文本
        Button select=(Button)findViewById(R.id.btn_select);
        select.setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View v) {
    Editable editable=editText.getText();
    Selection.setSelection(editable, 1,editable.length());
   }
  });
      //获取选中的文本
        Button getSelect=(Button)findViewById(R.id.btn_get_select);
        getSelect.setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View v) {
    int start=editText.getSelectionStart();
    int end=editText.getSelectionEnd();
    CharSequence selectText=editText.getText().subSequence(start, end);
    Toast.makeText(HelloEditText.this, selectText, Toast.LENGTH_SHORT).show();
   }
  });
    }
    /**
     * 交换两个索引
     * @param start 开始索引
     * @param end 结束索引
     */
 protected void switchIndex(int start, int end) {
  int temp=start;
  start=end;
  end=temp;
 }

 运行效果如下:


 
 可以通过输入文字和点击下面的按钮测试。

 

八:小结
     这结详细介绍了EditText的大部分特性和常用功能,如常用的密码框,获取值等等。这几天忙的没更新,这次更新个长的。可以够消化一阵子的。


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/homebei2/archive/2010/12/15/6077891.aspx


    
最新技术文章:
▪Android开发之登录验证实例教程
▪Android开发之注册登录方法示例
▪Android获取手机SIM卡运营商信息的方法
▪Android实现将已发送的短信写入短信数据库的...
▪Android发送短信功能代码
▪Android根据电话号码获得联系人头像实例代码
▪Android中GPS定位的用法实例
▪Android实现退出时关闭所有Activity的方法
▪Android实现文件的分割和组装
▪Android录音应用实例教程
▪Android双击返回键退出程序的实现方法
▪Android实现侦听电池状态显示、电量及充电动...
▪Android获取当前已连接的wifi信号强度的方法
▪Android实现动态显示或隐藏密码输入框的内容
▪根据USER-AGENT判断手机类型并跳转到相应的app...
▪Android Touch事件分发过程详解
▪Android中实现为TextView添加多个可点击的文本
▪Android程序设计之AIDL实例详解
▪Android显式启动与隐式启动Activity的区别介绍
▪Android按钮单击事件的四种常用写法总结
▪Android消息处理机制Looper和Handler详解
▪Android实现Back功能代码片段总结
▪Android实用的代码片段 常用代码总结
▪Android实现弹出键盘的方法
▪Android中通过view方式获取当前Activity的屏幕截...
▪Android提高之自定义Menu(TabMenu)实现方法
▪Android提高之多方向抽屉实现方法
▪Android提高之MediaPlayer播放网络音频的实现方法...
▪Android提高之MediaPlayer播放网络视频的实现方法...
▪Android提高之手游转电视游戏的模拟操控
 


站内导航:


特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!

©2012-2021,,E-mail:www_#163.com(请将#改为@)

浙ICP备11055608号-3