Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux 程式設計 >> Linux 程式設計

Android 第一個 UI 練習:從版面配置到登入對話框

一開始在 login_dlg.xml 裡拖了兩個文字標籤、兩個輸入框與兩個按鈕。過程中摸出一個規律:版面屬性中凡是名字開頭是 layout_ 的,描述的都是元件與父容器之間的關係;沒有這個前綴的,則是元件自身的樣式。後來才發現,按鈕其實可以直接用對話框自帶的,不必自己排。

怎麼知道對話框該怎麼寫

說實話,我沒寫過 Java,連對話框怎麼建立都不知道。但也不是完全沒頭緒——Android SDK 自帶大量範例原始碼,裡面的 ApiDemos 工程把各種控件的效果與實作都寫齊了,我的登入對話框程式碼就是從那兒抄回來的。遇到不熟的 API,翻 SDK 範例往往比搜尋引擎更快。

改寫 onCreateDialog

核心是把 onCreateDialog 覆寫掉,用 LayoutInflater 把自訂版面撐成 View,再交給 AlertDialog.Builder 組裝:

public class HelloWorld extends Activity {

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        showDialog(1);
    }

    @Override
    protected Dialog onCreateDialog(int id) {
        LayoutInflater factory = LayoutInflater.from(this);
        final View textEntryView = factory.inflate(R.layout.login_dlg, null);

        return new AlertDialog.Builder(HelloWorld.this)
                .setIcon(R.drawable.icon)
                .setTitle(R.string.land_dialog_title)
                .setView(textEntryView)
                .setPositiveButton(R.string.land_dialog_ok,
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                            /* 處理登入 */
                            Toast.makeText(HelloWorld.this,
                                getText(R.string.land_select_ok),
                                Toast.LENGTH_SHORT).show();
                        }
                    })
                .setNegativeButton(R.string.land_dialog_cancel,
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                            /* 處理取消登入 */
                            Toast.makeText(HelloWorld.this,
                                getText(R.string.land_select_cancel),
                                Toast.LENGTH_SHORT).show();
                        }
                    })
                .create();
    }
}

登入版面 login_dlg.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <TextView
        android:id="@+id/username"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dip"
        android:layout_marginRight="20dip"
        android:textColor="#ff0000"
        android:gravity="left"
        android:text="用戶名:" />

    <EditText
        android:id="@+id/txt_username"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dip"
        android:layout_marginRight="20dip"
        android:gravity="fill_horizontal"
        android:autoText="false"
        android:capitalize="none"
        android:text="請輸入用戶名" />

    <TextView
        android:id="@+id/password"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dip"
        android:layout_marginRight="20dip"
        android:textColor="#ff0000"
        android:gravity="left"
        android:text="密碼:" />

    <EditText
        android:id="@+id/txt_password"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dip"
        android:layout_marginRight="20dip"
        android:gravity="fill_horizontal"
        android:autoText="false"
        android:password="true"
        android:capitalize="none"
        android:text="請輸入密碼" />
</LinearLayout>

幾個實作細節

  • showDialog(1) 只是宣告「有個 id 為 1 的對話框」,真正的建立工作在 onCreateDialog 中完成,且只會在第一次顯示時被呼叫,之後會被 Activity 快取。
  • setView() 塞入自訂版面,因此標題、圖示與按鈕都由 Builder 負責,版面裡就不必再放按鈕。
  • 密碼框靠 android:password="true" 遮蔽輸入;輸入框的 android:text 寫的是提示文字,實務上改用 android:hint 會更合適,因為 hint 會在輸入後自動消失。
  • 要在按鈕回呼中讀取輸入內容,可用 findViewById(R.id.txt_username) 取得 EditText 再呼叫 getText().toString()。此處的 findViewById 需以對話框的 View 為根節點呼叫,而非 Activity。
Copyright © Linux教程網 All Rights Reserved