Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 新手入門 2016(12)——基於 Layout 檔的 AlertDialog

編輯:關於 Android 開發

Android 新手入門 2016(12)——基於 Layout 檔的 AlertDialog


上一章學習了 AlertDialog,後來發現還有「基於 Layout 檔」的 AlertDialog,可以自己排好元件位置,相對複雜一些。

先來看效果:

圖中的對話框已依佈局檔排好元件位置。

新增了一個 Layout 檔:dialog_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <EditText
        android:id="@+id/edtInput"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_marginLeft="20dip"
        android:layout_marginRight="20dip"
        android:textAppearance="?android:attr/textAppearanceMedium">
    </EditText>
</LinearLayout>

這個佈局檔只加入了一個輸入框。接著看程式碼(直接在上一章基礎上修改,程式碼稍長):

package com.fable.helloworld;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.SimpleAdapter;
import java.util.*;

public class HelloWorldActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hello_world);
        GridView gridview = (GridView) findViewById(R.id.gridview);
        ArrayList<HashMap<String, Object>> images = new ArrayList<HashMap<String, Object>>();
        for (int i = 1; i < 10; i++) {
            String imageName = "";
            switch (i) {
                case 1: imageName = "AlertDialog"; break;  // 普通的 AlertDialog
                case 2: imageName = "AlertDialog2"; break; // 基於佈局的 AlertDialog
                default: imageName = "app" + String.valueOf(i);
            }
            HashMap<String, Object> map = new HashMap<String, Object>();
            map.put("ItemImage", R.drawable.ic_launcher);
            map.put("ItemText", imageName);
            images.add(map);
        }
        SimpleAdapter simpleAdapter = new SimpleAdapter(
                this, images, R.layout.my_list_item,
                new String[] {"ItemImage","ItemText"},
                new int[] {R.id.ItemImage, R.id.ItemText});
        gridview.setAdapter(simpleAdapter);
        gridview.setOnItemClickListener(new ItemClickListener());
    }

    class ItemClickListener implements OnItemClickListener {
        public void onItemClick(AdapterView arg0, View arg1, int arg2, long arg3) {
            HashMap<String, Object> item = (HashMap<String, Object>) arg0.getItemAtPosition(arg2);
            String itemStr = (String) item.get("ItemText");
            if (itemStr.equals("AlertDialog")) {
                showDialog(HelloWorldActivity.this, itemStr);
            } else if (itemStr.equals("AlertDialog2")) {
                showDialogLayout(HelloWorldActivity.this);
            }
        }

        // ===================== AlertDialog =====================
        private void showDialog(Context context, String itemStr) {
            AlertDialog.Builder builder = new AlertDialog.Builder(context);
            builder.setIcon(R.drawable.ic_launcher);
            builder.setTitle("我是標題");
            builder.setMessage("這裡是內容啊啊啊啊!!!");
            builder.setPositiveButton("Button1", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    setTitle("點擊了對話框上的 Button1");
                }
            });
            builder.setNeutralButton("Button2", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    setTitle("點擊了對話框上的 Button2");
                }
            });
            builder.setNegativeButton("Button3", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    setTitle("點擊了對話框上的 Button3");
                }
            });
            builder.show();
        }

        // ============= 基於 Layout 的 AlertDialog =============
        private void showDialogLayout(Context context) {
            // LayoutInflater 用於動態載入 Layout 檔
            LayoutInflater inflater = LayoutInflater.from(context);
            final View textEntryView = inflater.inflate(R.layout.dialog_layout, null);
            final EditText edtInput = (EditText) textEntryView.findViewById(R.id.edtInput);
            final AlertDialog.Builder builder = new AlertDialog.Builder(context);
            builder.setCancelable(false);
            builder.setIcon(R.drawable.ic_launcher);
            builder.setTitle("Title");
            builder.setView(textEntryView);
            builder.setPositiveButton("確認", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    setTitle(edtInput.getText());
                }
            });
            builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    setTitle("");
                }
            });
            builder.show();
        }
    }
}

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved