iT邦幫忙

2019 iT 邦幫忙鐵人賽

DAY 8
2
Software Development

[Andriod] Andriod Studio 從入門到進入狀況系列 第 8

[Day 07] 我的第一個Android程式 - BMI程式設計(二) 簡介EditText的inputType

今天的主題是 改變輸入框的inputType,

首先打開昨天的專案檔,
讓我們看到EditText這邊,

 <EditText
            android:id="@+id/height"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="numberDecimal" />

有沒有看到這個inputType?

android:inputType="numberDecimal"

如果我們沒有指定,
表示他可以任意輸入,
但是在我們的程式當中,
我們只希望他輸入數字而已,
這時候可以透過EditText的inputType來限定他只能輸入數字,
(基本上大部分的功能android都幫我們做好了,
我們只需要知道怎麼用就好了)
在這裡我們指定inputType為numberDecimal,
表示輸入的是十進位含小數的數字.

較常用的輸入類型列表如下:

參數 解釋 參數 解釋
none 文字不可編輯 textCapCharacters 大寫文字
text 一般文字 textCapWords 字頭大寫
textUri 網址文字 textCapSentences 首字字頭大小
number 數字格式 textAutoCorrect 自動校正錯字
numberSigned 有號數字格式 textAutoComplete 輸入自動完成
numberDecimal 十進位數字格式 textMultiLine 允許多行輸入
phone 輸入電話號碼 textNoSuggestions 不提示
datetime 輸入日期時間 textEmailAddress Email格式
date 輸入日期 textPersonName 人名格式
time 輸入時間 textPassword 密碼格式
textVisiblePassword 可見密碼格式

這時候我們看一下畫面
https://ithelp.ithome.com.tw/upload/images/20181013/20105694tRyHmpQlf1.png
可以看到螢幕小鍵盤直接跳數字鍵盤出來讓你輸入

完整的程式碼

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TextView
            android:id="@+id/TextView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:text="哈囉!BMI" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="身高 (cm)" />

        <EditText
            android:id="@+id/height"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="numberDecimal" />

        <TextView
            android:id="@+id/textView3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="體重 (kg)" />

        <EditText
            android:id="@+id/weight"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="numberDecimal" />

        <Button
            android:id="@+id/button"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="計算BMI值" />

        <TextView
            android:id="@+id/result"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textAppearance="?android:textAppearanceLarge"/>

        <TextView
            android:id="@+id/suggest"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textAppearance="?android:textAppearanceLarge"/>
    </LinearLayout>
</android.support.constraint.ConstraintLayout>

上一篇
[Day 06] 我的第一個Android程式 - BMI程式設計(一) 新增一個專案及基本畫面設計
下一篇
[Day 08] 我的第一個Android程式 - BMI程式設計(三) 將字串抽離Xml介面描述檔
系列文
[Andriod] Andriod Studio 從入門到進入狀況33
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言