android 新浪微博客户端的表情功能的实现,Android开发入门教程

  1. }

  2. }

  3. }

  4. @Override

  5. public void endDocument() throws SAXException {

  6. super.endDocument();

  7. }

  8. @Override

  9. public void endElement(String uri, String localName, String qName)

  10. throws SAXException {

  11. tag = null;

  12. if (“mb”.equals(localName)) {

  13. } else if (“emotions”.equals(localName)) {

  14. responseresult =null;

  15. } else if (“emotion”.equals(localName)) {

  16. list.add(emotions);

  17. emotions = null;

  18. }

  19. }

  20. @Override

  21. public void startDocument() throws SAXException {

  22. list = new ArrayList();

  23. }

  24. @Override

  25. public void startElement(String uri, String localName, String qName,

  26. Attributes attributes) throws SAXException {

  27. if (“mb”.equals(localName)) {

  28. responseresult = new ResponseResult();

  29. } else if (“emotions”.equals(localName)) {

  30. } else if (“emotion”.equals(localName)) {

  31. emotions = new Emotions();

  32. }

  33. tag = localName;

  34. }

  35. }

package com.uim.microblog.net.handler;

import java.util.ArrayList;

import java.util.List;

import org.xml.sax.Attributes;

import org.xml.sax.SAXException;

import org.xml.sax.helpers.DefaultHandler;

import com.uim.microblog.model.Emotions;

import com.uim.microblog.model.ResponseResult;

public class BlogEmotionsHandler extends DefaultHandler {

private List list;

private Emotions emotions;

private ResponseResult responseresult;

private String tag = null;//正在解析的元素

public List getEmotionsList(){

return list;

}

@Override

public void characters(char[] ch, int start, int length)

throws SAXException {

if (tag != null) {

String textArea = new String(ch,start,length);

/*开始解析表情数据/

if (“phrase”.equals(tag)) {

emotions.setPhrase(textArea);

} else if (“type”.equals(tag)) {

emotions.setType(textArea);

} else if (“url”.equals(tag)) {

try {

emotions.setUrl(textArea);

String imageName = textArea.substring(textArea.lastIndexOf(“/”) + 1,textArea.length() - 4);

emotions.setImageName(imageName);

} catch (Exception e) {

e.printStackTrace();

}

} else if (“is_hot”.equals(tag)) {

emotions.setIsHot(textArea);

} else if (“is_common”.equals(tag)) {

emotions.setIsCommon(textArea);

} else if (“order_number”.equals(tag)) {

emotions.setOrderNumber(textArea);

} else if (“category”.equals(tag)) {

emotions.setCategory(textArea);

} else if (“retn”.equals(tag)) {

responseresult.setRetn(textArea);

} else if (“desc”.equals(tag)) {

responseresult.setDesc(textArea);

}

}

}

@Override

public void endDocument() throws SAXException {

super.endDocument();

}

@Override

public void endElement(String uri, String localName, String qName)

throws SAXException {

tag = null;

if (“mb”.equals(localName)) {

} else if (“emotions”.equals(localName)) {

responseresult =null;

} else if (“emotion”.equals(localName)) {

list.add(emotions);

emotions = null;

}

}

@Override

public void startDocument() throws SAXException {

list = new ArrayList();

}

@Override

public void startElement(String uri, String localName, String qName,

Attributes attributes) throws SAXException {

if (“mb”.equals(localName)) {

responseresult = new ResponseResult();

} else if (“emotions”.equals(localName)) {

} else if (“emotion”.equals(localName)) {

emotions = new Emotions();

}

tag = localName;

}

}

3.4sax解析

Java代码  

  1. public List getEmotion(){

  2. BlogGetData getdata = new BlogGetData();

  3. String result = getdata.blogEmotionsServlet();

  4. try {

  5. //生成SAX解析对象

  6. parser = SAXParserFactory.newInstance().newSAXParser();

  7. //生成xml读取器

  8. reader = parser.getXMLReader();

  9. BlogEmotionsHandler handler = new BlogEmotionsHandler();

  10. //设置Handler

  11. reader.setContentHandler(handler);

  12. //指定文件,进行解析

  13. reader.parse(new InputSource(new StringReader(result)));

  14. //获取 List

  15. emotionList = handler.getEmotionsList();

  16. } catch (ParserConfigurationException e) {

  17. e.printStackTrace();

  18. } catch (SAXException e) {

  19. e.printStackTrace();

  20. } catch (IOException e) {

  21. e.printStackTrace();

  22. }

  23. return emotionList;

  24. }

public List getEmotion(){

BlogGetData getdata = new BlogGetData();

String result = getdata.blogEmotionsServlet();

try {

//生成SAX解析对象

parser = SAXParserFactory.newInstance().newSAXParser();

//生成xml读取器

reader = parser.getXMLReader();

BlogEmotionsHandler handler = new BlogEmotionsHandler();

//设置Handler

reader.setContentHandler(handler);

//指定文件,进行解析

reader.parse(new InputSource(new StringReader(result)));

//获取 List

emotionList = handler.getEmotionsList();

} catch (ParserConfigurationException e) {

e.printStackTrace();

} catch (SAXException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

return emotionList;

}

4:

4.1实现表情选择器—GridView

Xml代码  

  1. <GridView

  2. android:id=“@+id/blog_sendmsg_gvemotion”

  3. android:layout_width=“fill_parent”

  4. android:layout_height=“150sp”

  5. android:scrollbars=“vertical”

  6. android:numColumns=“auto_fit”

  7. android:verticalSpacing=“15dp”

  8. android:background=“@color/blog_list_back”

  9. android:stretchMode=“columnWidth”

  10. android:gravity=“center”

  11. android:visibility=“gone”

  12. android:columnWidth=“40dp”>

  13. </GridView>

<GridView

android:id=“@+id/blog_sendmsg_gvemotion”

android:layout_width=“fill_parent”

android:layout_height=“150sp”

android:scrollbars=“vertical”

android:numColumns=“auto_fit”

android:verticalSpacing=“15dp”

android:background=“@color/blog_list_back”

android:stretchMode=“columnWidth”

android:gravity=“center”

android:visibility=“gone”

android:columnWidth=“40dp”>

4.2 GridView的item-----gridview_emotion_item.xml

Xml代码  

  1. <?xml version\="1.0" encoding\="utf-8"?>  
  2. <LinearLayout

  3. xmlns:android=“http://schemas.android.com/apk/res/android”

  4. android:layout_width=“fill_parent”

  5. android:layout_height=“fill_parent”>

  6. <ImageView

  7. android:id=“@+id/blog_sendmsg_emotion”

  8. android:layout_width=“wrap_content”

  9. android:layout_height=“wrap_content”

  10. android:layout_weight=“50”

  11. android:layout_gravity=“center”>

  12. </ImageView>

  13. </LinearLayout>

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout

xmlns:android=“http://schemas.android.com/apk/res/android”

android:layout_width=“fill_parent”

android:layout_height=“fill_parent”>

<ImageView

android:id=“@+id/blog_sendmsg_emotion”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:layout_weight=“50”

android:layout_gravity=“center”>

4.3代码加载表情图片到GridView进行显示

Java代码  

  1. public void addexpression(View view){

  2. if (expressionGriView.getVisibility() == View.GONE) {

  3. expressionGriView.setVisibility(View.VISIBLE);

  4. emotionList = BlogHomeActivity.emotions;

  5. ArrayList<HashMap<String, Object>> lstImageItem = new ArrayList<HashMap<String, Object>>();

  6. for(int i=0;i<70;i++)

  7. {

  8. emtions = emotionList.get(i);

  9. if (emtions != null) {

  10. HashMap<String, Object> map = new HashMap<String, Object>();

  11. Field f;

  12. try {

  13. f = (Field)R.drawable.class.getDeclaredField(emtions.getImageName());

  14. int j = f.getInt(R.drawable.class);

  15. map.put(“ItemImage”, j);//添加图像资源的ID

  16. lstImageItem.add(map);

  17. } catch (SecurityException e) {

  18. e.printStackTrace();

  19. } catch (NoSuchFieldException e) {

  20. e.printStackTrace();

  21. }catch (IllegalArgumentException e) {

  22. e.printStackTrace();

  23. } catch (IllegalAccessException e) {

  24. e.printStackTrace();

  25. }

  26. }

  27. }

  28. //生成适配器的ImageItem <====> 动态数组的元素,两者一一对应

  29. SimpleAdapter saImageItems = new SimpleAdapter(this,

  30. lstImageItem,//数据来源

  31. R.layout.blog_emotion_list,

  32. //动态数组与ImageItem对应的子项

  33. new String[] {“ItemImage”},

  34. //ImageItem的XML文件里面的一个ImageView

  35. new int[] {R.id.blog_sendmsg_emotion});

  36. expressionGriView.setAdapter(saImageItems);

  37. } else {

  38. expressionGriView.setVisibility(View.GONE);

  39. }

  40. }

public void addexpression(View view){

if (expressionGriView.getVisibility() == View.GONE) {

expressionGriView.setVisibility(View.VISIBLE);

emotionList = BlogHomeActivity.emotions;

ArrayList<HashMap<String, Object>> lstImageItem = new ArrayList<HashMap<String, Object>>();

for(int i=0;i<70;i++)

{

emtions = emotionList.get(i);

if (emtions != null) {

HashMap<String, Object> map = new HashMap<String, Object>();

Field f;

try {

f = (Field)R.drawable.class.getDeclaredField(emtions.getImageName());

int j = f.getInt(R.drawable.class);

map.put(“ItemImage”, j);//添加图像资源的ID

lstImageItem.add(map);

} catch (SecurityException e) {

e.printStackTrace();

} catch (NoSuchFieldException e) {

e.printStackTrace();

}catch (IllegalArgumentException e) {

e.printStackTrace();

} catch (IllegalAccessException e) {

e.printStackTrace();

}

}

}

//生成适配器的ImageItem <====> 动态数组的元素,两者一一对应

SimpleAdapter saImageItems = new SimpleAdapter(this,

lstImageItem,//数据来源

R.layout.blog_emotion_list,

//动态数组与ImageItem对应的子项

new String[] {“ItemImage”},

//ImageItem的XML文件里面的一个ImageView

new int[] {R.id.blog_sendmsg_emotion});

expressionGriView.setAdapter(saImageItems);

} else {

expressionGriView.setVisibility(View.GONE);

}

}

5:实现点击GridView的每一个item,处理根据item的index查找对应的表情code,然后再把code利用正则把code转换为相对应的表情图片,最后表情插入EditText进行发送

5.1:code转换为图片:

Java代码  

  1. public SpannableString txtToImg(String content){

  2. SpannableString ss = new SpannableString(content);

  3. int starts = 0;

  4. int end = 0;

  5. if(content.indexOf(“[”, starts) != -1 && content.indexOf(“]”, end) != -1){

  6. starts = content.indexOf(“[”, starts);

  7. end = content.indexOf(“]”, end);

  8. String phrase = content.substring(starts,end + 1);

  9. String imageName = “”;

  10. List list = BlogHomeActivity.emotions;

  11. for (Emotions emotions : list) {

  12. if (emotions.getPhrase().equals(phrase)) {

  13. imageName = emotions.getImageName();

  14. }

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
img
img
img
img
img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新

如果你觉得这些内容对你有帮助,可以添加V获取:vip204888 (备注Android)
img

最后

我的面试经验分享可能不会去罗列太多的具体题目,因为我依然认为面试经验中最宝贵的不是那一个个具体的题目或者具体的答案,而是结束面试时,那一刻你的感受以及多天之后你的回味~

很多人在刚接触这个行业的时候或者是在遇到瓶颈期的时候,总会遇到一些问题,比如学了一段时间感觉没有方向感,不知道该从那里入手去学习,对此我整理了一些资料,需要的可以免费分享给大家

在这里小编分享一份自己收录整理上述技术体系图相关的几十套腾讯、头条、阿里、美团等公司的面试题,把技术点整理成了视频和PDF(实际上比预期多花了不少精力),包含知识脉络 + 诸多细节,由于篇幅有限,这里以图片的形式给大家展示一部分。

【Android核心高级技术PDF文档,BAT大厂面试真题解析】

【算法合集】

【延伸Android必备知识点】

【Android部分高级架构视频学习资源】

**Android精讲视频领取学习后更加是如虎添翼!**进军BATJ大厂等(备战)!现在都说互联网寒冬,其实无非就是你上错了车,且穿的少(技能),要是你上对车,自身技术能力够强,公司换掉的代价大,怎么可能会被裁掉,都是淘汰末端的业务Curd而已!现如今市场上初级程序员泛滥,这套教程针对Android开发工程师1-6年的人员、正处于瓶颈期,想要年后突破自己涨薪的,进阶Android中高级、架构师对你更是如鱼得水,赶快领取吧!

如果你觉得这些内容对你有帮助,可以添加V获取:vip204888 (备注Android)*
[外链图片转存中…(img-cU8NNjXL-1711730259982)]

最后

我的面试经验分享可能不会去罗列太多的具体题目,因为我依然认为面试经验中最宝贵的不是那一个个具体的题目或者具体的答案,而是结束面试时,那一刻你的感受以及多天之后你的回味~

很多人在刚接触这个行业的时候或者是在遇到瓶颈期的时候,总会遇到一些问题,比如学了一段时间感觉没有方向感,不知道该从那里入手去学习,对此我整理了一些资料,需要的可以免费分享给大家

在这里小编分享一份自己收录整理上述技术体系图相关的几十套腾讯、头条、阿里、美团等公司的面试题,把技术点整理成了视频和PDF(实际上比预期多花了不少精力),包含知识脉络 + 诸多细节,由于篇幅有限,这里以图片的形式给大家展示一部分。

【Android核心高级技术PDF文档,BAT大厂面试真题解析】

[外链图片转存中…(img-pMkpRvMZ-1711730259982)]

【算法合集】

[外链图片转存中…(img-VqUNZCyx-1711730259983)]

【延伸Android必备知识点】

[外链图片转存中…(img-HEDAb1M8-1711730259983)]

【Android部分高级架构视频学习资源】

**Android精讲视频领取学习后更加是如虎添翼!**进军BATJ大厂等(备战)!现在都说互联网寒冬,其实无非就是你上错了车,且穿的少(技能),要是你上对车,自身技术能力够强,公司换掉的代价大,怎么可能会被裁掉,都是淘汰末端的业务Curd而已!现如今市场上初级程序员泛滥,这套教程针对Android开发工程师1-6年的人员、正处于瓶颈期,想要年后突破自己涨薪的,进阶Android中高级、架构师对你更是如鱼得水,赶快领取吧!

本文已被CODING开源项目:《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》收录

  • 28
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值