[url=http://sarin.iteye.com/blog/1601316]接上文[/url]
类似于Web开发中的HTML输入框,EditText就是Android中的文本输入框。前面介绍的TextView仅仅用于静态文本的显示,而需要用户进行输入的话,就需要使用EditText组件。
首先,看一下EditText的文档:
[img]http://dl.iteye.com/upload/attachment/0072/3185/c7fdcfc1-cc25-31f1-b193-c125bef31e80.jpg[/img]
[b]
java.lang.Object
↳ android.view.View
↳ android.widget.TextView
↳ android.widget.EditText
[/b]
和Button组件一样,EditText也是TextView的子类,可以看作是TextView类的扩展组件。那么我们开始编写EditText组件的代码:

<EditText
        android:id="@+id/edit1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

这是最基本的EditText组件的展示,我们可以获得如下效果:

[img]http://dl.iteye.com/upload/attachment/0072/3187/4240dce5-71ca-3426-bcec-4f7b4b55311e.jpg[/img]

效果就是出现一个文本框,里面出现我们给出的默认文字。下面修改这个组件代码:

<EditText
        android:id="@+id/edit1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:selectAllOnFocus="true"
        android:text="@string/hello" />

我们添加了一个属性,叫selectAllOnFocus,那么值为true,也就是默认全部选中,至于显示的效果,运行代码我们就可以看到:

[img]http://dl.iteye.com/upload/attachment/0072/3189/3af76370-22eb-34d1-9090-ea5fc5b0b5ec.jpg[/img]

不同之处就是EditText中的文本在初始状态时已经被默认全部选中了。

我们继续来看EditText组件的配置:

<EditText
        android:id="@+id/edit2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:enabled="false"
        android:text="http://www.google.com.hk" />

这里我们加了一个属性是enabled,值为false,不难从字面理解就是不可以使用,那么运行效果如下:

[img]http://dl.iteye.com/upload/attachment/0072/3191/dd338ea8-a2f3-3175-aa7f-a4ecb1e03ad2.jpg[/img]

在Web开发中,HTML文本输入框还有一种密码输入框,这样的设置是必要的,那么在Android中,我们也有对应的属性来设置:

<EditText
        android:id="@+id/edit3"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:inputType="textPassword"
        android:text="password" />

这里我们加入了inputType属性,值设置为textPasswrod,那么也就是密码显示效果,文本就不会以明文效果显示出来了,而是使用原点来作为替代。这样的设置是比较新的写法,我们还可以使用来表示android:password="true"密码输入框。

运行程序,我们得到如下的显示效果:

[img]http://dl.iteye.com/upload/attachment/0072/3193/7aff50fc-0978-33e2-ae31-c71010b27a74.jpg[/img]

现在我们已经在Android中实现了对HTML文本输入框和密码输入框的实现。在Web开发中我们有一种需求是对输入文本的限制,比如年龄输入框限制为必须是数字,而不能混合输入数字和字母等,这需要编写JavaScript代码来实现限制或是校验,但是在Android中我们可以直接做到限制效果,代码如下:

<EditText
        android:id="@+id/edit4"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:inputType="number"
        android:text="24abc" />

同样,还有一种老式写法为android:numeric="integer",表达的都是同样的意义,这里的取值可以有number,decimal等,具体可以参考文档去查找,或者在Eclipse中代码的提示功能也会看到。这里的默认文本我们输入24abc,带有字母,它可以正常的显示出来,但是在运行中,该输入框是不接受字母输入的,下面运行程序,我们可以看到:

[img]http://dl.iteye.com/upload/attachment/0072/3195/34ca5fbe-efa8-3c42-aeb7-e6df57ce56cc.jpg[/img]

将鼠标点击在该EditText组件上,使用键盘输入,发现字母无法输入,而数字可以被接受,就是这样的效果。

同样,我们可以在Activity程序中来进行组件的动态生成,我们编写如下代码:

package org.ourpioneer;
import android.app.Activity;
import android.os.Bundle;
import android.text.InputType;
import android.widget.EditText;
import android.widget.LinearLayout;
public class EditTextDemoActivity extends Activity {
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		super.setContentView(R.layout.main);
		LinearLayout layout = (LinearLayout) super.findViewById(R.id.layout);
		EditText edit = new EditText(this);
		edit.setInputType(InputType.TYPE_CLASS_NUMBER);
		edit.setText("2.4");
		layout.addView(edit);
	}
}

操作步骤和之前的类似,先获取到布局管理器,之后创建一个EditText组件,然后设置inputType为数字,之后设置默认显示文本为2.4,最后将该组件加入到布局管理器之中。

运行程序,我们就得到了最后的效果:

[img]http://dl.iteye.com/upload/attachment/0072/3199/830b0039-f161-3598-aed8-082883f33c5b.jpg[/img]

同样,这里是无法接受字母输入的。

这部分演示的代码请参考附件。

[url=http://sarin.iteye.com/blog/1636484]接下文[/url]