Android study note 13: Use of EditText

Source: Internet
Author: User

EditText is a control frequently used in Android development. It is mainly used to obtain user input content.

1. EditText common attributes

EditText inherits from TextView, so EditText also has attributes and methods in TextView. EditText has the following common attributes:

Android: capitalize = "sentences" // you can specify an uppercase letter for an English letter. sentences is only the first letter in uppercase. The size of the first letter of a word in a word is separated by spaces. characters is capitalized for each English letter.

Android: cursorVisible = "true" // set the cursor to show or hide. The default value is show.

Android: digits = "0123456789.abc#" // you can specify the characters that can be entered.

Android: drawableTop // output a drawable on the top of the Text

Android: drawableBottom // output a drawable under Text

Android: drawableLeft // output a drawable on the left of Text

Android: drawableRight // output a drawable on the Right of Text

Android: editable = "true" // sets whether to edit

Android: ellipsize = "start" // sets the display mode of the control when the text is too long. Start ellipsis is displayed at the beginning; end ellipsis is displayed at the end; middle ellipsis is displayed in the middle; marquee

Android: hint = "Enter the content! "// Set the default prompt information displayed in EditText

Android: inputType = "none" sets the text type to help the input method display the appropriate keyboard type. Configurable parameters are as follows: none, text, textCapCharacters uppercase letters, textCapWords uppercase letters, textCapSentences only the first letter size, textAutoCorrect, textAutoComplete Automatic completion, textMultiLine input, textImeMultiLine input multiple lines, textNoSuggestions do not prompt, textEmailAddress email address, textEmailSubject Email Subject, text0000message Short Message, textLongMessage long message, textPersonName, textPostalAddress, textPassword, textVisiblePassword visible password, textWebEditText as the webpage form text, textFilte text filtering textPhonetic Pinyin input, numberSigned digit format, numberDecimal floating point format with decimal point, phone number, datetime time date, date, time

Android: numeric = "integer" // set the input numeric type, integer, decimal, signed integer

Android: textColor = "# ff8c00" // set the font color

Android: textStyle = "bold" // you can specify the font type as bold, italic and bolditalic.

Android: textSize = "20dip" // set the font size

Android: textScaleX = "1.5" // set the font spacing

Android: singleLine = "true" // set single-line input mode. text cannot be wrapped automatically.


2. Set the Enter key icon
In Android, the Enter key icon of the soft keyboard displays "complete" text by default, as shown in 1.

 


Figure 1 default keypad

Imagine that when we have completed the input in EditText and want to search by using the entered content as a keyword, we need to press the Enter button on the "finish" icon, obviously this is not in line with a good user experience design.

So how can I change the icon of the Enter button? Android provides android: imeOptions to implement this function.

Android: imeOptions common parameters include normal, actionUnspecified, actionNone, actionGo, and actionSearch ), actionSend (send), actionNext (next), actionDone (complete), flagNoExtractUi, flagNoAccessoryAction, and flagNoEnterAction. The corresponding Enter key Icon 2 is shown.

Figure 2 Enter key icon Style

 

3. Enter the QQ emoticons image in EditText.
Enter the QQ emoticons image in EditText and use the SpannableString and ImageSpan classes.

The SpannableString class is inherited from android. text. Spanned and serves to append and separate immutable text content. Common Methods of the SpannableString class are as follows:

MSpannableString. CharAt (int I); // returns the bytes at the specified index I.

MSpannableString. getChars (int start, int end, char [] dest, int off); // gets the string from start to end

MSpannableString. getSpans (int queryStart, int queryEnd, Class kind );

MSpannableString. length (); // number of returned bytes

MSpannableString. setSpan (Object what, int start, int end, int flags );//

MSpannableString. toString (); // returns a string.

The ImageSpan class is used to replace specified text with images.

The specific implementation method is as follows:

SpannableString
1 Bitmap mBitmap = BitmapFactory. decodeResource (getResources (), resourceId );
2 ImageSpan mImageSpan = new ImageSpan (MainActivity. this, mBitmap );
3 SpannableString mSpannableString = new SpannableString ("qq ");
4 mSpannableString. setSpan (mImageSpan, 0, 2, Spannable. SPAN_EXCLUSIVE_EXCLUSIVE );
5 mEditText. append (mSpannableString );
For detailed usage of SpannableString, see:

[

4. input content verification in EditText

In normal applications, when users input obvious error content in EditText and select "Submit", in order to meet the user experience, A friendly message similar to "enter the correct content" should be displayed.

How can this function be implemented? Android provides the mEditText. setError () method to implement this function. First, you need to use the mEditText. getText () method to obtain the content in EditText, then determine its content, and finally display the judgment result using the mEditText. setError () method.

The specific implementation method is as follows:

EditText. setError ()
1 String mString = mEditText2.getText (). toString ();
2 if (mString = null | mString. trim (). equals ("")){
3 mEditText2.setError ("Enter the content! ");
4 return;
5}
 

5. Use EditText instances

In this example, the function of inputting content in the QQ emoticons image and EditText in EditText is verified. In instance 1, each time you press the "Enter QQ emoticons" button, you can enter a random QQ emoticons image in EditText. In instance 2, if no content is entered in EditText, press the "Submit" button and the "Enter content!" dialog box appears !" . The effect of instance 3 is shown.

 

Figure 3 instance

The instance source code is as follows:

MainActivity. java
1 package com. example. android_edittext;
2
3 import java. lang. reflect. Field;
4 import java. util. Random;
5 import android. OS. Bundle;
6 import android. app. Activity;
7 import android. graphics. Bitmap;
8 import android. graphics. BitmapFactory;
9 import android. text. Spannable;
10 import android. text. SpannableString;
11 import android. text. style. ImageSpan;
12 import android. view. View;
13 import android. widget. Button;
14 import android. widget. EditText;
15
16 public class MainActivity extends Activity {
17
18 private EditText mEditText_qq = null; // QQ emoticons input display box
19 private Button mButton_qq = null; // QQ emoticons input Button
20 private EditText mEditText_check = null; // content verification box
21 private Button mButton_check = null; // submit Button
22
23 @ Override
24 public void onCreate (Bundle savedInstanceState ){
25 super. onCreate (savedInstanceState );
26 setContentView (R. layout. activity_main );
27
28 mEditText_qq = (EditText) this. findViewById (R. id. editText_qq );
29 mButton_qq = (Button) this. findViewById (R. id. button_qq );
30 mEditText_check = (EditText) this. findViewById (R. id. editText_check );
31 mButton_check = (Button) this. findViewById (R. id. button_check );
32
33 // QQ expression input button listener
34 mButton_qq.setOnClickListener (new View. OnClickListener (){
35 public void onClick (View v ){
36 int randomId = 1 + new Random (). nextInt (6); // Random Number 1-6
37 try {
38 Field field = R. drawable. class. getDeclaredField ("qq" + randomId );
39 int resourceId = Integer. parseInt (field. get (null). toString (); // obtain the image resource Id
40 Bitmap mBitmap = BitmapFactory. decodeResource (getResources (), resourceId );
41 ImageSpan mImageSpan = new ImageSpan (MainActivity. this, mBitmap );
42 SpannableString mSpannableString = new SpannableString ("qq ");
43 mSpannableString. setSpan (mImageSpan, 0, 2, Spannable. SPAN_EXCLUSIVE_EXCLUSIVE );
44 mEditText_qq.append (mSpannableString );
45} catch (Exception e ){
46
47}
48}
49 });
50
51 // submit button listener
52 mButton_check.setOnClickListener (new View. OnClickListener (){
53 public void onClick (View v ){
54 String mString = mEditText_check.getText (). toString ();
55 if (mString = null | mString. trim (). equals ("") {// determines whether the content in EditText is null.
56 mEditText_check.setError ("Enter the content! ");
57 return;
58}
59}
60 });
61}
62
63}
The xml file is as follows:

Activity_main.xml
1 <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
2 xmlns: tools = "http://schemas.android.com/tools"
3 android: orientation = "vertical"
4 android: layout_width = "match_parent"
5 android: layout_height = "match_parent">
6
7 <! -- Input QQ emoticons in EditText -->
8 <TextView
9 android: layout_marginTop = "5dp"
10 android: layout_width = "match_parent"
11 android: layout_height = "wrap_content"
12 android: text = "@ string/string_textview1">
13 </TextView>
14
15 <EditText
16 android: id = "@ + id/editText_qq"
17 android: hint = "@ string/string_editText"
18 android: layout_width = "match_parent"
19 android: layout_height = "wrap_content"
20 android: layout_marginTop = "5dp">
21 </EditText>
22
23 <Button
24 android: id = "@ + id/button_qq"
25 android: layout_width = "match_parent"
26 android: layout_height = "wrap_content"
27 android: text = "@ string/string_button1">
28 </Button>
29
30 <! -- Input content verification in EditText -->
31 <TextView
32 android: layout_marginTop = "20dp"
33 android: layout_width = "match_parent"
34 android: layout_height = "wrap_content"
35 android: text = "@ string/string_textview2">
36 </TextView>
37
38 <EditText
39 android: id = "@ + id/editText_check"
40 android: layout_width = "match_parent"
41 android: layout_height = "wrap_content"
42 android: digits = "0123456789"
43 android: inputType = "number | textCapCharacters">
44 </EditText>
45
46 <Button
47 android: id = "@ + id/button_check"
48 android: layout_width = "match_parent"
49 android: layout_height = "wrap_content"
50 android: text = "@ string/string_button2">
51 </Button>
52
53 </LinearLayout>
 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.