SuperbookKing

4.用户接口UI布局----View控件的概述之EditText

1.EditText(textview 子类)----控件能向用户展现文本信息

//在XML布局文件中使用

android:singleLine="false" 设置多行

android:maxLength="3" 限制输入字符数量

android:hint="@string/hello" 设置提示信息

android:inputType="phone" 限制EditText输入信息

android:drawableLeft="@drawable/title" 在EditText中显示图片

android:background="@drawable/shape" 可设置EditText背景形状

 

<EditText
android:id="@+id/edtxt"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:background="@drawable/shap"
android:drawableLeft="@drawable/title"
android:hint="@string/hello"
android:inputType="phone" />

默认情况下软键盘右下角的按钮为“下一个”,点击会到下一个输入框,保持软键盘

设置 android:imeOptions="actionDone" ,软键盘下方变成“完成”,点击后光标保持在原来的输入框上,并且软键盘关闭

android:imeOptions="actionSend" 软键盘下方变成“发送”,点击后光标移动下一个
 
在这里设置的imeOptions如何使用呢?如下面的代码,让EditText实现setOnEditorActionListener,在onEditAction方法中actionId就对应我们设置的imeOptions。系统默认的actionId有:EditorInfo.IME_NULL、EditorInfo.IME_ACTION_SEND、EditorInfo.IME_ACTION_DONE等。这样我们就可以根据不同的EditText来实现不同的软键盘右下角功能键。
 
EditText editText =(EditText) findViewById(R.id.search);
editText.setOnEditorActionListener(newOnEditorActionListener(){@Overridepublicboolean onEditorAction(TextView v,int actionId,KeyEventevent){boolean handled =false;if(actionId ==EditorInfo.IME_ACTION_SEND){

// Send the user message handled =true;}return handled;

}

});
// AutoCompleteTextView(Edit
Text 子类)
  1. Add the AutoCompleteTextView to your layout. Here's a layout with only the text field:
    <AutoCompleteTextViewxmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/autocomplete_country"android:layout_width="fill_parent"android:layout_height="wrap_content"/>
  2. Define the array that contains all text suggestions. For example, here's an array of country names that's defined in an XML resource file (res/values/strings.xml):
    <resources>
    <string-arrayname="countries_array">
    <item>Afghanistan</item><item>Albania</item><item>Algeria</item><item>American Samoa</item><item>Andorra</item><item>Angola</item><item>Anguilla</item><item>Antarctica</item> ... </string-array></resources>
  3. In your Activity or Fragment, use the following code to specify the adapter that supplies the suggestions:
    // Get a reference to the AutoCompleteTextView in the layout
    AutoCompleteTextView textView =(AutoCompleteTextView) findViewById(R.id.autocomplete_country);
    // Get the string arrayString[] countries = getResources().getStringArray(R.array.countries_array);
    // Create the adapter and set it to the AutoCompleteTextView
    ArrayAdapter<String> adapter =newArrayAdapter<String>(this, android.R.layout.simple_list_item_1, countries); textView.setAdapter(adapter);

2.AutoCompleteTextView(EditText子类)----控件根据用户输入进行联想

java.lang.Object
android.view.View
android.widget.TextView
android.widget.EditText
android.widget.AutoCompleteTextView

// Layout/main.xml

<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
<AutoCompleteTextView
android:id="@+id/hello11"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_x="1px"
android:layout_y="30px"
android:text="@string/helloSecond" />
</AbsoluteLayout>

// values/strings.xml

<string-array name="planets_array">
<item>Mercury</item>
<item>Venus</item>
<item>Earth</item>
<item>Mars</item>
<item>Jupiter</item>
<item>Saturn</item>
<item>Uranus</item>
<item>Neptune</item>
</string-array>

// MyActivity.java

// Get a reference to the AutoCompleteTextView in the layout
AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.hello11);
// Get the string array
String[] countries = getResources().getStringArray(R.array.planets_array);
// Create the adapter and set it to the AutoCompleteTextView
ArrayAdapter<String> adapter =
new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, countries);
textView.setAdapter(adapter);

 

 

 

 

posted on 2012-07-21 14:52  SuperbookKing  阅读(317)  评论(0)    收藏  举报