Android EditText的使用方法

1. 简介

EditText是用于输入数据的一个文本框。


2. 示例:显示一个文本框

这个例子非常简单,就是创建一个文本框,并显示一个缺省的字符串。

2.1 布局文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    xmlns:tools="http://schemas.android.com/tools"  
    android:orientation="vertical"  
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent">  
  
    <EditText  
        android:id="@+id/edit_text_id"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:inputType="text"
        android:text="@string/edit_text_default_value" />  
  
</LinearLayout>  


2.2 字符串资源

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">HelloEditText</string>
    <string name="action_settings">Settings</string>
    <string name="edit_text_default_value">Hello, EditText</string>

</resources>


2.3 代码

package com.example.helloedittext;

import android.app.Activity;
import android.os.Bundle;
import android.widget.EditText;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        EditText editText = (EditText)this.findViewById(R.id.edit_text_id);
    }

}

2.4 运行结果



2.5 主要属性

上面EditText使用的一个主要属性是inputType,表示这个文本框可以输入的数据类型。inputType的取值范围如下:



3. 示例:几种inputType的效果

现在通过例子观察inputType的效果,以text、number、date三种为例。

3.1 布局文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    xmlns:tools="http://schemas.android.com/tools"  
    android:orientation="vertical"  
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent">  

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >
	    
	    <TextView
	        android:id="@+id/text_label_id"
	        android:layout_width="wrap_content"
	        android:layout_height="wrap_content"
	        android:text="@string/text_label" />
	    
	    <EditText
	        android:id="@+id/text_id"
	        android:layout_width="wrap_content"
	        android:layout_height="wrap_content"
	        android:inputType="text"
	        android:labelFor="@id/text_label_id"
	        android:text="@string/text_default_value" />
	</LinearLayout>
	
    <LinearLayout 
        android:orientation="horizontal"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content">  
        
        <TextView
            android:id="@+id/number_label_id"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/number_label" />
        
        <EditText
            android:id="@+id/number_id"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:inputType="number"
            android:labelFor="@id/number_label_id"
            android:text="@string/number_default_value" />
    </LinearLayout>
    
    <LinearLayout 
        android:orientation="horizontal"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content">  
        
        <TextView
            android:id="@+id/date_label_id"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/date_label" />
        
        <EditText
            android:id="@+id/date_id"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:inputType="date"
            android:labelFor="@id/date_label_id"
            android:text="@string/date_default_value" />
    </LinearLayout>
    
</LinearLayout>  


3.2 字符串资源

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">HelloEditText</string>
    <string name="action_settings">Settings</string>
    <string name="text_default_value">the text</string>
    <string name="text_label">text</string>
    <string name="number_default_value">1234567890</string>
    <string name="number_label">number</string>
    <string name="date_default_value">2014-08-15</string>
    <string name="date_label">date</string>
    
</resources>


3.3 代码

package com.example.helloedittext;

import android.app.Activity;
import android.os.Bundle;
import android.widget.EditText;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // do nothing now
        //EditText editText = (EditText)this.findViewById(R.id.text_id);
    }

}


3.4 运行效果

这里可以在三个编辑框中尝试输入各种数据,观察输入的效果。比如在number的编辑框中,就无法输入数字以外的字符。


目前的界面显示效果不佳,比如竖着的方向没有对齐。这个在后面的布局专题再进行分析。


3.5 EditText对应的TextView

在上面的例子中,已经通过labelFor属性把一个TextView关联到EditText。


4. methods介绍

4.1 焦点相关

包括设置焦点和判断是否可以设置焦点。实际上,这里的两个methods是基类View中定义的,因此适用于所有的View对象。

4.1.1 isFocusable()

    /**
     * Returns whether this View is able to take focus.
     *
     * @return True if this view can take focus, or false otherwise.
     * @attr ref android.R.styleable#View_focusable
     */
    @ViewDebug.ExportedProperty(category = "focus")
    public final boolean isFocusable() {
        return FOCUSABLE == (mViewFlags & FOCUSABLE_MASK);
    }

一个EditText缺省情况下返回true。

4.1.2 setFocusable()

    /**
     * Set whether this view can receive the focus.
     *
     * Setting this to false will also ensure that this view is not focusable
     * in touch mode.
     *
     * @param focusable If true, this view can receive the focus.
     *
     * @see #setFocusableInTouchMode(boolean)
     * @attr ref android.R.styleable#View_focusable
     */
    public void setFocusable(boolean focusable) {
        if (!focusable) {
            setFlags(0, FOCUSABLE_IN_TOUCH_MODE);
        }
        setFlags(focusable ? FOCUSABLE : NOT_FOCUSABLE, FOCUSABLE_MASK);
    }

4.1.3 setFocusableInTouchMode()

    /**
     * Set whether this view can receive focus while in touch mode.
     *
     * Setting this to true will also ensure that this view is focusable.
     *
     * @param focusableInTouchMode If true, this view can receive the focus while
     *   in touch mode.
     *
     * @see #setFocusable(boolean)
     * @attr ref android.R.styleable#View_focusableInTouchMode
     */
    public void setFocusableInTouchMode(boolean focusableInTouchMode) {
        // Focusable in touch mode should always be set before the focusable flag
        // otherwise, setting the focusable flag will trigger a focusableViewAvailable()
        // which, in touch mode, will not successfully request focus on this view
        // because the focusable in touch mode flag is not set
        setFlags(focusableInTouchMode ? FOCUSABLE_IN_TOUCH_MODE : 0, FOCUSABLE_IN_TOUCH_MODE);
        if (focusableInTouchMode) {
            setFlags(FOCUSABLE, FOCUSABLE_MASK);
        }
    }

4.1.4 isFocusableInTouchMode()

缺省情况下为true。

    /**
     * When a view is focusable, it may not want to take focus when in touch mode.
     * For example, a button would like focus when the user is navigating via a D-pad
     * so that the user can click on it, but once the user starts touching the screen,
     * the button shouldn't take focus
     * @return Whether the view is focusable in touch mode.
     * @attr ref android.R.styleable#View_focusableInTouchMode
     */
    @ViewDebug.ExportedProperty
    public final boolean isFocusableInTouchMode() {
        return FOCUSABLE_IN_TOUCH_MODE == (mViewFlags & FOCUSABLE_IN_TOUCH_MODE);
    }

4.1.5 requestFocus()

    /**
     * Call this to try to give focus to a specific view or to one of its
     * descendants.
     *
     * A view will not actually take focus if it is not focusable ({@link #isFocusable} returns
     * false), or if it is focusable and it is not focusable in touch mode
     * ({@link #isFocusableInTouchMode}) while the device is in touch mode.
     *
     * See also {@link #focusSearch(int)}, which is what you call to say that you
     * have focus, and you want your parent to look for the next one.
     *
     * This is equivalent to calling {@link #requestFocus(int, Rect)} with arguments
     * {@link #FOCUS_DOWN} and <code>null</code>.
     *
     * @return Whether this view or one of its descendants actually took focus.
     */
    public final boolean requestFocus() {
        return requestFocus(View.FOCUS_DOWN);
    }

4.1.6 isFocused()

    /**
     * Returns true if this view has focus
     *
     * @return True if this view has focus, false otherwise.
     */
    @ViewDebug.ExportedProperty(category = "focus")
    public boolean isFocused() {
        return (mPrivateFlags & PFLAG_FOCUSED) != 0;
    }




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值