EditText常用设置

来源:互联网 发布:js 导出excel插件 编辑:程序博客网 时间:2024/05/19 10:38

一:长度和空白提示文字,提示文字颜色,是否可编辑等

EditText有一些属性可以设置EditText的特性,比如最大长度、空白提示文字等。

1、有时候我们有一些特殊的需要,要求只能在EditText中输入特定个数的字符,比如身份证号、手机号码等。这时候就可以通过android:maxLength属性来设置最大输入字符个数,比如android:maxLength="4"就表示最多能输入4个字符,再多了就输入不进去了

2、空白提示文字。有时候我们需要说明你定义的这个EditText是做什么用的,比如让输入“用户名”,或者输入“电话号码”等,但是你又不想在EditText前面加一个TextView来说明,因为这会使用一个TextView,那么怎么办呢?EditText为我们提供了android:hint来设置当EditText内容为空时显示的文本,这个文本只在EditText为空时显示。你输入字符的时候就消失了,不影响你的EditText的文本。

3、上面列出了空白时的提示文字,但是有时候不想要这个灰色的提示文字,可以通过android:textColorHint属性来设置你想要的颜色。

4、还有一个比较实用的功能,就是设置EditText的不可编辑。设置android:enable="false"可以实现不可编辑,可以获得焦点。这时候我们看到EditText和一个TextView差不多。

5、实现类型html中Textarea的文本域。在android中没有专门的文本域组件,但是可以通过设置EditText的高来实现统一的文本域功能。

二、输入特殊格式的字符

在我们开发程序的时候不免会输入一些特殊个数的字符,比如密码(输入框的字符要加密显示),电话号码,数字等。

1、密码文本框。android:password="true"

2、电话号码。android:phoneNumber = "true"

3、数字。android:numeric=""有三种integer正整数、signed带符号整数和decimal浮点数。

三、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

xml:

<?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>  
java代码:

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;   
            }   
        });   
    }   
}  

 

四、EditText的取值,全选,部分选择、获取选中文本
<?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>  
java代码:

 

/**  
 * EditText演示  
 */  
public class EditTextInputTypeextends 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(EditTextInputType.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(EditTextInputType.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(EditTextInputType.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常用设置 - 无尘 - 无尘的博客

 

原创粉丝点击