編輯:關於Android編程
請尊重他人的勞動成果,轉載請注明出處:Android開發之解析XML並實現三級聯動效果
本實例主要應用XmlPullParser解析XML文檔中的省市區,然後將數據綁定到Spinner上實現三級聯動的效果。關於XmlPullParser的詳解大家可以參考《Android開發之使用PULL解析和生成XML》一文。
運行效果圖:
程序代碼:
核心代碼:
package com.jph.sevice; import java.io.InputStream; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.xmlpull.v1.XmlPullParser; import android.os.Handler; import android.os.Message; import android.util.Xml; /** * 解析省、市、區xml * @author jph * Date:2014.09.25 */ public class PullProvince { public static final int PARSESUCCWSS=0x2001; private Handler handler; public PullProvince(Handler handler) { // TODO Auto-generated constructor stub this.handler=handler; } /** * 獲取所有省份城市以及區 * @author jph * Date:2014.09.25 */ public void getProvinces(final InputStream inStream) { new Thread(new Runnable() { @Override public void run() { // TODO Auto-generated method stub try { XmlPullParser pullParser = Xml.newPullParser(); pullParser.setInput(inStream, "UTF-8"); int event; event = pullParser.getEventType(); Map>> provinces = new HashMap >>();// 省份 Map > cities=null;// 城市 ArrayList areaAll = null; String pName = "";// 省份名稱 String cName = "";// 城市名稱 String aName = "";// 地區名 String targetName = "";// 當前節點的名稱 while (event != XmlPullParser.END_DOCUMENT) { targetName = pullParser.getName(); switch (event) { case XmlPullParser.START_TAG: if ("province".equals(targetName)) {// 處理省份節點 pName = pullParser.getAttributeValue(0);// 當前省份名 cities=new HashMap >(); } else if ("city".equals(targetName)) {// 處理城市節點 cName = pullParser.getAttributeValue(0); areaAll = new ArrayList ();// 地區 } else if ("area".equals(targetName)) {// 處理地區節點 aName = pullParser.getAttributeValue(0); } break; case XmlPullParser.END_TAG: if (targetName.equals("area")) { areaAll.add(aName); } else if (targetName.equals("city")) { cities.put(cName, areaAll); } else if (targetName.equals("province")) { provinces.put(pName, cities); } break; } event = pullParser.next(); } Message message=new Message(); message.obj=provinces;//將解析出的數據放到message中傳給主線程 message.what=PARSESUCCWSS; handler.sendMessage(message);//通知主線程數據解析完畢 } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }).start(); } }
Activity:package com.jph.px; import java.io.InputStream; import java.util.List; import java.util.Map; import com.jph.sevice.PullProvince; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.app.Activity; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemSelectedListener; import android.widget.ArrayAdapter; import android.widget.Spinner; import android.widget.Toast; /** * 省,市,區三級聯動 * @author jph * Date:2014.09.25 */ public class MainActivity extends Activity { private Spinner province,city,area; private Map>> data=null; /**當前的選擇的省份**/ private String currentProvince; /**當前的選擇的城市**/ private String currentCity; private PullProvince pullProvince; // /**當前的選擇的區**/ // private String currentArea; private Handler mHandler=new Handler(){ @SuppressWarnings("unchecked") @Override public void handleMessage(Message msg) { // TODO Auto-generated method stub switch (msg.what) { case PullProvince.PARSESUCCWSS://數據解析完畢,加載數據 data=(Map >>) msg.obj; initData(); break; default: break; } super.handleMessage(msg); } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); province=(Spinner)findViewById(R.id.province); city=(Spinner)findViewById(R.id.city); area=(Spinner)findViewById(R.id.area); pullProvince=new PullProvince(mHandler); InputStream inStream = this.getClass().getClassLoader(). getResourceAsStream("province_city.xml"); pullProvince.getProvinces(inStream); } /** * 初始化數據 */ private void initData(){ if (data!=null) { String[]arrStrings= data.keySet().toArray(new String[0]); System.out.println(arrStrings); //將省份信息填充到 province Spinner province.setAdapter(new ArrayAdapter (this, android.R.layout.simple_list_item_multiple_choice,arrStrings)); currentProvince=getCurrentProvince(); bindCityAdapter(currentProvince); currentCity=getCurrentCity(); bindAreaAdapter(currentCity); setOnItemSelectedListener(); } } private void bindAreaAdapter(String currentCity) { // TODO Auto-generated method stub //根據當前顯示的城市將對應的區填充到area Spinner area.setAdapter(new ArrayAdapter (this, android.R.layout.simple_list_item_multiple_choice, data.get(currentProvince).get(currentCity))); } private void bindCityAdapter(String currentProvince) { // TODO Auto-generated method stub //根據當前顯示的省份將對應的城市填充到city Spinner city.setAdapter(new ArrayAdapter (this, android.R.layout.simple_list_item_multiple_choice,data. get(currentProvince).keySet().toArray(new String[0]))); } /** * 為Spinner設置監聽器 */ private void setOnItemSelectedListener() { // TODO Auto-generated method stub province.setOnItemSelectedListener(new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView> arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub currentProvince=getCurrentProvince(); bindCityAdapter(currentProvince); } @Override public void onNothingSelected(AdapterView> arg0) { // TODO Auto-generated method stub } }); city.setOnItemSelectedListener(new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView> arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub currentCity=getCurrentCity(); bindAreaAdapter(currentCity); } @Override public void onNothingSelected(AdapterView> arg0) { // TODO Auto-generated method stub } }); area.setOnItemSelectedListener(new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView> arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub Toast.makeText(MainActivity.this,area.getSelectedItem().toString(), Toast.LENGTH_SHORT).show(); } @Override public void onNothingSelected(AdapterView> arg0) { // TODO Auto-generated method stub } }); } /**獲取當前選擇的省份 * @return String:當前選擇的省份 */ private String getCurrentProvince() { // TODO Auto-generated method stub return province.getSelectedItem().toString(); } /**獲取當前選擇的城市 * @return String:當前選擇的城市 */ private String getCurrentCity() { // TODO Auto-generated method stub return city.getSelectedItem().toString(); } }
布局文件:
首先我們先來看一下效果分析我們來看這個進度條應該分為3個小部分1.中間的圓2.外邊的圓環3.中間的文字分開畫這3部分就是需要我們自己畫出來的,因此我們需要3根畫筆//設置
百度專車是百度地圖最新推出的一項提供高品質用車的服務。百度憑借百度地圖龐大的用戶群體,開展這項專車服務,提供從經濟型到奢華型共5大類20余種車型供用戶選擇,
在項目中,有時候在一些控件(如Button、TextView)中要添加一些符號,如下圖所示: 這個時候可以使用圖片的方式來顯
Android中的動畫主要分為三類1.Drawable Animation2.View Animation3.Property Animation這裡介紹其中的兩類,Dr