亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

android使用DOM來解析XML和做一個表情的彈出框的方法

發布時間:2020-08-21 11:23:38 來源:億速云 閱讀:205 作者:小新 欄目:編程語言

這篇文章主要介紹了android使用DOM來解析XML和做一個表情的彈出框的方法,具有一定借鑒價值,需要的朋友可以參考下。希望大家閱讀完這篇文章后大有收獲。下面讓小編帶著大家一起了解一下。

如何解析以下的xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<array>
	<string>(#大笑)</string>
	<string>(#微笑)</string>
	<string>(#親親)</string>
	<string>(#抱抱)</string>
	<string>(#色色)</string>
	<string>(#好失望喲)</string>
</array>


這樣來解析:

public class MessageFaceModel {

	/** single instance of this class */
	private static MessageFaceModel instance = null;
	
	/** context */
	private boolean mInitialized = false;
	
	private HashMap<String,Bitmap> mFaceMap = new HashMap<String,Bitmap>();
	
	private ArrayList<String> mFaceStrings = new ArrayList<String>();
	
	private ArrayList<Bitmap> mFaceIcons = new ArrayList<Bitmap>();
	
	/**
	 * 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<String> faces = new ArrayList<String>(); 
		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<Bitmap> getFaceIcons(){
		return mFaceIcons;
	}
	
	public ArrayList<String> 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:

  <activity android:name=".InputFaceActivity"
   		 	 android:theme="@android:style/Theme.Dialog"
   			 android:configChanges="keyboardHidden|orientation">
   		 </activity>


Layout是:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="300dp"
  android:minHeight="100dp"
  android:background="#EFEFEF">
  <GridView
	  xmlns:android="http://schemas.android.com/apk/res/android"
	  android:id="@+id/input_face_gridview" 
	  android:layout_width="fill_parent"
	  android:layout_height="wrap_content"
	  android:layout_marginLeft="18dp"
	  android:layout_marginRight="10dp" 
	  android:layout_marginTop = "18dp"
	  android:layout_marginBottom = "30dp"
	  android:numColumns="auto_fit" 
	  android:horizontalSpacing="10dp"
	  android:verticalSpacing="15dp"
	  android:columnWidth="50dp"
	  android:stretchMode="columnWidth"
	  android:gravity="center"
	  android:layout_weight="1.0">
  </GridView>
  
  <LinearLayout
	  xmlns:android="http://schemas.android.com/apk/res/android"
	  android:layout_width="fill_parent"
	  android:layout_height="wrap_content">
	  <Button
	  	  android:id="@+id/input_face_cancel_button" 
		  android:layout_width="wrap_content"
		  android:layout_height="wrap_content"
		  android:background="@drawable/cancel_button_style">
	  </Button>
  </LinearLayout>
</RelativeLayout>
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;
		}
		
	}
	
}

感謝你能夠認真閱讀完這篇文章,希望小編分享android使用DOM來解析XML和做一個表情的彈出框的方法內容對大家有幫助,同時也希望大家多多支持億速云,關注億速云行業資訊頻道,遇到問題就找億速云,詳細的解決方法等著你來學習!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

兴城市| 江永县| 临泉县| 赤城县| 丹江口市| 许昌县| 屏山县| 章丘市| 鲁山县| 依安县| 屯门区| 兴化市| 手机| 慈溪市| 东阿县| 高碑店市| 遂宁市| 佛坪县| 温州市| 内乡县| 滨州市| 乐平市| 界首市| 通州区| 常熟市| 密山市| 兴隆县| 正宁县| 永福县| 日照市| 东丰县| 曲靖市| 泰安市| 磴口县| 阳西县| 乐平市| 万州区| 阿巴嘎旗| 平陆县| 兰考县| 平凉市|