android的自定义表情啊?貌似还没有发现有客户端有,都是图片,如果能像在电脑上那样自定义表情的功能多好,那位大哥知道,麻烦告知一声,呵呵。写完,睡觉。


我们仔细来观察下腾讯微博的qq表情发送规律,由/开始,1到3个中文或者英文字符.


写个工具类来测试已测试正则表达式来匹配表情。


在上方输入框中可以输入查询 格式为 @你选择的列表值


这个是话题输入界面,格式为#话题#


表情选择页面,这个其实是一个每行5列的GridView


此界面可看到你写的微博的内容,点击发送,发送成功


哈哈,看到了吧,我的微博首页已经显示了我刚才发送的带有话题@person和表情的微博了。

接下来,上代码。

Java代码

  1. public
  2. class AddWeiboActivity extends Activity implements OnClickListener{
  3. private DataHelper dataHelper;
  4. private UserInfo user;
  5. private String user_default_name;
  6. private MyWeiboSync weibo;
  7. private ListView listView;
  8. private EditText weibo_content;
  9. private Button send_btn;
  10. private Button add_cmamera_btn;
  11. private Button add_at_btn;
  12. private Button add_topic_btn;
  13. private Button add_expression_btn;
  14. private Button add_location_btn;
  15. private GridView expressionGrid;
  16. private List<Map<String,Object>> expressionList;
  17. private ExpressionAdapter expressionAdapter;
  18. private FrameLayout operation_layout;
  19. private RelativeLayout add_top_bar;
  20. private ListView atListView;
  21. private RelativeLayout atRootLayout;
  22. private EditText atEditText;
  23. private Button atEnterBtn;
  24. private TextView topic_tip;
  25. private RelativeLayout.LayoutParams atEdiLayoutParams,atEnterBtnLayoutParams,atListViewLayoutParams,topicTipViewLayoutParams;
  26. private JSONArray array;
  27. private Handler handler;
  28. private ArrayAdapter atAdapter;
  29. private List<String> atList;
  30. private AtThread thread;
  31. private List<String> matchStrList;//选择atList匹配的字符串
  32. private
  33. int flag;
  34. private
  35. static
  36. int FLAG_1 = 1;
  37. private
  38. static
  39. int FLAG_2 = 2;//1和2代表atEnterBtn的父亲控件不同
  40. @Override
  41. protected
  42. void onCreate(Bundle savedInstanceState) {
  43. super.onCreate(savedInstanceState);
  44. setContentView(R.layout.add_weibo);
  45. setUpViews();
  46. setUpListeners();
  47. dataHelper = DataBaseContext.getInstance(getApplicationContext());
  48. weibo = WeiboContext.getInstance();
  49. SharedPreferences preferences = getSharedPreferences("default_user",Activity.MODE_PRIVATE);
  50. user_default_name = preferences.getString("user_default_name", "");//取得微博默认登录账号信息
  51. handler = new AtHandler();
  52. thread = new AtThread();
  53. thread.start();//开启一个线程获取数据
  54. }
  55. private
  56. void setUpViews(){
  57. weibo_content = (EditText)findViewById(R.id.weibo_content);
  58. send_btn = (Button)findViewById(R.id.send_btn);
  59. add_cmamera_btn = (Button)findViewById(R.id.add_cmamera_btn);
  60. add_at_btn = (Button)findViewById(R.id.add_at_btn);
  61. add_topic_btn = (Button)findViewById(R.id.add_topic_btn);
  62. add_expression_btn = (Button)findViewById(R.id.add_expression_btn);
  63. add_location_btn = (Button)findViewById(R.id.add_location_btn);
  64. add_top_bar = (RelativeLayout)findViewById(R.id.add_top_bar);
  65. operation_layout = (FrameLayout)findViewById(R.id.operation_layout);
  66. expressionGrid = new GridView(this);
  67. expressionGrid.setNumColumns(5);
  68. expressionList = buildExpressionsList();
  69. expressionAdapter = new ExpressionAdapter(AddWeiboActivity.this, expressionList);
  70. expressionGrid.setAdapter(expressionAdapter);
  71. //以下代码至本方法setUpViews结束,是个人纯粹蛋疼联系纯代码布局,各位老大可以改成xml布局,淡定
  72. atRootLayout = new RelativeLayout(AddWeiboActivity.this);
  73. atEditText = new EditText(AddWeiboActivity.this);
  74. atEditText.setId(10000);
  75. atEnterBtn = new Button(AddWeiboActivity.this);
  76. atEnterBtn.setBackgroundDrawable(getResources().getDrawable(R.drawable.btn_enter_selector));
  77. atListView = new ListView(AddWeiboActivity.this);
  78. atListView.setCacheColorHint(Color.TRANSPARENT);//防止滑屏时出现黑快,不信可以注释掉此句试一试
  79. atListView.setDivider(getResources().getDrawable(R.drawable.list_divider));//设置分割线
  80. atListView.setBackgroundColor(Color.argb(255, 239, 239, 239));//alpha通道一定不要设置成透明的了,要不然textView什么也看不见,因为这个我找了很久,以为代码错了,最后才发现是透明的
  81. topic_tip = new TextView(AddWeiboActivity.this);
  82. topic_tip.setText("请输入话题");
  83. topic_tip.setTextSize(20);
  84. topic_tip.setTextColor(Color.argb(255, 90, 142, 189));//alpha通道一定不要设置成透明的了,要不然textView什么也看不见,因为这个我找了很久,以为代码错了,最后才发现是透明的
  85. atRootLayout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
  86. atEdiLayoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,80);
  87. atEnterBtnLayoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
  88. atListViewLayoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
  89. topicTipViewLayoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
  90. //添加布局约束
  91. atEdiLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
  92. atEnterBtnLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP,RelativeLayout.TRUE);
  93. atEnterBtnLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,RelativeLayout.TRUE);
  94. atEnterBtnLayoutParams.setMargins(0, 10, 10, 0);//设置边距,分别代表左,上,右,下
  95. atListViewLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM,RelativeLayout.TRUE);
  96. atListViewLayoutParams.addRule(RelativeLayout.BELOW, atEditText.getId());
  97. topicTipViewLayoutParams.addRule(RelativeLayout.BELOW, atEditText.getId());
  98. }
  99. private
  100. void setUpListeners(){
  101. send_btn.setOnClickListener(this);
  102. add_cmamera_btn.setOnClickListener(this);
  103. add_at_btn.setOnClickListener(this);
  104. add_topic_btn.setOnClickListener(this);
  105. add_expression_btn.setOnClickListener(this);
  106. add_location_btn.setOnClickListener(this);
  107. expressionGrid.setOnItemClickListener(new GridItemClickListener());
  108. atListView.setOnItemClickListener(new AtListViewItemListener());
  109. atEditText.addTextChangedListener(new MyTextWatcher());
  110. atEnterBtn.setOnClickListener(new OnClickListener() {
  111. @Override
  112. public
  113. void onClick(View v) {
  114. add_top_bar.setVisibility(View.VISIBLE);
  115. weibo_content.setVisibility(View.VISIBLE);
  116. operation_layout.setVisibility(View.GONE);
  117. operation_layout.removeAllViews();//别忘记要移除掉
  118. if(flag==FLAG_1){
  119. weibo_content.setText(weibo_content.getText()+"@");
  120. }else
  121. if(flag==FLAG_2){
  122. weibo_content.setText(weibo_content.getText()+"#"+atEditText.getText()+"#");
  123. }
  124. }
  125. });
  126. }
  127. class AtThread extends Thread {
  128. @Override
  129. public
  130. void run() {
  131. String jsonStr = weibo.getFans(weibo.getAccessTokenKey(), weibo.getAccessTokenSecrect(), 20, 0, user_default_name);
  132. try {
  133. JSONObject dataObj = new JSONObject(jsonStr).getJSONObject("data");
  134. array = dataObj.getJSONArray("info");
  135. } catch (JSONException e) {
  136. e.printStackTrace();
  137. }
  138. //通知handler处理数据
  139. Message msg = handler.obtainMessage();
  140. handler.sendMessage(msg);
  141. }
  142. }
  143. class AtHandler extends Handler {
  144. @Override
  145. public
  146. void handleMessage(Message msg){
  147. int size = array.length();
  148. atList = new ArrayList<String>();
  149. for(int i = 0;i<size;i++){
  150. JSONObject data = array.optJSONObject(i);
  151. try {
  152. atList.add(data.getString("nick")+"("+data.getString("name")+")");
  153. } catch (JSONException e) {
  154. e.printStackTrace();
  155. }
  156. }
  157. matchStrList = new ArrayList<String>();
  158. matchStrList.addAll(atList);
  159. atAdapter = new ArrayAdapter<String>(AddWeiboActivity.this,R.layout.at_list_item,R.id.at_nick_name,atList);
  160. atListView.setAdapter(atAdapter);
  161. }
  162. }
  163. class ExpressionAdapter extends BaseAdapter {
  164. private Context context;
  165. private LayoutInflater inflater;
  166. private List<Map<String,Object>> list;
  167. public ExpressionAdapter(Context context, List<Map<String,Object>> list) {
  168. super();
  169. this.context = context;
  170. this.list = list;
  171. this.inflater = LayoutInflater.from(context);
  172. }
  173. @Override
  174. public
  175. int getCount() {
  176. return list.size();
  177. }
  178. @Override
  179. public Object getItem(int position) {
  180. return list.get(position);
  181. }
  182. @Override
  183. public
  184. long getItemId(int position) {
  185. return position;
  186. }
  187. @Override
  188. public View getView(final
  189. int position, View convertView, ViewGroup parent){
  190. Map<String,Object> map = list.get(position);
  191. ImageView image = new ImageView(context);
  192. image.setImageDrawable((Drawable)map.get("drawable"));
  193. return image;
  194. }
  195. }
  196. @Override
  197. public
  198. void onClick(View v) {
  199. if(operation_layout.getChildCount()>0){
  200. add_top_bar.setVisibility(View.VISIBLE);
  201. weibo_content.setVisibility(View.VISIBLE);
  202. operation_layout.setVisibility(View.GONE);
  203. operation_layout.removeAllViews();//别忘记要移除掉
  204. return;
  205. }
  206. switch (v.getId()) {
  207. case R.id.send_btn:{
  208. String returnStr = weibo.publishMsg(weibo.getAccessTokenKey(), weibo.getAccessTokenSecrect(), weibo_content.getText().toString());
  209. try {
  210. JSONObject dataObj = new JSONObject(returnStr);
  211. if("ok".equals(dataObj.getString("msg"))){
  212. Toast.makeText(AddWeiboActivity.this, "发送成功", Toast.LENGTH_SHORT).show();//我日,记得要show,每次都搞忘记
  213. }
  214. } catch (JSONException e) {
  215. e.printStackTrace();
  216. }
  217. }
  218. break;
  219. case R.id.add_cmamera_btn:{
  220. }
  221. break;
  222. case R.id.add_at_btn:{
  223. // 动态的组装view
  224. atRootLayout.removeAllViews();// 组装前先把所有的孩子拿掉
  225. atEditText.setText("@");
  226. flag = FLAG_1;//区分atEnterBtn是在哪个界面按的
  227. atRootLayout.addView(atEditText, atEdiLayoutParams);
  228. atRootLayout.addView(atEnterBtn, atEnterBtnLayoutParams);
  229. atRootLayout.addView(atListView, atListViewLayoutParams);
  230. operation_layout.addView(atRootLayout);
  231. add_top_bar.setVisibility(View.GONE);// 隐藏上面的bar和文本编辑框,不让之与at选择相互影响
  232. weibo_content.setVisibility(View.GONE);
  233. operation_layout.setVisibility(View.VISIBLE);
  234. }
  235. break;
  236. case R.id.add_topic_btn:{
  237. //动态的组装view
  238. atRootLayout.removeAllViews();//组装前先把所有的孩子拿掉
  239. atEditText.setText("");
  240. flag = FLAG_2;//区分atEnterBtn是在哪个界面按的
  241. atRootLayout.addView(atEditText,atEdiLayoutParams);
  242. atRootLayout.addView(atEnterBtn,atEnterBtnLayoutParams);
  243. atRootLayout.addView(topic_tip,topicTipViewLayoutParams);
  244. operation_layout.addView(atRootLayout);
  245. add_top_bar.setVisibility(View.GONE);// 隐藏上面的bar和文本编辑框,不让之与at选择相互影响
  246. weibo_content.setVisibility(View.GONE);
  247. operation_layout.setVisibility(View.VISIBLE);
  248. }
  249. break;
  250. case R.id.add_expression_btn:{
  251. add_top_bar.setVisibility(View.GONE);//隐藏上面的bar和文本编辑框,不让之与表情选择的gridView相互影响
  252. weibo_content.setVisibility(View.GONE);
  253. operation_layout.addView(expressionGrid);
  254. operation_layout.setVisibility(View.VISIBLE);
  255. }
  256. break;
  257. case R.id.add_location_btn:{
  258. }
  259. break;
  260. default:
  261. break;
  262. }
  263. }
  264. private List<Map<String,Object>> buildExpressionsList(){
  265. List<Map<String,Object>> list = new ArrayList<Map<String, Object>>();
  266. DecimalFormat df = new DecimalFormat("000");//格式化数字
  267. for(int i = 0;i<105;i++){
  268. Map<String,Object> map = new HashMap<String, Object>();
  269. String formatStr = "h"+df.format(i);
  270. int drawableId = 0 ;
  271. try {
  272. drawableId = R.drawable.class.getDeclaredField(formatStr).getInt(this);//反射取得id,这个地方循环套反射,是不是很耗性能啊,我没测试过,麻烦有好办法的兄弟姐妹分享一下
  273. } catch (IllegalArgumentException e) {
  274. e.printStackTrace();
  275. } catch (SecurityException e) {
  276. e.printStackTrace();
  277. } catch (IllegalAccessException e) {
  278. e.printStackTrace();
  279. } catch (NoSuchFieldException e) {
  280. e.printStackTrace();
  281. }
  282. Drawable drawable = getResources().getDrawable(drawableId);
  283. map.put("drawableId", formatStr);
  284. map.put("drawable",drawable);
  285. list.add(map);
  286. }
  287. return list;
  288. }
  289. class GridItemClickListener implements OnItemClickListener {
  290. @Override
  291. public
  292. void onItemClick(AdapterView<?> adapterView, View view, int position,long arg3) {
  293. Map<String, Object> map = expressionList.get(position);
  294. String drawableId = (String)map.get("drawableId");
  295. add_top_bar.setVisibility(View.VISIBLE);
  296. weibo_content.setVisibility(View.VISIBLE);
  297. operation_layout.setVisibility(View.GONE);
  298. operation_layout.removeAllViews();//别忘记要移除掉
  299. String expressionStr=null;
  300. expressionStr = TextUtil.drawableIdToFaceName.get(drawableId);
  301. expressionStr="/"+expressionStr;
  302. weibo_content.setText(weibo_content.getText().toString()+expressionStr);
  303. }
  304. }
  305. class MyTextWatcher implements TextWatcher{
  306. @Override
  307. public
  308. void afterTextChanged(Editable s){
  309. String changingStr = atEditText.getText().toString();
  310. if(changingStr.indexOf("@")!=-1){
  311. changingStr = changingStr.substring(1);
  312. }
  313. int size = atList.size();
  314. matchStrList.clear();
  315. for(int i = 0;i<size;i++){
  316. String currentStr = atList.get(i);
  317. if(currentStr.indexOf(changingStr)!=-1){
  318. matchStrList.add(currentStr);
  319. }
  320. }
  321. atAdapter = new ArrayAdapter<String>(AddWeiboActivity.this,R.layout.at_list_item,R.id.at_nick_name,matchStrList);
  322. atAdapter.notifyDataSetChanged();
  323. atListView.setAdapter(atAdapter);
  324. }
  325. @Override
  326. public
  327. void beforeTextChanged(CharSequence s, int start, int count,
  328. int after) {
  329. }
  330. @Override
  331. public
  332. void onTextChanged(CharSequence s, int start, int before,int count) {
  333. }
  334. }
  335. class AtListViewItemListener implements OnItemClickListener{
  336. @Override
  337. public
  338. void onItemClick(AdapterView<?> arg0, View arg1, int position,long arg3){
  339. add_top_bar.setVisibility(View.VISIBLE);
  340. weibo_content.setVisibility(View.VISIBLE);
  341. operation_layout.setVisibility(View.GONE);
  342. operation_layout.removeAllViews();//别忘记要移除掉
  343. String str = matchStrList.get(position);
  344. String nickStr = str.substring(0,str.indexOf("("));
  345. weibo_content.setText(weibo_content.getText()+"@"+nickStr);
  346. }
  347. }
  348. }

复制代码

Java代码

  1. public
  2. class AddWeiboActivity extends Activity implements OnClickListener{
  3. private DataHelper dataHelper;
  4. private UserInfo user;
  5. private String user_default_name;
  6. private MyWeiboSync weibo;
  7. private ListView listView;
  8. private EditText weibo_content;
  9. private Button send_btn;
  10. private Button add_cmamera_btn;
  11. private Button add_at_btn;
  12. private Button add_topic_btn;
  13. private Button add_expression_btn;
  14. private Button add_location_btn;
  15. private GridView expressionGrid;
  16. private List<Map<String,Object>> expressionList;
  17. private ExpressionAdapter expressionAdapter;
  18. private FrameLayout operation_layout;
  19. private RelativeLayout add_top_bar;
  20. private ListView atListView;
  21. private RelativeLayout atRootLayout;
  22. private EditText atEditText;
  23. private Button atEnterBtn;
  24. private TextView topic_tip;
  25. private RelativeLayout.LayoutParams atEdiLayoutParams,atEnterBtnLayoutParams,atListViewLayoutParams,topicTipViewLayoutParams;
  26. private JSONArray array;
  27. private Handler handler;
  28. private ArrayAdapter atAdapter;
  29. private List<String> atList;
  30. private AtThread thread;
  31. private List<String> matchStrList;//选择atList匹配的字符串
  32. private
  33. int flag;
  34. private
  35. static
  36. int FLAG_1 = 1;
  37. private
  38. static
  39. int FLAG_2 = 2;//1和2代表atEnterBtn的父亲控件不同
  40. @Override
  41. protected
  42. void onCreate(Bundle savedInstanceState) {
  43. super.onCreate(savedInstanceState);
  44. setContentView(R.layout.add_weibo);
  45. setUpViews();
  46. setUpListeners();
  47. dataHelper = DataBaseContext.getInstance(getApplicationContext());
  48. weibo = WeiboContext.getInstance();
  49. SharedPreferences preferences = getSharedPreferences("default_user",Activity.MODE_PRIVATE);
  50. user_default_name = preferences.getString("user_default_name", "");//取得微博默认登录账号信息
  51. handler = new AtHandler();
  52. thread = new AtThread();
  53. thread.start();//开启一个线程获取数据
  54. }
  55. private
  56. void setUpViews(){
  57. weibo_content = (EditText)findViewById(R.id.weibo_content);
  58. send_btn = (Button)findViewById(R.id.send_btn);
  59. add_cmamera_btn = (Button)findViewById(R.id.add_cmamera_btn);
  60. add_at_btn = (Button)findViewById(R.id.add_at_btn);
  61. add_topic_btn = (Button)findViewById(R.id.add_topic_btn);
  62. add_expression_btn = (Button)findViewById(R.id.add_expression_btn);
  63. add_location_btn = (Button)findViewById(R.id.add_location_btn);
  64. add_top_bar = (RelativeLayout)findViewById(R.id.add_top_bar);
  65. operation_layout = (FrameLayout)findViewById(R.id.operation_layout);
  66. expressionGrid = new GridView(this);
  67. expressionGrid.setNumColumns(5);
  68. expressionList = buildExpressionsList();
  69. expressionAdapter = new ExpressionAdapter(AddWeiboActivity.this, expressionList);
  70. expressionGrid.setAdapter(expressionAdapter);
  71. //以下代码至本方法setUpViews结束,是个人纯粹蛋疼联系纯代码布局,各位老大可以改成xml布局,淡定
  72. atRootLayout = new RelativeLayout(AddWeiboActivity.this);
  73. atEditText = new EditText(AddWeiboActivity.this);
  74. atEditText.setId(10000);
  75. atEnterBtn = new Button(AddWeiboActivity.this);
  76. atEnterBtn.setBackgroundDrawable(getResources().getDrawable(R.drawable.btn_enter_selector));
  77. atListView = new ListView(AddWeiboActivity.this);
  78. atListView.setCacheColorHint(Color.TRANSPARENT);//防止滑屏时出现黑快,不信可以注释掉此句试一试
  79. atListView.setDivider(getResources().getDrawable(R.drawable.list_divider));//设置分割线
  80. atListView.setBackgroundColor(Color.argb(255, 239, 239, 239));//alpha通道一定不要设置成透明的了,要不然textView什么也看不见,因为这个我找了很久,以为代码错了,最后才发现是透明的
  81. topic_tip = new TextView(AddWeiboActivity.this);
  82. topic_tip.setText("请输入话题");
  83. topic_tip.setTextSize(20);
  84. topic_tip.setTextColor(Color.argb(255, 90, 142, 189));//alpha通道一定不要设置成透明的了,要不然textView什么也看不见,因为这个我找了很久,以为代码错了,最后才发现是透明的
  85. atRootLayout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
  86. atEdiLayoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,80);
  87. atEnterBtnLayoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
  88. atListViewLayoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
  89. topicTipViewLayoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
  90. //添加布局约束
  91. atEdiLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
  92. atEnterBtnLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_TOP,RelativeLayout.TRUE);
  93. atEnterBtnLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,RelativeLayout.TRUE);
  94. atEnterBtnLayoutParams.setMargins(0, 10, 10, 0);//设置边距,分别代表左,上,右,下
  95. atListViewLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM,RelativeLayout.TRUE);
  96. atListViewLayoutParams.addRule(RelativeLayout.BELOW, atEditText.getId());
  97. topicTipViewLayoutParams.addRule(RelativeLayout.BELOW, atEditText.getId());
  98. }
  99. private
  100. void setUpListeners(){
  101. send_btn.setOnClickListener(this);
  102. add_cmamera_btn.setOnClickListener(this);
  103. add_at_btn.setOnClickListener(this);
  104. add_topic_btn.setOnClickListener(this);
  105. add_expression_btn.setOnClickListener(this);
  106. add_location_btn.setOnClickListener(this);
  107. expressionGrid.setOnItemClickListener(new GridItemClickListener());
  108. atListView.setOnItemClickListener(new AtListViewItemListener());
  109. atEditText.addTextChangedListener(new MyTextWatcher());
  110. atEnterBtn.setOnClickListener(new OnClickListener() {
  111. @Override
  112. public
  113. void onClick(View v) {
  114. add_top_bar.setVisibility(View.VISIBLE);
  115. weibo_content.setVisibility(View.VISIBLE);
  116. operation_layout.setVisibility(View.GONE);
  117. operation_layout.removeAllViews();//别忘记要移除掉
  118. if(flag==FLAG_1){
  119. weibo_content.setText(weibo_content.getText()+"@");
  120. }else
  121. if(flag==FLAG_2){
  122. weibo_content.setText(weibo_content.getText()+"#"+atEditText.getText()+"#");
  123. }
  124. }
  125. });
  126. }
  127. class AtThread extends Thread {
  128. @Override
  129. public
  130. void run() {
  131. String jsonStr = weibo.getFans(weibo.getAccessTokenKey(), weibo.getAccessTokenSecrect(), 20, 0, user_default_name);
  132. try {
  133. JSONObject dataObj = new JSONObject(jsonStr).getJSONObject("data");
  134. array = dataObj.getJSONArray("info");
  135. } catch (JSONException e) {
  136. e.printStackTrace();
  137. }
  138. //通知handler处理数据
  139. Message msg = handler.obtainMessage();
  140. handler.sendMessage(msg);
  141. }
  142. }
  143. class AtHandler extends Handler {
  144. @Override
  145. public
  146. void handleMessage(Message msg){
  147. int size = array.length();
  148. atList = new ArrayList<String>();
  149. for(int i = 0;i<size;i++){
  150. JSONObject data = array.optJSONObject(i);
  151. try {
  152. atList.add(data.getString("nick")+"("+data.getString("name")+")");
  153. } catch (JSONException e) {
  154. e.printStackTrace();
  155. }
  156. }
  157. matchStrList = new ArrayList<String>();
  158. matchStrList.addAll(atList);
  159. atAdapter = new ArrayAdapter<String>(AddWeiboActivity.this,R.layout.at_list_item,R.id.at_nick_name,atList);
  160. atListView.setAdapter(atAdapter);
  161. }
  162. }
  163. class ExpressionAdapter extends BaseAdapter {
  164. private Context context;
  165. private LayoutInflater inflater;
  166. private List<Map<String,Object>> list;
  167. public ExpressionAdapter(Context context, List<Map<String,Object>> list) {
  168. super();
  169. this.context = context;
  170. this.list = list;
  171. this.inflater = LayoutInflater.from(context);
  172. }
  173. @Override
  174. public
  175. int getCount() {
  176. return list.size();
  177. }
  178. @Override
  179. public Object getItem(int position) {
  180. return list.get(position);
  181. }
  182. @Override
  183. public
  184. long getItemId(int position) {
  185. return position;
  186. }
  187. @Override
  188. public View getView(final
  189. int position, View convertView, ViewGroup parent){
  190. Map<String,Object> map = list.get(position);
  191. ImageView image = new ImageView(context);
  192. image.setImageDrawable((Drawable)map.get("drawable"));
  193. return image;
  194. }
  195. }
  196. @Override
  197. public
  198. void onClick(View v) {
  199. if(operation_layout.getChildCount()>0){
  200. add_top_bar.setVisibility(View.VISIBLE);
  201. weibo_content.setVisibility(View.VISIBLE);
  202. operation_layout.setVisibility(View.GONE);
  203. operation_layout.removeAllViews();//别忘记要移除掉
  204. return;
  205. }
  206. switch (v.getId()) {
  207. case R.id.send_btn:{
  208. String returnStr = weibo.publishMsg(weibo.getAccessTokenKey(), weibo.getAccessTokenSecrect(), weibo_content.getText().toString());
  209. try {
  210. JSONObject dataObj = new JSONObject(returnStr);
  211. if("ok".equals(dataObj.getString("msg"))){
  212. Toast.makeText(AddWeiboActivity.this, "发送成功", Toast.LENGTH_SHORT).show();//我日,记得要show,每次都搞忘记
  213. }
  214. } catch (JSONException e) {
  215. e.printStackTrace();
  216. }
  217. }
  218. break;
  219. case R.id.add_cmamera_btn:{
  220. }
  221. break;
  222. case R.id.add_at_btn:{
  223. // 动态的组装view
  224. atRootLayout.removeAllViews();// 组装前先把所有的孩子拿掉
  225. atEditText.setText("@");
  226. flag = FLAG_1;//区分atEnterBtn是在哪个界面按的
  227. atRootLayout.addView(atEditText, atEdiLayoutParams);
  228. atRootLayout.addView(atEnterBtn, atEnterBtnLayoutParams);
  229. atRootLayout.addView(atListView, atListViewLayoutParams);
  230. operation_layout.addView(atRootLayout);
  231. add_top_bar.setVisibility(View.GONE);// 隐藏上面的bar和文本编辑框,不让之与at选择相互影响
  232. weibo_content.setVisibility(View.GONE);
  233. operation_layout.setVisibility(View.VISIBLE);
  234. }
  235. break;
  236. case R.id.add_topic_btn:{
  237. //动态的组装view
  238. atRootLayout.removeAllViews();//组装前先把所有的孩子拿掉
  239. atEditText.setText("");
  240. flag = FLAG_2;//区分atEnterBtn是在哪个界面按的
  241. atRootLayout.addView(atEditText,atEdiLayoutParams);
  242. atRootLayout.addView(atEnterBtn,atEnterBtnLayoutParams);
  243. atRootLayout.addView(topic_tip,topicTipViewLayoutParams);
  244. operation_layout.addView(atRootLayout);
  245. add_top_bar.setVisibility(View.GONE);// 隐藏上面的bar和文本编辑框,不让之与at选择相互影响
  246. weibo_content.setVisibility(View.GONE);
  247. operation_layout.setVisibility(View.VISIBLE);
  248. }
  249. break;
  250. case R.id.add_expression_btn:{
  251. add_top_bar.setVisibility(View.GONE);//隐藏上面的bar和文本编辑框,不让之与表情选择的gridView相互影响
  252. weibo_content.setVisibility(View.GONE);
  253. operation_layout.addView(expressionGrid);
  254. operation_layout.setVisibility(View.VISIBLE);
  255. }
  256. break;
  257. case R.id.add_location_btn:{
  258. }
  259. break;
  260. default:
  261. break;
  262. }
  263. }
  264. private List<Map<String,Object>> buildExpressionsList(){
  265. List<Map<String,Object>> list = new ArrayList<Map<String, Object>>();
  266. DecimalFormat df = new DecimalFormat("000");//格式化数字
  267. for(int i = 0;i<105;i++){
  268. Map<String,Object> map = new HashMap<String, Object>();
  269. String formatStr = "h"+df.format(i);
  270. int drawableId = 0 ;
  271. try {
  272. drawableId = R.drawable.class.getDeclaredField(formatStr).getInt(this);//反射取得id,这个地方循环套反射,是不是很耗性能啊,我没测试过,麻烦有好办法的兄弟姐妹分享一下
  273. } catch (IllegalArgumentException e) {
  274. e.printStackTrace();
  275. } catch (SecurityException e) {
  276. e.printStackTrace();
  277. } catch (IllegalAccessException e) {
  278. e.printStackTrace();
  279. } catch (NoSuchFieldException e) {
  280. e.printStackTrace();
  281. }
  282. Drawable drawable = getResources().getDrawable(drawableId);
  283. map.put("drawableId", formatStr);
  284. map.put("drawable",drawable);
  285. list.add(map);
  286. }
  287. return list;
  288. }
  289. class GridItemClickListener implements OnItemClickListener {
  290. @Override
  291. public
  292. void onItemClick(AdapterView<?> adapterView, View view, int position,long arg3) {
  293. Map<String, Object> map = expressionList.get(position);
  294. String drawableId = (String)map.get("drawableId");
  295. add_top_bar.setVisibility(View.VISIBLE);
  296. weibo_content.setVisibility(View.VISIBLE);
  297. operation_layout.setVisibility(View.GONE);
  298. operation_layout.removeAllViews();//别忘记要移除掉
  299. String expressionStr=null;
  300. expressionStr = TextUtil.drawableIdToFaceName.get(drawableId);
  301. expressionStr="/"+expressionStr;
  302. weibo_content.setText(weibo_content.getText().toString()+expressionStr);
  303. }
  304. }
  305. class MyTextWatcher implements TextWatcher{
  306. @Override
  307. public
  308. void afterTextChanged(Editable s){
  309. String changingStr = atEditText.getText().toString();
  310. if(changingStr.indexOf("@")!=-1){
  311. changingStr = changingStr.substring(1);
  312. }
  313. int size = atList.size();
  314. matchStrList.clear();
  315. for(int i = 0;i<size;i++){
  316. String currentStr = atList.get(i);
  317. if(currentStr.indexOf(changingStr)!=-1){
  318. matchStrList.add(currentStr);
  319. }
  320. }
  321. atAdapter = new ArrayAdapter<String>(AddWeiboActivity.this,R.layout.at_list_item,R.id.at_nick_name,matchStrList);
  322. atAdapter.notifyDataSetChanged();
  323. atListView.setAdapter(atAdapter);
  324. }
  325. @Override
  326. public
  327. void beforeTextChanged(CharSequence s, int start, int count,
  328. int after) {
  329. }
  330. @Override
  331. public
  332. void onTextChanged(CharSequence s, int start, int before,int count) {
  333. }
  334. }
  335. class AtListViewItemListener implements OnItemClickListener{
  336. @Override
  337. public
  338. void onItemClick(AdapterView<?> arg0, View arg1, int position,long arg3){
  339. add_top_bar.setVisibility(View.VISIBLE);
  340. weibo_content.setVisibility(View.VISIBLE);
  341. operation_layout.setVisibility(View.GONE);
  342. operation_layout.removeAllViews();//别忘记要移除掉
  343. String str = matchStrList.get(position);
  344. String nickStr = str.substring(0,str.indexOf("("));
  345. weibo_content.setText(weibo_content.getText()+"@"+nickStr);
  346. }
  347. }
  348. }

复制代码

android的自定义表情相关推荐

  1. Android键盘自定义表情包,关于自定义表情键盘...

    在做输入的时候,除了可以输入系统的表情符号,项目中通常还要求输入表情图片 表情图片 1.正则表达式 一个正则表达式,就是一串有特定意义的字符,首先要编译成为一个Pattern对象,然后使用matche ...

  2. IM界面高仿微信,android表情转ios表情,支持自定义表情,支持语音(实战界面)

    前言: 2018年底由子公司来到现在的集团公司,从互联网公司变成了企业公司.在最近一个项目里,做的辛辛苦苦,功能又被砍了.没有理由,心力交瘁!本来是打算自己做IM的,现在被砍了.我就把本地功能贡献出来 ...

  3. Android 输入法键盘和自定义表情面板

    序 .项目有一版本是在优化了直播的聊天功能 ,需要自定义表情面板 .emmmm . 效果图 功能点 : 点击左边的笑脸会弹出表情面板 ,点击输入框会切换到键盘 . 直接上代码 package com. ...

  4. Android自定义表情功能的实现

    Android开发中经常用到评论.回复.和发送聊天信息的功能开发,这其中就有表情的插入发送功能,经过长时间的开发总结,先封装出插入表情的功能实现代码开源给大家,共大家参考,共同学习进步: 1.拷贝表情 ...

  5. Android融云自定义表情

    融云官方文档链接:https://www.rongcloud.cn/docs/android.html#ui_customize_extension 一开始实现功能的时候被官方知识库问答各种贴链接搞懵 ...

  6. android 自定义表情包,android基于环信的聊天和表情自定义

    环信sdk的导入 自定义聊天界面 此处只有静态图,请谅解. 自定义表情发送 自定义聊天界面 简单说下自定义的聊天界面,一个带有recyclerview和的xml文件,和对应的adapter即可.rec ...

  7. android 微博字体高亮,安卓开发札记——高仿新浪微博文字处理(实现关键字高亮,自定义表情替换并加入点击事件实现)...

    安卓开发笔记--高仿新浪微博文字处理(实现关键字高亮,自定义表情替换并加入点击事件实现) 先让大家看下效果图,这个是我自己在闲暇时间仿写的新浪微博客户端: 今天来讲讲如何实现上图的效果,这里需要用到S ...

  8. flutter聊天界面-自定义表情键盘实现

    flutter聊天界面-自定义表情键盘实现 flutter 是 Google推出并开源的移动应用开发框架,主打跨平台.高保真.高性能.开发者可以通过 Dart语言开发 App,一套代码同时运行在 iO ...

  9. 如何给你的 Android App 添加自定义表情

    上一篇文章 Android Span 原理解析 介绍了 Span 的原理.这一篇文章将介绍 Span 的应用,使用 Span 来给 App 添加自定义表情. 原理 添加自定义表情的原理其实很简单,就是 ...

最新文章

  1. matlab球坐标曲线,matlab绘制曲线subplotsphere球面坐标绘制饼图
  2. POP缩小区域扩张导致的延迟差距—Vecloud微云
  3. 前端基础入门四(JavaScript基础)
  4. jQuery.Event的一些用法
  5. 论文浅尝 | GMNN: Graph Markov Neural Networks
  6. m1笔记本android开发,Apple M1设备开发Android小tips
  7. ip动态分配痕迹会保留多久_段王爷《新国潮七剑》为你解剖新国潮还能潮多久?...
  8. 十字路口待转区什么用_左转待转区,到底怎样掉头?
  9. 使用JS禁用浏览器后退按钮
  10. 前端知识点回顾——Javascript篇(三)
  11. python音乐编程_可以编程写音乐的python库musicpy教程(第一期) musicpy的数据结构...
  12. 数字谐音记忆编码连连看网页应用
  13. 初探MySQL的语句之一
  14. IDEA2018版本相关配置
  15. 小红书java算法难吗_Java面试系列之记一次小红书之旅
  16. MT6771平台简要了解
  17. Go语言150行代码搞定苹果Apns高并发推送
  18. 数字后端基本概念介绍——Pin Blockage
  19. python弹性碰撞次数圆周率_碰撞出来的圆周率(一)
  20. 变革时代 国内通讯云服务厂商对比介绍

热门文章

  1. 闲鱼上哪些商品抢手?Python 分析后告诉你
  2. sql的DDL,DML,DQL,DCL,TCL语言
  3. 亚历山大·贝尔的传奇人生
  4. (c++)已知空间三维两个点坐标,得到直线方程以及两点之间所有的点,使用VTK进行绘制显示
  5. 用python的pyautogui库制作伪脚本
  6. 全景图下载工具软件说明书,如何快速一键下载vr平台的全景图
  7. matlab 如何画散点图
  8. 优化方案电子版_【热点文章推荐】响应曲面法优化废弃食用油脂脱色工艺
  9. 【LOSOL关卡】动线,机制以及引导的融合
  10. 没听过这些网页翻译插件,你还敢说你用谷歌浏览器?