EditText (编辑文件对象)的各种属性

来源:互联网 发布:网络零售的特点与模式 编辑:程序博客网 时间:2024/05/01 22:46

文章来自于:http://blog.csdn.net/wqjsir/article/details/22901651#

指定EditText输入类型

    通过android:inputType可以指定EditText  的输入类型,比如输入数字,日期,密码或者邮件地址等。
下面列出常用的类型值:
text                  普通文本的输入
textEmailAddress      包含“@”字符的文本输入(邮件地址)
textUri               包含“/”字符的输入
number                基本数字输入
phone                 电话格式的输入

android:inputType控制输入行为
textCapWords       单词首字母大写,一般用于标题或者人名
textCapSentences   句子首个单词首字母大写,一般用于段落的开始
textCapCharacters  所有字符的大写
textAutoCorrect    纠正单词的拼写错误
textPassword       密码输入
textMultiLine      允许多行输入

android:inputType允许输入类型和输入行为按位组合的方式指定,比如:
[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <EditText  
  2.     android:id="@+id/postal_address"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="wrap_content"  
  5.     android:hint="@string/postal_address_hint"  
  6.     android:inputType="textPostalAddress|  
  7.                        textCapWords|  
  8.                        textNoSuggestions" />  
    这我们就指定了为邮政地址(textPostalAddress),且地址单词子首字母大写(textCapWords),且不显示输入单词的词典建议(textNoSuggestions)。

指定键盘操作

    软键盘的Enter键默认显示的是“完成”文本,我们知道按Enter建表示前置工作已经准备完毕了,要去什么什么。比如,在一个搜索中,我们输入要搜索的文本,然后按Enter表示要去搜索了,但是默认的Enter键显示的是“完成”文本,不符合搜索的语义,如果能显示“搜索”两个字或者显示一个表示搜索的图标多好。事实证明我们的想法是合理的,Android也为我们提供的这样的功能。通过设置android:imeOptions来改变默认的“完成”文本。这里举几个常用的常量值:
actionNext    软键盘不关闭,光标跳到下一个输入焦点位置(发送EditorInfo.IME_ACTION_NEXT命令)
actionGo      软键盘不关闭,光标跳到下一个输入焦点位置,发送EditorInfo.IME_ACTION_GO命令
actionSearch  软键盘不关闭,发送EditorInfo.IME_ACTION_SEARCH命令
actionSend    软键盘关闭,发送EditorInfo.IME_ACTION_SEND命令
actionDone    软键盘关闭,光标保持在原来的输入框上,发送EditorInfo.IME_ACTION_DONE命令
配置文件:
[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <EditText  
  2.        android:id="@+id/action_next"  
  3.        android:layout_width="fill_parent"  
  4.        android:layout_height="wrap_content"  
  5.        android:hint="@string/action_next"  
  6.        android:imeOptions="actionNext"  
  7.        android:inputType="text" />  
  8.   
  9.    <EditText  
  10.        android:id="@+id/action_go"  
  11.        android:layout_width="fill_parent"  
  12.        android:layout_height="wrap_content"  
  13.        android:hint="@string/action_go"  
  14.        android:imeOptions="actionGo"  
  15.        android:inputType="number" />  
  16.   
  17.    <EditText  
  18.        android:id="@+id/action_search"  
  19.        android:layout_width="fill_parent"  
  20.        android:layout_height="wrap_content"  
  21.        android:hint="@string/action_search"  
  22.        android:imeOptions="actionSearch"  
  23.        android:inputType="text" />  
  24.    <EditText  
  25.        android:id="@+id/action_done"  
  26.        android:layout_width="fill_parent"  
  27.        android:layout_height="wrap_content"  
  28.        android:hint="@string/action_done"  
  29.        android:imeOptions="actionDone"  
  30.        android:inputType="text" />  
  31.      
  32.    <EditText  
  33.        android:id="@+id/action_send"  
  34.        android:layout_width="fill_parent"  
  35.        android:layout_height="wrap_content"  
  36.        android:hint="@string/action_send"  
  37.        android:imeOptions="actionSend"  
  38.        android:inputType="text" />  
源代码:
[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. public class MainEditText extends Activity implements OnEditorActionListener{  
  2.     private EditText editNext;  
  3.     private EditText editGo;  
  4.     private EditText editSearch;  
  5.     private EditText editDone;  
  6.     private EditText editSend;  
  7.   
  8.     @Override  
  9.     public void onCreate(Bundle savedInstanceState){  
  10.         super.onCreate(savedInstanceState);  
  11.         setContentView(R.layout.activity_edit);  
  12.         editNext= (EditText) findViewById(R.id.action_next);  
  13.         editGo = (EditText)findViewById(R.id.action_go);  
  14.         editSearch = (EditText)findViewById(R.id.action_search);  
  15.         editDone = (EditText)findViewById(R.id.action_done);  
  16.         editSend = (EditText)findViewById(R.id.action_send);  
  17.         editNext.setOnEditorActionListener(this);  
  18.     }  
  19.   
  20.     @Override  
  21.     public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {  
  22.         boolean handled = false;  
  23.         EditText text = (EditText) findViewById(R.id.editText);  
  24.         switch (actionId) {  
  25.   
  26.         case EditorInfo.IME_ACTION_NEXT:  
  27.             Log.d("action next", text.getText().toString());  
  28.             handled = true;  
  29.             break;  
  30.         case  EditorInfo.IME_ACTION_SEND:  
  31.             Log.d("action send", text.getText().toString());  
  32.             handled = true;  
  33.             break;  
  34.         case EditorInfo.IME_ACTION_GO:  
  35.             Log.d("action send", text.getText().toString());  
  36.             handled = true;  
  37.             break;  
  38.         case EditorInfo.IME_ACTION_DONE:  
  39.             Log.d("action done", text.getText().toString());  
  40.             handled = true;  
  41.             break;  
  42.         case EditorInfo.IME_ACTION_SEARCH:  
  43.             Log.d("action search", text.getText().toString());  
  44.             handled = true;  
  45.             break;  
  46.         default:  
  47.             break;  
  48.         }  
  49.         return handled;  
  50.     }  
  51. }  
效果如图:
    

设置一个自定义操作按钮标签

   上面设置的键盘操作,的按钮文字都是系统默认设置,我们可以通过android:imeActionLabel属性来设置我们自定义的标签文本,如下:
[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <EditText  
  2.         android:id="@+id/action_next"  
  3.         android:layout_width="fill_parent"  
  4.         android:layout_height="wrap_content"  
  5.         android:hint="@string/action_next"  
  6.         android:imeOptions="actionNext"  
  7.         android:imeActionLabel="@string/action_next_text"  
  8.         android:inputType="text" />  
在string.xml中设置为:
[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <pre code_snippet_id="274045" snippet_file_name="blog_20140404_5_1054654" name="code" class="html"><string name="action_next_text">下一个</string></pre>  
显示为:

AutoCompleteTextView

   AutoCompleteTextView自动补充输入文本,是EditText的一个子类,当我们输入一部分字符时,就会给我们列出和我们输入相匹配的内容。
效果如下:

   list 列表中的建议词是通过AutoCompleteTextView的setAdapter方法设置上的,源码如下:
[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. ArrayAdapter<String> countries = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line,getResources().getStringArray(R.array.countries_arry));  
  2. AutoCompleteTextView autoText = (AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1);  
  3. autoText.setAdapter(countries);  
    如果用户选择了给出的建议词,如何检查系统的变化呢?这里我们注册一个TextWatcher来实现对输入的文本变化的检查,源码如下:
[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. autoText.addTextChangedListener(new TextWatcher(){  
  2.   
  3.     @Override  
  4.     public void afterTextChanged(Editable arg0) {  
  5.     }  
  6.   
  7.     @Override  
  8.     public void beforeTextChanged(CharSequence arg0, int arg1,  
  9.                     int arg2, int arg3) {  
  10.     }  
  11.   
  12.     @Override  
  13.     public void onTextChanged(CharSequence value, int arg1, int arg2,  
  14.                     int arg3) {  
  15.         Log.d("AutoCompleteTextActivity", value.toString());  
  16.     }  
  17. });  
xml文件内容为:
[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <AutoCompleteTextView  
  2.     android:id="@+id/autoCompleteTextView1"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="wrap_content"  
  5.     android:ems="10"  
  6.     android:completionThreshold="2"  
  7.     android:text="AutoCompleteTextView">  
其中android:completionThreshold=“2”表示当我们输入第二个字符时建议词list的显示
android:ems = "10" 设置TextView或者Edittext的宽度为10个字符的宽度。当设置该属性后,控件显示的长度就为10个字符的长度,超出的部分将不显示

自定义建议词list风格

设置描述List中item的格式(listitem.xml文件)
[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <TextView xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:id="@+id/list_item"  
  4.     android:layout_width="wrap_content"  
  5.     android:layout_height="wrap_content"  
  6.     android:padding="10dp"  
  7.     android:textColor="#0000aa"  
  8.     android:textSize="16sp" />  
源码:
[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. ArrayAdapter<String> countries = new ArrayAdapter<String>(this,R.layout.listitem,getResources().getStringArray(R.array.countries_arry));  
显示效果如下:
0 0
原创粉丝点击