Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android如何使用DOM來解析XML+如果做一個表情的彈出框

android如何使用DOM來解析XML+如果做一個表情的彈出框

編輯:關於Android編程

效果圖:

\

如何解析以下的xml:<喎?/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+PHByZSBjbGFzcz0="brush:java;"> (#大笑) (#微笑) (#親親) (#抱抱) (#色色) (#好失望喲)
這樣來解析:

public class MessageFaceModel {

	/** single instance of this class */
	private static MessageFaceModel instance = null;
	
	/** context */
	private boolean mInitialized = false;
	
	private HashMap mFaceMap = new HashMap();
	
	private ArrayList mFaceStrings = new ArrayList();
	
	private ArrayList mFaceIcons = new ArrayList();
	
	/**
	 * constructor
	 */
	private MessageFaceModel(){
		
	}
	
	/**
	 * Factory method
	 */
	public static synchronized MessageFaceModel getInstance(){
		if(instance == null){
			instance = new MessageFaceModel();
		}
		return instance;
	}
	
	/**
	 * initialize face data
	 */
	public void init(Context context){
		if(mInitialized){
			//initialize only once
			return;
		}
		
		mFaceMap.clear();
		mFaceStrings.clear();
		mFaceIcons.clear();
		
		AssetManager assetManager = context.getAssets();
		ArrayList faces = new ArrayList(); 
		DocumentBuilderFactory docBuilderFactory = null;
		DocumentBuilder docBuilder = null;
		Document doc = null;
		try {
			docBuilderFactory = DocumentBuilderFactory.newInstance();
			docBuilder = docBuilderFactory.newDocumentBuilder();
			doc = docBuilder.parse(assetManager.open("MessageFace.xml"));
			Element root = doc.getDocumentElement();
			NodeList nodeList = root.getElementsByTagName("string");
			for(int i =0;i< nodeList.getLength();i++)
			{
				Node node = nodeList.item(i);
				String s = "";
				NodeList list = node.getChildNodes();
				if(list != null){
					for(int j = 0; j < list.getLength(); j++){
						s += list.item(j).getNodeValue();
					}
				}
				faces.add(s);
			}
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally{
			doc = null;
			docBuilder = null;
			docBuilderFactory = null;

		}
		
		int i;
		for(i = 0; i < faces.size(); ++i){
			int index = i + 1;
			int id = context.getResources().getIdentifier(   
                    "msgface_" + index,    
                    "drawable", "com.example.tianqitongtest");
			try {
				Bitmap bm =  BitmapFactory.decodeResource(context.getResources(),id); 
				mFaceMap.put(faces.get(i), bm);
				mFaceStrings.add(faces.get(i));
				mFaceIcons.add(bm);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		mInitialized = true;
		
	}
	
	public ArrayList getFaceIcons(){
		return mFaceIcons;
	}
	
	public ArrayList getFaceStrings(){
		return mFaceStrings;
	}
	
	public Bitmap getFaceIcon(String face){
		if(mFaceMap != null){
			return mFaceMap.get(face);
		}else{
			return null;
		}
	}
	
	public void clear() {
		mInitialized = false;
		mFaceMap.clear();
		mFaceStrings.clear();
		mFaceIcons.clear();
	}
}

然後寫這個Dialog樣式的activity:

  
   		 

Layout是:



  
  
  
  
	  
  

public class InputFaceActivity extends Activity{

    private MessageFaceModel mMessageFaceModel = MessageFaceModel.getInstance();
	public static final int SELECT_STATE_FACE_ICON = 209;
	public static final int SELECT_MESSAGE_FACE_ICON = 109;
	private int mWidth = 0;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		mWidth = this.getResources().getDimensionPixelSize(R.dimen.image_width);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM,
							 WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
		setContentView(R.layout.input_face_activity); 
		GridView gridView = (GridView) findViewById(R.id.input_face_gridview);   
		gridView.setAdapter(new FaceListAdapter()); 
		gridView.setOnItemClickListener(new FaceListOnItemClickListener());
		
		Button cancelButton = (Button)findViewById(R.id.input_face_cancel_button); 
		cancelButton.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View arg0) {
				finish();
			}
			
		});

	}
	
	private class FaceListAdapter extends BaseAdapter {
		
		public int getCount() {
			if(mMessageFaceModel.getFaceIcons() != null){
				return mMessageFaceModel.getFaceIcons().size();
			}else{
			return 0;
			}
		}

		public Object getItem(int arg0) {
			return arg0;
		}

		public long getItemId(int arg0) {
			return arg0;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			
			ImageView view = new ImageView(InputFaceActivity.this);
			view.setImageBitmap(mMessageFaceModel.getFaceIcons().get(position));
			
			view.setLayoutParams(new GridView.LayoutParams(mWidth, mWidth));
			view.setScaleType(ScaleType.CENTER);
			return view;
		}
		
	}
	
}


  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved