|
13 | 13 | import android.text.SpannableString; |
14 | 14 | import android.text.SpannableStringBuilder; |
15 | 15 | import android.text.Spanned; |
16 | | -import android.text.TextUtils; |
17 | 16 | import android.text.TextWatcher; |
18 | 17 | import android.text.style.ImageSpan; |
19 | 18 | import android.util.Log; |
@@ -88,9 +87,15 @@ public void onTextChanged(CharSequence s, int start, int before, int count) { |
88 | 87 | } |
89 | 88 | if (editText.getText() != null) { |
90 | 89 | TextSpan[] spans = editText.getText().getSpans(0, editText.getText().length(), TextSpan.class); |
| 90 | + ImageSpan[] spansImg = editText.getText().getSpans(0, editText.getText().length(), ImageSpan.class); |
91 | 91 | //整体删除span |
92 | 92 | if (before == 1 && count == 0) { |
93 | 93 | for (TextSpan textSpan : spans) { |
| 94 | + if (editText.getText().getSpanEnd(textSpan) == start && !editText.getText().toString().endsWith(textSpan.getRichTextBean().name)) { |
| 95 | + editText.getText().delete(editText.getText().getSpanStart(textSpan), editText.getText().getSpanEnd(textSpan)); |
| 96 | + } |
| 97 | + } |
| 98 | + for (ImageSpan textSpan : spansImg) { |
94 | 99 | if (editText.getText().getSpanEnd(textSpan) == start) { |
95 | 100 | editText.getText().delete(editText.getText().getSpanStart(textSpan), editText.getText().getSpanEnd(textSpan)); |
96 | 101 | } |
@@ -312,64 +317,83 @@ public void insertImage(String imagePath) { |
312 | 317 | editText.append(spannableString); |
313 | 318 | } |
314 | 319 |
|
315 | | - public void insertMentions(ReadableArray args) { |
316 | | - if (args != null && args.size() > 0) { |
317 | | - RichTextBean richTextBean = handleParams(args); |
318 | | - setMentionsSpan(richTextBean); |
319 | | - } |
320 | | - } |
321 | | - |
322 | | - public void insertEmoji(ReadableArray args) { |
| 320 | + public void handleRichText(ReadableArray args) { |
323 | 321 | if (args != null && args.size() > 0) { |
324 | | - RichTextBean richTextBean = handleParams(args); |
325 | | - int startIndex = editText.getSelectionStart(); |
326 | | - Log.e("startIndex", startIndex + ""); |
327 | | - int endIndex = startIndex + richTextBean.tag.length(); |
328 | | - Log.e("endIndex", endIndex + ""); |
329 | | - Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.kuxiao); |
330 | | - ImageSpan imageSpan = new ImageSpan(mContext, bitmap); |
331 | | - if (editText.getText() != null) { |
332 | | - editText.getText().insert(startIndex, richTextBean.tag); |
| 322 | + for (int i = 0; i < args.size(); i++) { |
| 323 | + RichTextBean richTextBean = handleParams(args.getMap(i)); |
| 324 | + switch (richTextBean.type) { |
| 325 | + case 0: |
| 326 | + //普通文本 |
| 327 | + editText.setText(richTextBean.text); |
| 328 | + break; |
| 329 | + case 1: |
| 330 | + //自定义表情 |
| 331 | + insertEmoji(richTextBean); |
| 332 | + break; |
| 333 | + case 2: |
| 334 | + //@或者#话题 |
| 335 | + insertMentions(richTextBean); |
| 336 | + break; |
| 337 | + default: |
| 338 | + break; |
| 339 | + } |
333 | 340 | } |
334 | | - SpannableStringBuilder ss = SpannableStringBuilder.valueOf(editText.getText()); |
335 | | - ss.setSpan(imageSpan, startIndex, endIndex, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); |
336 | | - editText.setText(ss); |
337 | | - editText.setSelection(endIndex); |
338 | | - editText.getText().replace(startIndex, endIndex, richTextBean.content); |
339 | 341 | } |
340 | 342 | } |
341 | 343 |
|
342 | | - private RichTextBean handleParams(ReadableArray args) { |
| 344 | + private RichTextBean handleParams(ReadableMap map) { |
343 | 345 | RichTextBean richTextBean = new RichTextBean(); |
344 | | - ReadableMap map = args.getMap(0); |
| 346 | + if (map.hasKey(ActivityConst.TYPE)) { |
| 347 | + richTextBean.type = map.getInt(ActivityConst.TYPE); |
| 348 | + } |
345 | 349 | if (map.hasKey(ActivityConst.ID)) { |
346 | 350 | richTextBean.id = map.getString(ActivityConst.ID); |
347 | 351 | } |
348 | 352 | if (map.hasKey(ActivityConst.TAG)) { |
349 | | - String tag = map.getString(ActivityConst.TAG); |
350 | | - //表情 |
351 | | - if (!TextUtils.isEmpty(tag) && tag.startsWith("[")) { |
352 | | - richTextBean.tag = tag; |
353 | | - richTextBean.content = String.format(mContext.getString(R.string.insert_emoji), tag.replaceAll("\\[|\\]", "")); |
354 | | - } else { |
355 | | - richTextBean.tag = tag; |
356 | | - } |
| 353 | + richTextBean.tag = map.getString(ActivityConst.TAG); |
| 354 | + } |
| 355 | + if (map.hasKey(ActivityConst.EMOJI_TAG) && richTextBean.type == 1) { |
| 356 | + richTextBean.tag = map.getString(ActivityConst.EMOJI_TAG); |
| 357 | + richTextBean.content = String.format(mContext.getString(R.string.insert_emoji), richTextBean.tag.replaceAll("\\[|\\]", "")); |
357 | 358 | } |
358 | 359 | if (map.hasKey(ActivityConst.NAME)) { |
359 | 360 | String name = map.getString(ActivityConst.NAME); |
360 | 361 | richTextBean.name = name + " "; |
361 | | - richTextBean.content = String.format(mContext.getString(R.string.insert_mention), richTextBean.tag, name, richTextBean.id); |
| 362 | + //插入@或者# |
| 363 | + if (richTextBean.type == 2) { |
| 364 | + richTextBean.content = String.format(mContext.getString(R.string.insert_mention), richTextBean.tag, name, richTextBean.id); |
| 365 | + } |
362 | 366 | } |
363 | 367 | if (map.hasKey(ActivityConst.COLOR)) { |
364 | 368 | richTextBean.color = map.getInt(ActivityConst.COLOR); |
365 | 369 | } |
| 370 | + if (map.hasKey(ActivityConst.TEXT)) { |
| 371 | + richTextBean.text = map.getString(ActivityConst.TEXT); |
| 372 | + } |
366 | 373 | return richTextBean; |
367 | 374 | } |
368 | 375 |
|
369 | | - private void setMentionsSpan(RichTextBean richTextBean) { |
| 376 | + public void insertEmoji(RichTextBean richTextBean) { |
| 377 | + int startIndex = editText.getSelectionStart(); |
| 378 | + Log.e("startIndex", startIndex + ""); |
| 379 | + int endIndex = startIndex + richTextBean.tag.length(); |
| 380 | + Log.e("endIndex", endIndex + ""); |
| 381 | + if (editText.getText() != null) { |
| 382 | + editText.getText().insert(startIndex, richTextBean.tag); |
| 383 | + } |
| 384 | + Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.kuxiao); |
| 385 | + ImageSpan imageSpan = new ImageSpan(mContext, bitmap); |
| 386 | + SpannableStringBuilder ss = SpannableStringBuilder.valueOf(editText.getText()); |
| 387 | + ss.setSpan(imageSpan, startIndex, endIndex, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); |
| 388 | + editText.setText(ss); |
| 389 | + editText.setSelection(endIndex); |
| 390 | + editText.getText().replace(startIndex, endIndex, richTextBean.content); |
| 391 | + } |
| 392 | + |
| 393 | + private void insertMentions(RichTextBean richTextBean) { |
370 | 394 | int startIndex = editText.getSelectionStart(); |
371 | 395 | Log.e("startIndex", startIndex + ""); |
372 | | - int endIndex = startIndex + richTextBean.name.length() + richTextBean.tag.length(); |
| 396 | + int endIndex = startIndex + richTextBean.tag.length() + richTextBean.name.length(); |
373 | 397 | Log.e("endIndex", endIndex + ""); |
374 | 398 | if (editText.getText() != null) { |
375 | 399 | editText.getText().insert(startIndex, richTextBean.tag + richTextBean.name); |
|
0 commit comments