The Enter key of the soft keyboard displays the "complete" text by default. We know that pressing Enter indicates that the preparations are complete and what to go. For example, in a search, we Enter the text to be searched, and press Enter to search. However, the default Enter key displays the "finished" text, which is not suitable, it does not conform to the search syntax. If the word "Search" is displayed or a search icon is displayed, how nice is it. Facts have proved that our ideas are reasonable, and Android also provides us with such features. Set android: imeOptions to change the default "finish" text. Here are several common constant values:
ActionUnspecified is not specified. The corresponding constant EditorInfo. IME_ACTION_UNSPECIFIED. effect:
ActionNone has no action. The corresponding constant EditorInfo. IME_ACTION_NONE has the following effect:
ActionGo goes, corresponding to the constant EditorInfo. IME_ACTION_GO effect:
ActionSearch search, corresponding to the constant EditorInfo. IME_ACTION_SEARCH:
Send actionSend, corresponding to the constant EditorInfo. IME_ACTION_SEND effect:
Next to actionNext, corresponding to the constant EditorInfo. IME_ACTION_NEXT:
ActionDone is complete, corresponding to the constant EditorInfo. IME_ACTION_DONE effect:
The following search is used as an example to demonstrate how to modify main. xml:
Xml Code
<? 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>
<? 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> modify HelloEditText as follows:
Java code
Package com. flysnow;
Import android. app. Activity;
Import android. OS. Bundle;
Import android. view. KeyEvent;
Import android. widget. EditText;
Import android. widget. TextView;
Import android. widget. Toast;
Import android. widget. TextView. oneditexceptionlistener;
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. setoneditexceptionlistener (new oneditexceptionlistener (){
@ Override
Public boolean oneditexception (TextView v, int actionId, KeyEvent event ){
Toast. makeText (HelloEditText. this, String. valueOf (actionId), Toast. LENGTH_SHORT). show ();
Return false;
}
});
}
}
Package com. flysnow;
Import android. app. Activity;
Import android. OS. Bundle;
Import android. view. KeyEvent;
Import android. widget. EditText;
Import android. widget. TextView;
Import android. widget. Toast;
Import android. widget. TextView. oneditexceptionlistener;
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. setoneditexceptionlistener (new oneditexceptionlistener (){
@ Override
Public boolean oneditexception (TextView v, int actionId, KeyEvent event ){
Toast. makeText (HelloEditText. this, String. valueOf (actionId), Toast. LENGTH_SHORT). show ();
Return false;
}
});
}
} Run the program. Click Enter (that is, the soft keyboard button of the search icon) to display the actionId. Each of the above settings corresponds to a constant. The actionId here is the constant value.
7. EditText value, full selection, partial selection, and retrieval of selected text
The following example shows how to modify the value of EditText, select all, partially select, and obtain the selected text. main. xml as follows:
Xml Code
<? 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 = "value"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"/>
<Button
Android: id = "@ + id/btn_all"
Android: text = "select all"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"/>
<Button
Android: id = "@ + id/btn_select"
Android: text = "starting from 2nd characters"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"/>
<Button
Android: id = "@ + id/btn_get_select"
Android: text = "getting selected text"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"/>
</LinearLayout>
<? 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 = "value"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"/>
<Button
Android: id = "@ + id/btn_all"
Android: text = "select all"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"/>
<Button
Android: id = "@ + id/btn_select"
Android: text = "starting from 2nd characters"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"/>
<Button
Android: id = "@ + id/btn_get_select"
Android: text = "getting selected text"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"/>
</LinearLayout>
Modify HelloEditText as follows:
Java code
Package com. flysnow;
Import android. app. Activity;
Import android. OS. Bundle;
Import android. text. Editable;
Import android. text. Selection;
Import android. view. KeyEvent;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. widget. Button;
Import android. widget. EditText;
Import android. widget. TextView;
Import android. widget. Toast;
Import android. widget. TextView. oneditexceptionlistener;
/**
* EditText demo
* @ Author feixue is heartless
* @ Since 2010-11-29
*/
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 );
Final EditText editText = (EditText) findViewById (R. id. edit_text );
// Listen to the Enter key
EditText. setoneditexceptionlistener (new oneditexceptionlistener (){
@ Override
Public boolean oneditexception (TextView v, int actionId, KeyEvent event ){
Toast. makeText (HelloEditText. this, String. valueOf (actionId), Toast. LENGTH_SHORT). show ();
Return false;
}
});
// Obtain EditText
Button getValue = (Button) findViewById (R. id. btn_get_value );
GetValue. setOnClickListener (new OnClickListener (){
@ Override
Public void onClick (View v ){
Toast. makeText (HelloEditText. this, editText. getText (). toString (), Toast. LENGTH_SHORT). show ();
}
});
// Select all EditText
Button all = (Button) findViewById (R. id. btn_all );
All. setOnClickListener (new OnClickListener (){
@ Override
Public void onClick (View v ){
EditText. selectAll ();
}
});
// Select EditText starting from 2nd characters
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 ());
}
});
// Obtain the selected text
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 (HelloEditText. this, selectText, Toast. LENGTH_SHORT). show ();
}
});
}
/**
* Exchange two indexes
* @ Param start Index
* @ Param end the index
*/
Protected void switchIndex (int start, int end ){
Int temp = start;
Start = end;
End = temp;
}
}
Package com. flysnow;
Import android. app. Activity;
Import android. OS. Bundle;
Import android. text. Editable;
Import android. text. Selection;
Import android. view. KeyEvent;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. widget. Button;
Import android. widget. EditText;
Import android. widget. TextView;
Import android. widget. Toast;
Import android. widget. TextView. oneditexceptionlistener;
/**
* EditText demo
* @ Author feixue is heartless
* @ Since 2010-11-29
*/
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 );
Final EditText editText = (EditText) findViewById (R. id. edit_text );
// Listen to the Enter key
EditText. setoneditexceptionlistener (new oneditexceptionlistener (){
@ Override
Public boolean oneditexception (TextView v, int actionId, KeyEvent event ){
Toast. makeText (HelloEditText. this, String. valueOf (actionId), Toast. LENGTH_SHORT). show ();
Return false;
}
});
// Obtain EditText
Button getValue = (Button) findViewById (R. id. btn_get_value );
GetValue. setOnClickListener (new OnClickListener (){
@ Override
Public void onClick (View v ){
Toast. makeText (HelloEditText. this, editText. getText (). toString (), Toast. LENGTH_SHORT). show ();
}
});
// Select all EditText
Button all = (Button) findViewById (R. id. btn_all );
All. setOnClickListener (new OnClickListener (){
@ Override
Public void onClick (View v ){
EditText. selectAll ();
}
});
// Select EditText starting from 2nd characters
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 ());
}
});
// Obtain the selected text
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 (HelloEditText. this, selectText, Toast. LENGTH_SHORT). show ();
}
});
}
/**
* Exchange two indexes
* @ Param start Index
* @ Param end the index
*/
Protected void switchIndex (int start, int end ){
Int temp = start;
Start = end;
End = temp;
}
}
The running effect is as follows:
You can enter the text and click the button below to test.
8. Summary
This section details most of the features and common functions of EditText, such as frequently-used password boxes and getting values. I have not updated these busy days. This update is a long one. It can be digested for a while.