編輯:關於 Android 程式設計
本文示範如何以 Jsoup 解析一段 HTML 表格字串,並把其中的 label、text 輸入框、button 等元素對應轉成 Android 的原生控制項,動態組出一個 TableLayout 表單。
import java.util.ArrayList; import java.util.List; import org.apache.http.NameValuePair; import org.apache.http.message.BasicNameValuePair; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; import android.app.Activity; import android.graphics.Color; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; import android.view.Window; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.TableLayout; import android.widget.TableRow; import android.widget.TextView; import android.widget.Toast; public class TableParseActivity extends Activity{ private Document doc; private String html = null; private TableLayout tableLayout; private final int WC = ViewGroup.LayoutParams.WRAP_CONTENT; private final int FP = ViewGroup.LayoutParams.FILL_PARENT; private final int WIDTH = 80; private String functionName,fields; private List<NameValuePair> params; private static String url; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.analyzing); html = "需要解析的HTML字符串"; tableParse(); } public void tableParse(){ doc = Jsoup.parse(html); Elements trs = doc.select("tr"); tableLayout = (TableLayout)findViewById(R.id.tableLayout1); TableLayout.LayoutParams p = new TableLayout.LayoutParams(FP, WC); this.setTitle(doc.title()); for (Element row : trs) {//循環表下的行 tr 對象 TableRow tableRow = new TableRow(this); Elements cols = row.children(); for (Element col : cols) {//循環行下的列 td 對象 Elements children = col.children(); for (Element child : children) { if(child.tagName().equals("label")){ TextView textView = new TextView(this); textView.setText(child.val()); textView.setTextColor(Color.BLACK); tableRow.addView(textView); }else if(child.tagName().equals("input")&&child.attributes().get("type").equals("text")){ EditText editText = new EditText(this); editText.setText(child.val()); editText.setWidth(WIDTH); tableRow.addView(editText); String id = child.attributes().get("id"); if(id.length() > 0){ editText.setId(Integer.parseInt(child.attributes().get("id"))); } }else if(child.tagName().equals("input")&&child.attributes().get("type").equals("button")){ Button button = new Button(this); button.setText(child.val()); tableRow.addView(button); fields = child.attributes().get("fields"); functionName = child.attributes().get("functionName"); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { //TODO onClick } }); }//end if(child.tagName().equals("input")&&child.attributes().get("type").equals("button")) }//end for (Element child : children) }//end for (Element col : cols) tableLayout.addView(tableRow,p); }//end for (Element row : rows) }//end tableParse() }
整個流程是三段巢狀迴圈:doc.select("tr") 取出所有表格列,接著用 row.children() 取得該列的儲存格,最後再逐一檢查儲存格內的子元素標籤名稱。
對應關係很單純:label 轉成 TextView、type="text" 的 input 轉成 EditText(並可透過 id 屬性設定控制項 ID)、type="button" 的 input 轉成 Button 並掛上點擊監聽。原本 HTML 上的自訂屬性(例如 fields、functionName)也能用 child.attributes().get(...) 讀出來當作後續邏輯的依據。
想再擴充 select、textarea 或 checkbox 這類元素,只要照同樣模式加分支即可。
希望本文所述對大家 Android 程式設計有所幫助。
字體管家怎麼用?教你美化手機字型
字體管家是一款用來更換手機字型的工具,歸在手機美化類別。它在線上字型的收錄算是相當齊全,雖然還稱不上百變,但分類足夠應付日常的美化需求。 剛接觸這類軟體時,多數人會有點不
Android NDK 實作:把彩色圖轉成灰階圖並比較效能
在 Android 上用 Java 把彩圖轉為灰階圖,做法與 J2ME 上類似,都是逐像素取 RGB 平均。但一旦遇到需要頻繁轉換、或是尺寸很大的圖片,Java 的效能就
Android 頁面間回傳結果:startActivityForResult 與 setResult 實作
頁面跳轉不只是「過去」,很多時候還需要「回來時帶答案」——例如從清單頁挑一個使用者,回到表單頁帶入。這就必須用到 startActivityForResult 與 set
Android 動態載入 APK 實作:Fragment 與 Activity 兩種做法
起因是專案膨脹到編譯不過——Android 對單一應用中的方法數量有上限(就是知名的 65536 或 DEX 64K 限制)。於是開始研究能否把某些模塊做成獨立 APK,