EditText的一些属性
来源:互联网 发布:摄像头拍照软件 编辑:程序博客网 时间:2024/04/29 17:28
我在IM即时通讯项目中,想在输入文字的时候,输入法上面显示发送,然后去响应我的发送消息的事件,但是不知道怎么做,于是我去查下了EdiText的文档(Google guide),获得了下面的消息:
Specifying Keyboard Actions(指定action)
In addition to changing the keyboard's input type, Android allows you to specify an action to be made when users have completed their input. The action specifies the button that appears in place of the carriage return key and the action to be made, such as "Search" or "Send."
You can specify the action by setting theandroid:imeOptions
attribute. For example, here's how you can specify the Send action:
<EditText android:id="@+id/search" android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="@string/search_hint" android:inputType="text" android:imeOptions="actionSend" />
上面话的意思是只要在EditText下制定这样的一个属性:android:imeOptions
让他的值为actionSend即可。
同样如果添加搜索,只需要指定该属性的值为:actionSearch即可。
If you do not explicitly specify an input action then the system attempts to determine if there are any subsequent android:focusable
fields. If any focusable fields are found following this one, the system applies the (@code actionNext} action to the current EditText
so the user can select Next to move to the next field. If there's no subsequent focusable field, the system applies the"actionDone"
action. You can override this by setting the android:imeOptions
attribute to any other value such as "actionSend"
or "actionSearch"
or suppress the default behavior by using the"actionNone"
action.
Responding to action button events(相应事件)
If you have specified a keyboard action for the input method using android:imeOptions
attribute (such as "actionSend"
), you can listen for the specific action event using anTextView.OnEditorActionListener
. The TextView.OnEditorActionListener
interface provides a callback method called onEditorAction()
that indicates the action type invoked with an action ID such asIME_ACTION_SEND
or IME_ACTION_SEARCH
.
使用TextView.OnEditorActionListener来处理监听事件。
For example, here's how you can listen for when the user clicks the Send button on the keyboard:
EditText editText = (EditText) findViewById(R.id.search);editText.setOnEditorActionListener(new OnEditorActionListener() { @Override public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { boolean handled = false; if (actionId == EditorInfo.IME_ACTION_SEND) { sendMessage(); handled = true; } return handled; }});
Setting a custom action button label(给一个label)
If the keyboard is too large to reasonably share space with the underlying application (such as when a handset device is in landscape orientation) then fullscreen ("extract mode") is triggered. In this mode, a labeled action button is displayed next to the input. You can customize the text of this button by setting the android:imeActionLabel
attribute:
<EditText android:id="@+id/launch_codes" android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="@string/enter_launch_codes" android:inputType="number" android:imeActionLabel="@string/launch" />
Adding Other Keyboard Flags
In addition to the actions you can specify with the android:imeOptions
attribute, you can add additional flags to specify other keyboard behaviors. All available flags are listed along with the actions in the android:imeOptions
documentation.
For example, figure 5 shows how the system enables a fullscreen text field when a handset device is in landscape orientation (or the screen space is otherwise constrained for space). You can disable the fullscreen input mode with flagNoExtractUi
in the android:imeOptions
attribute, as shown in figure 6.
Providing Auto-complete Suggestions(自动提示)
If you want to provide suggestions to users as they type, you can use a subclass of EditText
calledAutoCompleteTextView
. To implement auto-complete, you must specify an (@link android.widget.Adapter) that provides the text suggestions. There are several kinds of adapters available, depending on where the data is coming from, such as from a database or an array.
The following procedure describes how to set up an AutoCompleteTextView
that provides suggestions from an array, using ArrayAdapter
:
- Add the
AutoCompleteTextView
to your layout. Here's a layout with only the text field:<?xml version="1.0" encoding="utf-8"?><AutoCompleteTextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/autocomplete_country" android:layout_width="fill_parent" android:layout_height="wrap_content" />
- 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
):<?xml version="1.0" encoding="utf-8"?><resources> <string-array name="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>
- In your
Activity
orFragment
, use the following code to specify the adapter that supplies the suggestions:// Get a reference to the AutoCompleteTextView in the layoutAutoCompleteTextView 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 = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, countries);textView.setAdapter(adapter);
Here, a new
ArrayAdapter
is initialized to bind each item in theCOUNTRIES
string array to aTextView
that exists in thesimple_list_item_1
layout (this is a layout provided by Android that provides a standard appearance for text in a list).Then assign the adapter to the
AutoCompleteTextView
by callingsetAdapter()
.
- EditText的一些属性
- EditText的一些属性
- EditText的一些属性
- EditText的一些属性
- EditText 的一些属性设置
- EditText的一些属性设置
- EditText的属性InputType的一些经验
- textView 和 edittext 的一些属性
- 有关Android中EditText的一些属性
- EditText中一些属性的用法
- EditText一些属性
- Android EditText 一些属性设置
- EditText输入校验和一些相关的属性
- 16-Android界面控件之EditText的一些属性
- EditText 的一些软键盘属性和回调
- Android EditText限制输入一些固定字符的属性
- EditText的属性说明
- EditText的属性
- ALV Header
- Memcached学习资料
- C++primer plus第四章
- ubuntu下搭建tftp服务器
- jQuery UI dialog 的使用
- EditText的一些属性
- 容器模式插件(一)——解析APK
- online_judge_1475改
- 11.AOP 基础
- CentOS 安装jdk1.7 64位
- 无约束最优化四
- 开启新的篇章
- ffmpeg 的 configure 文件的编译参数
- Wireshark图解教程(简介、抓包、过滤器)