編輯:關於 Android 開發
上一章學習了 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();
}
}
}
Java 正則表達式入門與 Jakarta-ORO 應用整理
正則表達式基礎 正則表達式用於描述字串的搜尋規則。最簡單的例子是直接比對 cat;若不分大小寫,也可匹配 Catherine 等包含相同字元的字串。 常用符號 句點 .
釋放 Android 的函數式能量(一):Kotlin Lambda 表達式入門
背景:為何要在 Android 上談函數式 Java 8 已內建若干函數式工具,但 Android 開發者過去很長一段時間無法立即使用。既然官方語言支援落後,社群便轉而尋
PNG 圖片尺寸快速轉換:Android 多密度資源的實用小工具
PNG 圖片尺寸快速轉換:Android 多密度資源的實用小工具 做 Android 開發時,最常被同一張圖折騰的就是密度適配。系統會依裝置的 dpi 去對應 drawa
Android 開發學習:SQLite 數據庫操作與單元測試入門
Android 開發學習:SQLite 數據庫操作與單元測試入門 SQLite 是 Android 自帶的輕量級關系型數據庫,幾乎所有需要本地持久化的應用都會用到它。本文