"Turn" Pro Android Learning Note (14): User interface and Control (2): Text class control

Source: Internet
Author: User
Tags xml attribute xml example

Directory (?) [-]

    1. TextView
      1. Example 1 Setting the Autolink property in XML
      2. Example 2 Setting the Autolink property in code
    2. EditText
    3. Autocompletetextview
    4. Multiautocompletetextview
TextView

TextView has been used a lot before, direct display, relatively simple. But we can with the "Autolink" attribute, so that users can click on a network connection, phone number, email address, map address, through the system application to open them. In addition, there are other font properties, Minlines,maxlines and so on, all very well understood, you can read the Android Learning Note series.

Example 1: Setting the Autolink property in XML

We set the <textview in XML ... android:autolink= "Email|web"/>, you can set the Autolink to have All,email,map,none (this is the default, which means no automatic linking), Phone,web.

Example 2: Setting the Autolink property in code

Tv. Setautolinkmask (linkify.all);//Set all automatic links
Tv. Setautolinkmask (linkify.phone_numberslinkify.web_urls); Set up the web and phone
Tv.settext (...); //Note that the setting property should be before SetText (), otherwise the property will not function on the text that has been set

In some cases, such as only requiring temporary effect, we may need to add the Link property after setting the text, using the following method. Linkify.addlinkes () can also be used for spannable controls.

Tv.settext ("My phone:87654321");
linkify.addlinks (TV, Linkify.all); //Be aware that linkify.addlinkes () will not work until SetText () is placed

EditText

EditText is a subclass of TextView. EditText does not have the ability to function on the Internet, such as editing a blog to enter images, but it also offers rich features for mobile phones. See reference for details, here are 2 more interesting features.

Hint You can display the hint in gray font in EditText, which disappears when the user taps the input. In XML, the following settings are set:

<edittext .... android:hint= "@string/ui_text_et_hint"/>

EditText inherited an important attribute of TextView is inputtype. We can set Textautocorrect to detect spelling errors, which can be capitalized by textcapwords from the first letter set to each word. You can also set the required input format, such as Texturi,textemailaddress,textpassword, and so on.

<edittext .... android:inputtype= "textpersonname|textcapwords"/>

In the old version, many properties were moved to InputType settings, and if we set inputtype in XML, these outdated properties would not work, such as Android:capitalize.

The new and old version of the need to pay attention to multiple lines of the problem, in the old version, the default is more than one line, and when the row can not be displayed all, automatically on the next line, such as a single line to display the need to set android:singleline. However, when the XML attribute is set to InputType, the default is a single line, and if more than one line is required, set android:inputtype= "Textmultiline".

Autocompletetextview

The auto-complete control example is as above. The XML is as follows:

<textview .../>
<autocompletetextview android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
Android:id= "@+id/ui_text_auto"/>

The code is as follows:

Autocompletetextview ACTV = (autocompletetextview) Findviewbyid (R.id.ui_text_auto);
Create a Adpater class that contains the suggested items, and displays the suggested control
Arrayadapter<String> AA = new Arrayadapter<string> (This,
Android. R.layout.simple_dropdown_item_1line,//control that displays the suggested content
New string[]{"Chinese", "中文版", "French", "Greek"}); Suggested items
Association Adapter
Actv.setadapter (AA);

Multiautocompletetextview

Autocompletetextview matches the entire text view, and sometimes we want to be able to match each word like a sentence, which requires multiautocompletetextview.

The XML example is as follows:

<multiautocompletetextview android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
Android:id= "@+id/ui_text_auto_multi"/>

The code is as follows. It is also important to set the suggested item through adapter, and to display the control of the proposed item, and to associate it with Setadapter (). In addition, it is necessary to tell the system when to re-match by Tokenizer.

Multiautocompletetextview MT = (multiautocompletetextview) Findviewbyid (R.id.ui_text_auto_multi);
arrayadapter<string> AA = new Arrayadapter<string> (This,
Android. R.layout.simple_dropdown_item_1line,
New string[]{"Chinese", "中文版", "French", "Greek", "Spainish"});
Mt.setadapter (AA);
by Settokenizer () tell me what to divide and when to start matching. The partitioning method is provided by implementing the Multiautocompletetextview.tokenizer interface. The system has implemented two, one is commatokenizer, separated by commas (including a space), and the other is Rfc822tokenizer, used in email format. If you want to implement it yourself, you can refer to the two examples of system implementations.
Mt.Settokenizer(New Multiautocompletetextview.commatokenizer ());

RELATED Links: My Android development related articles

"Turn" Pro Android Learning Note (14): User interface and Control (2): Text class control

Related Article

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.