Android: Daily Learning Notes (7) ——— Explore UI Development (1)

Source: Internet
Author: User

Android: Daily Learning Notes (7) ——— Explore UI Development (1) How to use common controls TextView

Description: TextView is one of the simplest controls in Android and is commonly used to display a text message on the interface.

Code:

 <  textview  android:id  =" @+id/text_view "   Android:layout_width  = "Match_parent"   Android:layout_height  = "wrap_content"    Android:text  = "Hello world!"    android:gravity  = "center"    android:textsize  = "24SP"   Android:textcolor  = "#00FF00"  />  

Description

1.match_parent means that the current control is the same size as its parent layout, which is the parent layout that determines the size of the current control.

2.wrap_content means that the content in the control determines the size of the control.

3.android:gravity can be used to specify how text is aligned.

Button

Description: A button consists of text or an icon (or both text and an icon) This communicates what action occurs when the user touches It.

(a button is a control that can contain text or icons or text and icon information to feed back the user's Click events.)

Code:

Depending on whether your want a button with text, an icon, or both, you can create the button in your layout in three ways :

  • with text, using the  Button  class:
     <  button  android:layout_width  =" wrap_content "  Android:layout_height  = "Wrap_content"   Android:text  = "@string/button_text"   ...  />  
  • with an icon, using the  ImageButton  class:
     <  imagebutton  android:layout_width  = "Wrap_content"   Android:layout_height  = "Wrap_content"   android:src  = "@drawable/button_icon"   ...  />  
  • With text and a icon, using the Button class with the android:drawableLeft attribute:
    < Button     Android:layout_width = "Wrap_content"     android:layout_height= "Wrap_content"    android:text= "@string/button_ Text "    android:drawableleft=" @drawable/button_icon "    /> 

You can add the following code to resolve all letters that are capitalized:

Android:textallcaps= "false"

Let the button respond to an event

1. First method: Binding events in XML

To define the Click event handler for a button, add the android:onClick attribute to the <Button> element in your XML layout. The value for this attribute must is the name of the the method you want to call in response to a click event. The Activity hosting the layout must then implement the corresponding method.

For example, here's a layout with a button using android:onClick :

<? XML version= "1.0" encoding= "Utf-8" ?> <  xmlns:android= "http://schemas.android.com/apk/res/android"    android:id = "@+id/button_send"     android:layout_width= "Wrap_content"    android:layout_height= " Wrap_content "    android:text=" @string/button_send "    Android:onclick  = "SendMessage"/>

Within Activity the that's hosts this layout, the following method handles the Click event:

/***/publicvoid  sendMessage (view view)    {// do Something in response to button click}

The method you declare in the android:onClick attribute must has a signature exactly as shown above. Specifically, the method must:

    • Be public
    • Return void
    • Define A as its only parameter (this would be the, that is View View clicked)

2. Second method: Using an Onclicklistener

You can also declare the click event handler programmatically rather than in an XML layout. This might is necessary if you instantiate the "at runtime" or "need to declare the" Button click Behavior in a Fragment SUBCL The.

To declare the event handler programmatically, create a View.OnClickListener object and assign it to the button by calling setOnClickListener(View.OnClickListener) . For example:

Button button = (Button) Findviewbyid (r.id.button_send); Button.setonclicklistener (new  View.onclicklistener () {    publicvoid  OnClick (View v) {        /// do Something in response to button click    }});

EditText

Description

Every text field expects a certain type of text input, such as an email address, phone number, or just plain text. So it's important that's specify the input type for each text field in your app so the system displays the appropriate s Oft input method (such as an on-screen keyboard).

Beyond the type of buttons available with a input method, you should specify behaviors such as whether the input method p Rovides spelling suggestions, capitalizes new sentences, and replaces the carriage return button with an action button suc H as a done or Next. This lesson shows how to specify these characteristics

Code:

    < EditText         Android:id = "@+id/phone"         android:layout_width= "Fill_parent"        android:layout_height= "Wrap_ Content "        android:hint=" default hint message "        android:maxlines=" 2 "        android:inputtype=" Phone "/>    

Description

The Android:inputtype property determines what kind of information this text box should receive and what information it should have. If the input sends the default switch to the mobile phone number display.
Constant Value Description
Date 14 For entering a date. Corresponds to TYPE_CLASS_DATETIME | TYPE_DATETIME_VARIATION_DATE .
Datetime 4 For entering a date and time. Corresponds to TYPE_CLASS_DATETIME | TYPE_DATETIME_VARIATION_NORMAL .
None 0 There is no content type. The text is not editable.
Number 2 A Numeric Only field. Corresponds to TYPE_CLASS_NUMBER | TYPE_NUMBER_VARIATION_NORMAL .
Numberdecimal 2002 Can is combined with and it other options to allow number a decimal (fractional) number. Corresponds to TYPE_CLASS_NUMBER | TYPE_NUMBER_FLAG_DECIMAL .
Numberpassword 12 A Numeric Password field. Corresponds to TYPE_CLASS_NUMBER | TYPE_NUMBER_VARIATION_PASSWORD .
numbersigned 1002 Can is combined with and it other options to allow number a signed number. Corresponds to TYPE_CLASS_NUMBER | TYPE_NUMBER_FLAG_SIGNED .
Phone 3 For entering a phone number. Corresponds to TYPE_CLASS_PHONE .
Text 1 Just plain old text. Corresponds to TYPE_CLASS_TEXT | TYPE_TEXT_VARIATION_NORMAL .
Textautocomplete 10001 Can is combined with and it variations to specify the This field would be a text doing its own auto-completion and talking With the input method appropriately. Corresponds to TYPE_TEXT_FLAG_AUTO_COMPLETE .
Textautocorrect 8001 Can is combined with and it text variations to request auto-correction of text being input. Corresponds to TYPE_TEXT_FLAG_AUTO_CORRECT .
Textcapcharacters 1001 Can is combined with and it text variations to request capitalization of all characters. Corresponds to TYPE_TEXT_FLAG_CAP_CHARACTERS .
Textcapsentences 4001 Can is combined with and it variations to request capitalization of the first text character of every sentence. Corresponds to TYPE_TEXT_FLAG_CAP_SENTENCES .
Textcapwords 2001 Can is combined with and it variations to request capitalization of the first text character of every word. Corresponds to TYPE_TEXT_FLAG_CAP_WORDS .
Textemailaddress 21st Text that'll be is used as an e-mail address. Corresponds to TYPE_CLASS_TEXT | TYPE_TEXT_VARIATION_EMAIL_ADDRESS .
Textemailsubject 31 Text that's being supplied as the subject of an e-mail. Corresponds to TYPE_CLASS_TEXT | TYPE_TEXT_VARIATION_EMAIL_SUBJECT .
TextFilter B1 Text is filtering some and other data. Corresponds to TYPE_CLASS_TEXT | TYPE_TEXT_VARIATION_FILTER .
Textimemultiline 40001 Can is combined with and it text variations to indicate this though the regular text view should not being multiple lines, The IME should provide multiple lines if it can. Corresponds to TYPE_TEXT_FLAG_IME_MULTI_LINE .
Textlongmessage 51 The Text is the content of a long message. Corresponds to TYPE_CLASS_TEXT | TYPE_TEXT_VARIATION_LONG_MESSAGE .
Textmultiline 20001 Can is combined with and it variations to allow text multiple lines of text in the field. IF This flag was not set, the text field would be constrained to a. Corresponds to TYPE_TEXT_FLAG_MULTI_LINE .
Textnosuggestions 80001 Can is combined with and it variations to indicate, the text IME should not show any dictionary-based word suggestio Ns. Corresponds to TYPE_TEXT_FLAG_NO_SUGGESTIONS .
Textpassword 81 Text is a password. Corresponds to TYPE_CLASS_TEXT | TYPE_TEXT_VARIATION_PASSWORD .
Textpersonname 61 The Text is the name of a person. Corresponds to TYPE_CLASS_TEXT | TYPE_TEXT_VARIATION_PERSON_NAME .
Textphonetic C1 The Text is for phonetic pronunciation, such as a phonetic the name field in a contact entry. Corresponds to TYPE_CLASS_TEXT | TYPE_TEXT_VARIATION_PHONETIC .
Textpostaladdress 71 Text is being supplied as a postal mailing address. Corresponds to TYPE_CLASS_TEXT | TYPE_TEXT_VARIATION_POSTAL_ADDRESS .
Textshortmessage 41 The Text of the content of a short message. Corresponds to TYPE_CLASS_TEXT | TYPE_TEXT_VARIATION_SHORT_MESSAGE .
Texturi 11 Text that would be used as a URI. Corresponds to TYPE_CLASS_TEXT | TYPE_TEXT_VARIATION_URI .
Textvisiblepassword 91 Text that's a password that should be visible. Corresponds to TYPE_CLASS_TEXT | TYPE_TEXT_VARIATION_VISIBLE_PASSWORD .
Textwebedittext A1 Text is being supplied as text in a Web form. Corresponds to TYPE_CLASS_TEXT | TYPE_TEXT_VARIATION_WEB_EDIT_TEXT .
Textwebemailaddress D1 Text that would be is used as an e-mail address on a Web form. Corresponds to TYPE_CLASS_TEXT | TYPE_TEXT_VARIATION_WEB_EMAIL_ADDRESS .
Textwebpassword E1 Text that would be is used as a password on a Web form. Corresponds to TYPE_CLASS_TEXT | TYPE_TEXT_VARIATION_WEB_PASSWORD .
Time 24 For entering a time. Corresponds to TYPE_CLASS_DATETIME | TYPE_DATETIME_VARIATION_TIME .

Specify Input Method actions

Most soft input methods provide a user action button in the bottom corner this ' s appropriate for the current text field. By default, the system uses this button for either a Next or done action unless your text field allows M Ulti-line text (such android:inputType="textMultiLine" as with), in which case the action button is a carriage return. However, you can specify additional actions that might is more appropriate for your text field, such as Send or C4>go.

To specify the keyboard action button, use the android:imeOptions attribute with an action value such as "actionSend" or "actionSearch" . For example:

Figure 4. The Send button appears when you declare android:imeOptions="actionSend" .

< 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"/>

You can then listen for presses on the action button by defining a  textview.oneditoractionlistener &NB Sp;for the  EditText  element. In your listener, respond to the appropriate IME action ID defined in the  editorinfo  class, such as   ime_action_send . For example:

 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; }})

Android: Daily Learning Notes (7) ——— Explore UI Development (1)

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.