Android:日常學習筆記(7)———探究UI開發(1)

來源:互聯網
上載者:User

標籤:資訊   regular   utf-8   int   editor   show   color   parameter   ext   

Android:日常學習筆記(7)———探究UI開發(1)常用控制項的使用方法TextView

說明:TextView是安卓中最為簡單的一個控制項,常用來在介面上顯示一段文本資訊。

代碼:

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

說明:

  1.match_parent表示讓當前控制項的大小與其父布局大小一樣,也就是由父布局來決定當前控制項的大小。

  2.wrap_content表示讓控制項中的內容決定控制項的大小。

  3.android:gravity可以用來指定文本的對齊。

Button

說明:A button consists of text or an icon (or both text and an icon) that communicates what action occurs when the user touches it.

(按鈕是一種可以包含文本表徵圖文本和表徵圖資訊的用來反饋使用者點擊事件的控制項)

代碼:

  Depending on whether you 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 an 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"    ... />

可以加入下句代碼解決所有字母都是大寫的問題:

android:textAllCaps="false"

 讓按鈕響應事件

1.第一種方法:在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 be the name of 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"?><Button 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 the Activity that hosts this layout, the following method handles the click event:

/** Called when the user touches the button */public void sendMessage(View view) {    // Do something in response to button click}

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

  • Be public
  • Return void
  • Define a View as its only parameter (this will be the View that was clicked)

2.第二種方法:Using an OnClickListener

You can also declare the click event handler programmatically rather than in an XML layout. This might be necessary if you instantiate the Button at runtime or you need to declare the click behavior in a Fragment subclass.

To declare the event handler programmatically, create an 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() {    public void onClick(View v) {        // Do something in response to button click    }});

 

EditText

說明:

  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 you specify the input type for each text field in your app so the system displays the appropriate soft input method (such as an on-screen keyboard).

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

代碼:

    <EditText        android:id="@+id/phone"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:hint="預設的提示資訊"        android:maxLines="2"        android:inputType="phone" />    

 說明:

android:inputType屬性決定了這個文字框應該接收和資訊哪一類資訊。如輸入發預設切換為 手機號碼 顯示。
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 be combined with number and its other options to allow a decimal (fractional) number. Corresponds toTYPE_CLASS_NUMBER | TYPE_NUMBER_FLAG_DECIMAL.
numberPassword 12 A numeric password field. Corresponds to TYPE_CLASS_NUMBER | TYPE_NUMBER_VARIATION_PASSWORD.
numberSigned 1002 Can be combined with number and its other options to allow a signed number. Corresponds toTYPE_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 be combined with text and its variations to specify that this field will be doing its own auto-completion and talking with the input method appropriately. Corresponds to TYPE_TEXT_FLAG_AUTO_COMPLETE.
textAutoCorrect 8001 Can be combined with text and its variations to request auto-correction of text being input. Corresponds toTYPE_TEXT_FLAG_AUTO_CORRECT.
textCapCharacters 1001 Can be combined with text and its variations to request capitalization of all characters. Corresponds toTYPE_TEXT_FLAG_CAP_CHARACTERS.
textCapSentences 4001 Can be combined with text and its variations to request capitalization of the first character of every sentence. Corresponds to TYPE_TEXT_FLAG_CAP_SENTENCES.
textCapWords 2001 Can be combined with text and its variations to request capitalization of the first character of every word. Corresponds to TYPE_TEXT_FLAG_CAP_WORDS.
textEmailAddress 21 Text that will be used as an e-mail address. Corresponds to TYPE_CLASS_TEXT |TYPE_TEXT_VARIATION_EMAIL_ADDRESS.
textEmailSubject 31 Text that is being supplied as the subject of an e-mail. Corresponds to TYPE_CLASS_TEXT |TYPE_TEXT_VARIATION_EMAIL_SUBJECT.
textFilter b1 Text that is filtering some other data. Corresponds to TYPE_CLASS_TEXT | TYPE_TEXT_VARIATION_FILTER.
textImeMultiLine 40001 Can be combined with text and its variations to indicate that though the regular text view should not be multiple lines, the IME should provide multiple lines if it can. Corresponds to TYPE_TEXT_FLAG_IME_MULTI_LINE.
textLongMessage 51 Text that is the content of a long message. Corresponds to TYPE_CLASS_TEXT |TYPE_TEXT_VARIATION_LONG_MESSAGE.
textMultiLine 20001 Can be combined with text and its variations to allow multiple lines of text in the field. If this flag is not set, the text field will be constrained to a single line. Corresponds to TYPE_TEXT_FLAG_MULTI_LINE.
textNoSuggestions 80001 Can be combined with text and its variations to indicate that the IME should not show any dictionary-based word suggestions. Corresponds to TYPE_TEXT_FLAG_NO_SUGGESTIONS.
textPassword 81 Text that is a password. Corresponds to TYPE_CLASS_TEXT | TYPE_TEXT_VARIATION_PASSWORD.
textPersonName 61 Text that is the name of a person. Corresponds to TYPE_CLASS_TEXT | TYPE_TEXT_VARIATION_PERSON_NAME.
textPhonetic c1 Text that is for phonetic pronunciation, such as a phonetic name field in a contact entry. Corresponds toTYPE_CLASS_TEXT | TYPE_TEXT_VARIATION_PHONETIC.
textPostalAddress 71 Text that is being supplied as a postal mailing address. Corresponds to TYPE_CLASS_TEXT |TYPE_TEXT_VARIATION_POSTAL_ADDRESS.
textShortMessage 41 Text that is the content of a short message. Corresponds to TYPE_CLASS_TEXT |TYPE_TEXT_VARIATION_SHORT_MESSAGE.
textUri 11 Text that will be used as a URI. Corresponds to TYPE_CLASS_TEXT | TYPE_TEXT_VARIATION_URI.
textVisiblePassword 91 Text that is a password that should be visible. Corresponds to TYPE_CLASS_TEXT |TYPE_TEXT_VARIATION_VISIBLE_PASSWORD.
textWebEditText a1 Text that is being supplied as text in a web form. Corresponds to TYPE_CLASS_TEXT |TYPE_TEXT_VARIATION_WEB_EDIT_TEXT.
textWebEmailAddress d1 Text that will be 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 will be 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.

指定IME行動

Most soft input methods provide a user action button in the bottom corner that‘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 multi-line text (such as with android:inputType="textMultiLine"), in which case the action button is a carriage return. However, you can specify additional actions that might be more appropriate for your text field, such as Send or 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 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:日常學習筆記(7)———探究UI開發(1)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.