您的位置:首页 > 技术中心 > 其他 >

android如何使用DOM来解析XML+如果做一个表情的弹出框

时间:2022-02-24 10:28

效果图:


如何解析以下的xml:


  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
  3. <array>
  4. <string>(#大笑)</string>
  5. <string>(#微笑)</string>
  6. <string>(#亲亲)</string>
  7. <string>(#抱抱)</string>
  8. <string>(#色色)</string>
  9. <string>(#好失望哟)</string>
  10. </array>


这样来解析:



  1. public class MessageFaceModel {
  2. /** single instance of this class */
  3. private static MessageFaceModel instance = null;
  4. /** context */
  5. private boolean mInitialized = false;
  6. private HashMap<String,Bitmap> mFaceMap = new HashMap<String,Bitmap>();
  7. private ArrayList<String> mFaceStrings = new ArrayList<String>();
  8. private ArrayList<Bitmap> mFaceIcons = new ArrayList<Bitmap>();
  9. /**
  10. * constructor
  11. */
  12. private MessageFaceModel(){
  13. }
  14. /**
  15. * Factory method
  16. */
  17. public static synchronized MessageFaceModel getInstance(){
  18. if(instance == null){
  19. instance = new MessageFaceModel();
  20. }
  21. return instance;
  22. }
  23. /**
  24. * initialize face data
  25. */
  26. public void init(Context context){
  27. if(mInitialized){
  28. //initialize only once
  29. return;
  30. }
  31. mFaceMap.clear();
  32. mFaceStrings.clear();
  33. mFaceIcons.clear();
  34. AssetManager assetManager = context.getAssets();
  35. ArrayList<String> faces = new ArrayList<String>();
  36. DocumentBuilderFactory docBuilderFactory = null;
  37. DocumentBuilder docBuilder = null;
  38. Document doc = null;
  39. try {
  40. docBuilderFactory = DocumentBuilderFactory.newInstance();
  41. docBuilder = docBuilderFactory.newDocumentBuilder();
  42. doc = docBuilder.parse(assetManager.open("MessageFace.xml"));
  43. Element root = doc.getDocumentElement();
  44. NodeList nodeList = root.getElementsByTagName("string");
  45. for(int i =0;i< nodeList.getLength();i++)
  46. {
  47. Node node = nodeList.item(i);
  48. String s = "";
  49. NodeList list = node.getChildNodes();
  50. if(list != null){
  51. for(int j = 0; j < list.getLength(); j++){
  52. s += list.item(j).getNodeValue();
  53. }
  54. }
  55. faces.add(s);
  56. }
  57. } catch (Exception e) {
  58. // TODO Auto-generated catch block
  59. e.printStackTrace();
  60. } finally{
  61. doc = null;
  62. docBuilder = null;
  63. docBuilderFactory = null;
  64. }
  65. int i;
  66. for(i = 0; i < faces.size(); ++i){
  67. int index = i + 1;
  68. int id = context.getResources().getIdentifier(
  69. "msgface_" + index,
  70. "drawable", "com.example.tianqitongtest");
  71. try {
  72. Bitmap bm = BitmapFactory.decodeResource(context.getResources(),id);
  73. mFaceMap.put(faces.get(i), bm);
  74. mFaceStrings.add(faces.get(i));
  75. mFaceIcons.add(bm);
  76. } catch (Exception e) {
  77. // TODO Auto-generated catch block
  78. e.printStackTrace();
  79. }
  80. }
  81. mInitialized = true;
  82. }
  83. public ArrayList<Bitmap> getFaceIcons(){
  84. return mFaceIcons;
  85. }
  86. public ArrayList<String> getFaceStrings(){
  87. return mFaceStrings;
  88. }
  89. public Bitmap getFaceIcon(String face){
  90. if(mFaceMap != null){
  91. return mFaceMap.get(face);
  92. }else{
  93. return null;
  94. }
  95. }
  96. public void clear() {
  97. mInitialized = false;
  98. mFaceMap.clear();
  99. mFaceStrings.clear();
  100. mFaceIcons.clear();
  101. }
  102. }


然后写这个Dialog样式的activity:



  1. <activity android:name=".InputFaceActivity"
  2. android:theme="@android:style/Theme.Dialog"
  3. android:configChanges="keyboardHidden"orientation">
  4. </activity>


Layout是:



  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:layout_width="fill_parent"
  5. android:layout_height="300dp"
  6. android:minHeight="100dp"
  7. android:background="#EFEFEF">
  8. <GridView
  9. xmlns:android="http://schemas.android.com/apk/res/android"
  10. android:id="@+id/input_face_gridview"
  11. android:layout_width="fill_parent"
  12. android:layout_height="wrap_content"
  13. android:layout_marginLeft="18dp"
  14. android:layout_marginRight="10dp"
  15. android:layout_marginTop = "18dp"
  16. android:layout_marginBottom = "30dp"
  17. android:numColumns="auto_fit"
  18. android:horizontalSpacing="10dp"
  19. android:verticalSpacing="15dp"
  20. android:columnWidth="50dp"
  21. android:stretchMode="columnWidth"
  22. android:gravity="center"
  23. android:layout_weight="1.0">
  24. </GridView>
  25. <LinearLayout
  26. xmlns:android="http://schemas.android.com/apk/res/android"
  27. android:layout_width="fill_parent"
  28. android:layout_height="wrap_content">
  29. <Button
  30. android:id="@+id/input_face_cancel_button"
  31. android:layout_width="wrap_content"
  32. android:layout_height="wrap_content"
  33. android:background="@drawable/cancel_button_style">
  34. </Button>
  35. </LinearLayout>
  36. </RelativeLayout>


  1. public class InputFaceActivity extends Activity{
  2. private MessageFaceModel mMessageFaceModel = MessageFaceModel.getInstance();
  3. public static final int SELECT_STATE_FACE_ICON = 209;
  4. public static final int SELECT_MESSAGE_FACE_ICON = 109;
  5. private int mWidth = 0;
  6. @Override
  7. protected void onCreate(Bundle savedInstanceState) {
  8. // TODO Auto-generated method stub
  9. super.onCreate(savedInstanceState);
  10. requestWindowFeature(Window.FEATURE_NO_TITLE);
  11. mWidth = this.getResources().getDimensionPixelSize(R.dimen.image_width);
  12. getWindow().setFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM,
  13. WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
  14. setContentView(R.layout.input_face_activity);
  15. GridView gridView = (GridView) findViewById(R.id.input_face_gridview);
  16. gridView.setAdapter(new FaceListAdapter());
  17. gridView.setOnItemClickListener(new FaceListOnItemClickListener());
  18. Button cancelButton = (Button)findViewById(R.id.input_face_cancel_button);
  19. cancelButton.setOnClickListener(new OnClickListener(){
  20. @Override
  21. public void onClick(View arg0) {
  22. finish();
  23. }
  24. });
  25. }
  26. private class FaceListAdapter extends BaseAdapter {
  27. public int getCount() {
  28. if(mMessageFaceModel.getFaceIcons() != null){
  29. return mMessageFaceModel.getFaceIcons().size();
  30. }else{
  31. return 0;
  32. }
  33. }
  34. public Object getItem(int arg0) {
  35. return arg0;
  36. }
  37. public long getItemId(int arg0) {
  38. return arg0;
  39. }
  40. public View getView(int position, View convertView, ViewGroup parent) {
  41. ImageView view = new ImageView(InputFaceActivity.this);
  42. view.setImageBitmap(mMessageFaceModel.getFaceIcons().get(position));
  43. view.setLayoutParams(new GridView.LayoutParams(mWidth, mWidth));
  44. view.setScaleType(ScaleType.CENTER);
  45. return view;
  46. }
  47. }
  48. }

以上就是android如何使用DOM来解析XML+如果做一个表情的弹出框 的内容,更多相关内容请关注PHP中文网(www.gxlsystem.com)!

热门排行

今日推荐

热门手游