- Android 基礎
- Android - 首頁
- Android - 概述
- Android - 環境搭建
- Android - 架構
- Android - 應用元件
- Android - Hello World 例子
- Android - 資源
- Android - 活動 (Activities)
- Android - 服務 (Services)
- Android - 廣播接收器 (Broadcast Receivers)
- Android - 內容提供者 (Content Providers)
- Android - 碎片 (Fragments)
- Android - 意圖/過濾器 (Intents/Filters)
- Android - 使用者介面
- Android - UI 佈局
- Android - UI 控制元件
- Android - 事件處理
- Android - 風格和主題
- Android - 自定義元件
- Android 高階概念
- Android - 拖放
- Android - 通知
- 基於位置的服務
- Android - 傳送郵件
- Android - 傳送簡訊
- Android - 打電話
- 釋出 Android 應用
- Android 實用例子
- Android - 警報對話方塊
- Android - 動畫
- Android - 音訊錄製
- Android - 音訊管理器 (AudioManager)
- Android - 自動完成
- Android - 最佳實踐
- Android - 藍牙
- Android - 相機
- Android - 剪貼簿
- Android - 自定義字型
- Android - 資料備份
- Android - 開發者工具
- Android - 模擬器
- Android - Facebook 整合
- Android - 手勢
- Android - 谷歌地圖
- Android - 圖片特效
- Android - ImageSwitcher
- Android - 內部儲存
- Android - JetPlayer
- Android - JSON 解析器
- Android - Linkedin 整合
- Android - 載入旋轉器
- Android - 本地化
- Android - 登入介面
- Android - MediaPlayer
- Android - 多點觸控
- Android - 導航
- Android - 網路連線
- Android - NFC 指南
- Android - PHP/MySQL
- Android - 進度圓圈
- Android - 進度條
- Android - 推送通知
- Android - RenderScript
- Android - RSS 閱讀器
- Android - 螢幕錄製
- Android - SDK 管理器
- Android - 感測器
- Android - 會話管理
- Android - 共享首選項
- Android - SIP 協議
- Android - 拼寫檢查器
- Android - SQLite 資料庫
- Android - 支援庫
- Android - 測試
- Android - 文字轉語音
- Android - TextureView
- Android - Twitter 整合
- Android - UI 設計
- Android - UI 模式
- Android - UI 測試
- Android - WebView 佈局
- Android - Wi-Fi
- Android - 小部件
- Android - XML 解析器
- Android 實用資源
- Android - 問答
- Android - 實用資源
- Android - 討論
Android - EditText 控制元件
EditText 是一個覆蓋在 TextView 上的控制元件,它本身配置為可編輯的。它是 TextView 的預定義子類,包含豐富的編輯功能。
EditText 的樣式
EditText 屬性
以下是與 EditText 控制元件相關的重要的屬性。您可以檢視 Android 官方文件以獲取屬性的完整列表,以及可以在執行時更改這些屬性的相關方法。
繼承自 android.widget.TextView 類:
| 序號 | 屬性 & 描述 |
|---|---|
| 1 | android:autoText 如果設定,則指定此 TextView 具有文字輸入法,並自動更正一些常見的拼寫錯誤。 |
| 2 | android:drawableBottom 這是要在文字下方繪製的可繪製物件。 |
| 3 | android:drawableRight 這是要在文字右側繪製的可繪製物件。 |
| 4 | android:editable 如果設定,則指定此 TextView 具有輸入法。 |
| 5 | android:text 這是要顯示的文字。 |
繼承自 android.view.View 類:
| 序號 | 屬性 & 描述 |
|---|---|
| 1 | android:background 這是用作背景的可繪製物件。 |
| 2 | android:contentDescription 這定義了簡要描述檢視內容的文字。 |
| 3 | android:id 這為該檢視提供了一個識別符號名稱。 |
| 4 | android:onClick 這是此檢視上下文中呼叫的方法的名稱,當單擊該檢視時呼叫。 |
| 5 | android:visibility
這控制檢視的初始可見性。 |
示例
此示例將引導您完成簡單的步驟,演示如何使用線性佈局和 EditText 建立您自己的 Android 應用程式。
| 步驟 | 描述 |
|---|---|
| 1 | 您將使用 Android Studio IDE 建立一個 Android 應用程式,並在 com.example.demo 包下將其命名為 demo,如“Hello World 例子”章節中所述。 |
| 2 | 修改 src/MainActivity.java 檔案以新增單擊事件。 |
| 3 | 修改 res/layout/activity_main.xml 檔案的預設內容,以包含 Android UI 控制元件。 |
| 4 | 在 res/values/strings.xml 檔案中定義所需的字串常量 |
| 5 | 執行應用程式以啟動 Android 模擬器並驗證對應用程式所做的更改的結果。 |
以下是修改後的主活動檔案 src/com.example.demo/MainActivity.java 的內容。此檔案可以包含每個基本生命週期方法。
package com.example.demo;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
EditText eText;
Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
eText = (EditText) findViewById(R.id.edittext);
btn = (Button) findViewById(R.id.button);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
String str = eText.getText().toString();
Toast msg = Toast.makeText(getBaseContext(),str,Toast.LENGTH_LONG);
msg.show();
}
});
}
}
res/layout/activity_main.xml 檔案的內容如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="14dp"
android:layout_marginTop="18dp"
android:text="@string/example_edittext" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="130dp"
android:text="@string/show_the_text" />
<EditText
android:id="@+id/edittext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button"
android:layout_below="@+id/textView1"
android:layout_marginTop="61dp"
android:ems="10"
android:text="@string/enter_text" android:inputType="text" />
</RelativeLayout>
res/values/strings.xml 檔案的內容如下,用於定義這些新的常量:
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">demo</string> <string name="example_edittext">Example showing EditText</string> <string name="show_the_text">Show the Text</string> <string name="enter_text">text changes</string> </resources>
AndroidManifest.xml 的預設內容如下:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.demo" >
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.demo.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
讓我們嘗試執行您的 demo 應用程式。我假設您在進行環境設定時建立了您的 AVD。要從 Android Studio 執行應用程式,請開啟專案的其中一個活動檔案,然後單擊工具欄中的執行
圖示。Android Studio 將應用程式安裝到您的 AVD 並啟動它,如果您的設定和應用程式一切正常,它將顯示以下模擬器視窗:
練習
我建議您在佈局 XML 檔案中以及程式設計時嘗試使用 EditText 的不同屬性,以獲得 EditText 不同的外觀和感覺。嘗試使其可編輯,更改字型顏色、字體系列、寬度、textSize 等,然後檢視結果。您還可以嘗試在一個活動中使用多個 EditText 控制元件。
