1. 程式人生 > >Android點選事件的新增、輸入框引入外形資源調節樣式

Android點選事件的新增、輸入框引入外形資源調節樣式

案例一

  1. 事件監聽(三種寫法)
    1.1 標籤上直接繫結監聽方法
    public void xxx(View view)

1.2 建立監聽器物件,元件再繫結監聽器物件
1.2.1 匿名內部類

1.2.2 使用匿名內部類並定義成全域性的屬性
  1. 文字框(TextView)
    2.1 簡介
    TextView直接繼承View,作用就是在介面上顯示文字(類似於Swing中的JLabel),同時它還是EditText、Button兩個UI元件類的父類。
    另外Android關閉了它的文字編輯功能,如果想編輯內容,則可以使用EditText。
    2.2 TextView常用屬性
    2.2.1 text
    2.2.2 autoLink
    2.2.3 singleLine(已過時,現在應為:android:lines)
    2.2.4 lines
    2.2.5 minLines
    2.2.6 textColor
    2.2.7 textSize
    2.2.8 textStyle
    字型風格粗體、斜體
    2.2.9 backgroud
    設定背景,可以是顏色或圖片
    2.2.10 drawableXX
    設定文字的icon
    2.2.11 ellipsize 當文字長度超過textview寬度時的顯示方式
    android:ellipsize=“start” 省略號顯示在開頭 “…pedia”
    android:ellipsize=“end” 省略號顯示在結尾 “encyc…”
    android:ellipsize=“middle” 省略號顯示在中間 “en…dia”
    android:ellipsize=“marquee” 以橫向滾動方式顯示(另外還需要設定其它三個屬性,否則不滾動,即必須讓該控制元件獲得焦點)

案例二 常用編輯框
3. 編輯框(EditText)
3.1 簡介
EditText和TextView非常相似,它與TextView共用了絕大總分XML屬性和文法,
二者最大區別在於:EditText可以接受使用者輸入。
3.2 常用屬性
3.2.1 inputType
它是EditText元件最重要的屬性,它相當於HTML中標籤的type屬性,用於EditText指定輸入元件的型別。
常用取值有:number|numberPassword|date|phone
3.2.2 hint:提示字元資訊

案例三:橢圓形、背景漸變的文字框
4. Android資源
4.1 什麼是資源
指在res資料夾下的xml檔案,每一種型別的xml檔案都對應了一種資源。
例如:外形資源(shape)、尺寸資源(dimension)、顏色資源(color)等
4.2 尺寸資源和顏色資源放res/values目錄下
4.3 *.xml(文字框的外形資源)可放到drawable目錄下
4.4 設定漸變色與背景色只能取其一,後面會覆蓋前面的

案例四:根據獲得/失去焦點邊框變色編輯框
5. 選擇器
5.1 作用:根據控制元件狀態顯示不同樣式
5.2 Item: 指定不同狀態下控制元件顯示哪個樣式

核心點:
1、點選事件的新增
2、輸入框引入外形資源調節樣式

<?xml version="1.0" encoding="utf-8"?>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />


<EditText
    android:layout_width="match_parent"
    android:hint="請輸入姓名"
    android:id="@+id/et_main_uname"
    android:layout_height="wrap_content" />

<EditText
    android:layout_width="match_parent"
    android:hint="請輸入密碼"
    android:id="@+id/et_main_pwd"
    android:layout_height="wrap_content" />

<Button
    android:layout_width="match_parent"
    android:id="@+id/btn_main_1"
    android:onClick="login"
    android:layout_height="wrap_content"/>


<Button
    android:layout_width="match_parent"
    android:id="@+id/btn_main_2"
    android:layout_height="wrap_content"/>