編輯:Android 開發教學
幾乎每個專案都需要回饋建議的功能,實作方式很多,以下是其中一種,供大家交流。
先看介面,共三個欄位:名字與信箱為選填,可留空;建議內容為必填。
這裡用到 <include layout="@layout/uphead" /> 把另一個版面檔合併進來,讓多頁共用同一個標題列。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/bg_gray" >
<include layout="@layout/uphead"/>
<!-- Name Label -->
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="名字(選填)"
android:paddingLeft="10dip"
android:paddingRight="10dip"
android:paddingTop="10dip"
android:textColor="@color/coffee"
android:textSize="12sp"/>
<!-- Input Name -->
<EditText
android:id="@+id/inputName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="5dip"
android:layout_marginBottom="15dip"
android:singleLine="true"/>
<!-- Email Label -->
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="郵箱(選填)"
android:paddingLeft="10dip"
android:paddingRight="10dip"
android:paddingTop="10dip"
android:textColor="@color/coffee"
android:textSize="12sp"/>
<!-- Input Email -->
<EditText
android:id="@+id/inputEmail"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="5dip"
android:layout_marginBottom="15dip"
android:singleLine="true"/>
<!-- Description Label -->
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="建議(必填)"
android:paddingLeft="10dip"
android:paddingRight="10dip"
android:paddingTop="10dip"
android:textColor="@color/coffee"
android:textSize="12sp"/>
<!-- Input description -->
<EditText
android:id="@+id/inputDesc"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="5dip"
android:layout_marginBottom="15dip"
android:lines="4"
android:gravity="top"/>
<!-- Button Submit -->
<Button
android:id="@+id/btnCreateProduct"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="提交"
android:textSize="20sp"
android:textColor="@color/coffee" />
</LinearLayout>
按下提交後,把三個欄位的值組成 JSON 字串 POST 到 PHP 介面,服務端解析後寫入 MySQL,最後回傳一個代表成功與否的 JSON。實作上有三點值得注意:
1. 建議欄位要做非空檢查,順便 trim 掉空白,避免使用者送出只有空格的內容。
2. 信箱欄位用 Patterns.EMAIL_ADDRESS 做格式驗證,比在服務端才發現錯誤友善得多。
3. 網路請求必須放在非主執行緒,並在 UI 執行緒上根據結果提示使用者;同時務必對伺服器端做好參數驗證,用戶端檢查只是為了體驗,不能當成安全措施。
Android Studio 中如何修改編輯器字體大小
Android Studio 默認的編輯器方案(Scheme)不允許直接修改字體大小,需要先另存為一個新方案,然後在新方案上調整。 操作步驟 第一步:打開設置界面,路徑
Android FragmentTabHost 實現選項卡:思路與關鍵配置
為什麼使用 FragmentTabHost在 Android 3.0 之後,Fragment 成為常見的畫面組件,因此傳統 TabHost 已較少使用。FragmentT
Android 使用 HTML Parser 解析網頁內容
行動應用常需要呈現來自傳統網路的內容。原生應用可下載網頁後解析 HTML,再重新排版顯示,以获得更好的效能與使用者體驗。HTML Parser 的整合方式HTML Par
Android ApiDemos 解析(24):App → Launcher Shortcuts
在 Android 中,凡是 <intent-filter> 同時含有下列兩項的 Activity,就會被應用程式管理器(Launcher)列出來,通常這就是