2012-07-15 48 views
6

我正在做一個簡單的聊天應用程序,我想在編輯文字時顯示笑臉,同時編寫消息。EditText上的ImageSpan(笑臉)。使用SwiftKey鍵盤不起作用

我有這個識別至極字符將通過圖像作爲替代內容的throught的ImageSpan(這被稱爲只有當一個笑臉字符被插入的EditText):

for (index = start; index < start+num_chars; index++) { 
     if (index + 1 > editable.length()) 
      continue; 
      if(emoticons.containsKey(editable.subSequence(index, index + 1).toString())){ 
      int length=1; 

      Drawable drawable = context.getResources().getDrawable(emoticons.get(editable.subSequence(index, index + 1).toString())); 
      Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap(); 


      int size=Utils.GetDipsFromPixel(context, (int)(textSize*1.3)); 

      Drawable d = new BitmapDrawable(Bitmap.createScaledBitmap(bitmap, size, size, true)); 
      int dWidth = d.getIntrinsicWidth(); 
      int dHeight = d.getIntrinsicHeight(); 

      d.setBounds(0 , -dHeight, dWidth, 0); 
      ImageSpan span; 
      span = new ImageSpan(d,ImageSpan.ALIGN_BASELINE); 
      editable.setSpan(span, index, index + length,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 

      index += length - 1; 
      } 

     } 

我使用SPAN_EXCLUSIVE_EXCLUSIVE標籤設置跨度,但我有swiftkey鍵盤的問題,因爲當我在edittext中插入笑臉時,我在imageSpan後面寫入的所有內容都會保留在圖像下方(如SPAN_EXCLUSIVE_INCLUSIVE)。與Android默認鍵盤我沒有這個問題。

我只想在EditText上使用WhatsApp應用程序相同的行爲。

有什麼建議嗎?我必須對我的代碼做任何更改?

編輯:「可編輯」變量傳遞給該方法。它是txtMessage.getText()的值,其中txtMessage是一個EditText。

謝謝!

編輯:只跨越一部分代碼!這適用於多行!我認爲問題在於使用Drawable-> Bitmap-> ResizedBitmap-> Drawable。

public static final HashMap<String, Integer> emoticons = new HashMap(); 
static { 
    emoticons.put("\ue415", R.drawable.e415); 
    emoticons.put("\ue056", R.drawable.e056); 
    emoticons.put("\ue057", R.drawable.e057); 
... 
public static Spannable getSmiledText(Context context, Spannable editable, 
     int start, int num_chars, float textSize) { 

    int index; 
    for (index = start; index < start + num_chars; index++) { 
     if (index + 1 > editable.length()) 
      continue; 
     if (EmojiLayout.emoticons.containsKey(editable.subSequence(index, 
       index + 1).toString())) { 
      int length = 1; 

      Bitmap smiley = BitmapFactory.decodeResource(context.getResources(), ((Integer) EmojiLayout.emoticons.get(editable.subSequence(index, 
        index + 1).toString()))); 
      int size = Utils.GetDipsFromPixel(context, 
      (int) (textSize * 1.37)); 

      Bitmap scaledbmp=Bitmap.createScaledBitmap(
        smiley, size, size, false); 
      ImageSpan span; 
      span = new ImageSpan(scaledbmp); 
      Log.d("EmojiLayout", "Index: " + String.valueOf(index) + "To: " 
        + String.valueOf(index + length)); 
      editable.setSpan(span, index, index + length, 
        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
      index += length - 1; 
     } 
    } 
    return editable; 
} 
+0

郵寄此缺陷給開發者,看看他們說什麼.. – Ronnie 2012-07-24 17:57:56

回答

13

使用這一個::

public static CharSequence addSmileySpans(Context ch, CharSequence your_recieved_message) 
{ 
    //smilyRegexMap = new HashMap<Integer, String>(); 

private static final HashMap<String, Integer> smilyRegexMap = new HashMap<String, Integer>(); 
smilyRegexMap.put(">:-\\(" , R.drawable.fb_grumpy); 
     smilyRegexMap.put(">:\\(" , R.drawable.fb_grumpy); 
     smilyRegexMap.put(">:-O" , R.drawable.fb_upset); 
     smilyRegexMap.put(":-\\)" , R.drawable.fb_smile); 
     smilyRegexMap.put(":\\)",R.drawable.fb_smile); 
     smilyRegexMap.put(":-\\]" , R.drawable.fb_smile); 
     smilyRegexMap.put(":-\\(", R.drawable.fb_frown); 




    System.out.println("==In Spannable Function.........."); 
    SpannableStringBuilder builder = new SpannableStringBuilder(your_recieved_message); 

    System.out.println("==================Size of Smily : "+ smilyRegexMap.size()); 

    @SuppressWarnings("rawtypes") 
    Iterator it = smilyRegexMap.entrySet().iterator(); 
    while (it.hasNext()) { 
     @SuppressWarnings("rawtypes") 
     Map.Entry pairs = (Map.Entry) it.next(); 




     Pattern mPattern = Pattern.compile((String) pairs.getKey(),Pattern.CASE_INSENSITIVE); 
     Matcher matcher = mPattern.matcher(your_recieved_message); 

     while (matcher.find()) { 

       Bitmap smiley = BitmapFactory.decodeResource(ch.getResources(), ((Integer) pairs.getValue())); 
       Object[] spans = builder.getSpans(matcher.start(), matcher.end(), ImageSpan.class); 
       if (spans == null || spans.length == 0) { 
        builder.setSpan(new ImageSpan(smiley), matcher.start(), matcher.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
       } 
     } 
    } 
    return builder; 
} 
+0

Yeeessss !!! !我與Swiftkey有同樣的問題,但用這種方法,你已經解決了其他問題與表情視圖項目與表情文字換行!!!!現在我只需要提高滾動速度,因爲我有470個笑臉!任何提高滾動速度的建議? – Igor 2012-07-31 22:12:12

+0

I'Have編輯你的代碼只能跨越一部分字符串,我已經發布在第一條消息 – Igor 2012-07-31 22:39:54

+0

我正在使用對話框視圖顯示錶情符號..我已經把圖片和代碼的新答案。可能是這個幫助你。 .. – 2012-08-01 10:24:50

4

你剛纔使用ImageSpan建立一個Spannable文本,然後的setText Spannable到的TextView或EditText上的CommonsWare在這個post建議。您也可以嘗試使用A-IV's解決方案:

private static final HashMap<String, Integer> emoticons = new HashMap(); 
    static { 
     emoticons.put(":*", R.drawable.emo_im_kiss); 
     emoticons.put(":-D", R.drawable.emo_im_glad); 
     emoticons.put(":)", R.drawable.emo_im_happy); 
     emoticons.put(":-(", R.drawable.emo_im_sad); 
     ... 
    } 

    public static Spannable getSmiledText(Context context, String text) { 

    SpannableStringBuilder builder = new SpannableStringBuilder(text); 
    int index; 

    for (index = 0; index < builder.length(); index++) { 
     for (Entry<String, Integer> entry : emoticons.entrySet()) { 
      int length = entry.getKey().length(); 
      if (index + length > builder.length()) 
       continue; 
      if (builder.subSequence(index, index + length).toString().equals(entry.getKey())) { 
       builder.setSpan(new ImageSpan(context, entry.getValue()), index, index + length, 
       Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
       index += length - 1; 
       break; 
      } 
    } 
    } 
    return builder; 
    } 
+0

我已經試過這個解決方案,但我的問題是,我每次在EditText上插入笑臉時都會這樣做,而我只想改變新插入的笑臉字符。我必須傳遞給方法txtMessage.getText()(Spannable),所以我只能改變新的字符。如果我每次插入笑臉都必須檢查所有字符,那麼您發送的方法是不合適的。 – Igor 2012-07-16 08:26:44

+0

我試圖將ImageSpan設置爲SpannableStringBuilder,結果是相同的...使用SwiftKey鍵盤我不能在ImageSpan後面寫入,因爲所有字符都被ImageSpan隱藏.... :-( – Igor 2012-07-16 15:32:50

-1

在XML文件的表情把圖像按鈕

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 

android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" > 
<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="#dddddd" 
    android:padding="10dp" > 

    <ImageButton 
     android:id="@+id/btn_smile" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:background="@null" 
     android:padding="10dp" 
     android:src="@drawable/fb_smile" /> 

    <ImageButton 
     android:id="@+id/btn_kiss" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:background="@null" 
     android:padding="10dp" 
     android:src="@drawable/fb_kiss" /> 

    <ImageButton 
     android:id="@+id/btn_wink" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:background="@null" 
     android:padding="10dp" 
     android:src="@drawable/fb_wink" /> 

    <ImageButton 
     android:id="@+id/btn_cry" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:background="@null" 
     android:padding="10dp" 
     android:src="@drawable/fb_cry" /> 

    <ImageButton 
     android:id="@+id/btn_grumpy" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:background="@null" 
     android:padding="10dp" 
     android:src="@drawable/fb_grumpy" /> 

    <ImageButton 
     android:id="@+id/btn_upset" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:background="@null" 
     android:padding="10dp" 
     android:src="@drawable/fb_upset" /> 
</LinearLayout> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="#dddddd" 
    android:padding="10dp" > 

    <ImageButton 
     android:id="@+id/btn_frown" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:background="@null" 
     android:padding="10dp" 
     android:src="@drawable/fb_frown" /> 

    <ImageButton 
     android:id="@+id/btn_tougn" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:background="@null" 
     android:padding="10dp" 
     android:src="@drawable/fb_tounge" /> 

    <ImageButton 
     android:id="@+id/btn_grin" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:background="@null" 
     android:padding="10dp" 
     android:src="@drawable/fb_grin" /> 

    <ImageButton 
     android:id="@+id/btn_gasp" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:background="@null" 
     android:padding="10dp" 
     android:src="@drawable/fb_gasp" /> 

    <ImageButton 
     android:id="@+id/btn_glasses" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:background="@null" 
     android:padding="10dp" 
     android:src="@drawable/fb_glasses" /> 

    <ImageButton 
     android:id="@+id/btn_unsure" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:background="@null" 
     android:padding="10dp" 
     android:src="@drawable/fb_unsure" /> 
</LinearLayout> 
</LinearLayout> 

// === ================================================== ===

在對話框類把這段代碼

private class SmileyMenu extends Dialog implements android.view.View.OnClickListener{ 

    private Chat cht; 
    Context con; 
    public String MyStatus1=""; 
    public SmileyMenu(Chat cht) { 
     super(cht); 
     this.con = cht; 
     this.cht = cht; 
    } 



    @Override 
    protected void onStart() { 
     // TODO Auto-generated method stub 
     super.onStart(); 


     setTitle("Select Smileys :"); 

     setContentView(R.layout.smily_menu); 

     ImageButton btnSmile = (ImageButton)findViewById(R.id.btn_smile); 
     ImageButton btnkiss = (ImageButton)findViewById(R.id.btn_kiss); 
     ImageButton btnwink = (ImageButton)findViewById(R.id.btn_wink); 
     ImageButton btncry = (ImageButton)findViewById(R.id.btn_cry); 
     ImageButton btngrupy = (ImageButton)findViewById(R.id.btn_grumpy); 
     ImageButton btnupset = (ImageButton)findViewById(R.id.btn_upset); 
     ImageButton btnfrown = (ImageButton)findViewById(R.id.btn_frown); 
     ImageButton btngrin = (ImageButton)findViewById(R.id.btn_grin); 
     ImageButton btngasp = (ImageButton)findViewById(R.id.btn_gasp); 
     ImageButton btnglass = (ImageButton)findViewById(R.id.btn_glasses); 
     ImageButton btnunsure = (ImageButton)findViewById(R.id.btn_unsure); 
     ImageButton btndevil = (ImageButton)findViewById(R.id.btn_devil); 
     ImageButton btnheart = (ImageButton)findViewById(R.id.btn_heart); 
     ImageButton btnpacman = (ImageButton)findViewById(R.id.btn_pacman); 
     ImageButton btn42 = (ImageButton)findViewById(R.id.btn_42); 


     btnSmile.setOnClickListener(this); 
     btnkiss.setOnClickListener(this); 
     btnwink.setOnClickListener(this); 
     btncry.setOnClickListener(this); 
     btngrupy.setOnClickListener(this); 
     btnupset.setOnClickListener(this); 
     btnfrown.setOnClickListener(this); 
     btngrin.setOnClickListener(this); 
     btngasp.setOnClickListener(this); 
     btnglass.setOnClickListener(this); 
     btnunsure.setOnClickListener(this); 
     btndevil.setOnClickListener(this); 
     btnheart.setOnClickListener(this); 
     btnpacman.setOnClickListener(this); 
     btn42.setOnClickListener(this); 




    } 



    @Override 
    public void onClick(View v) { 


     switch (v.getId()) { 

     case R.id.btn_smile: 
      cht.setMyText(":-)"); 
      break; 

     case R.id.btn_kiss: 
      cht.setMyText(":-*"); 
      break; 

     case R.id.btn_wink: 
      cht.setMyText(";-)"); 
      break; 

     case R.id.btn_cry: 
      cht.setMyText(":'("); 
      break; 

     case R.id.btn_grumpy: 
      cht.setMyText(">:-("); 
      break; 

     case R.id.btn_upset: 
      cht.setMyText(">:-O"); 
      break; 

     case R.id.btn_frown: 
      cht.setMyText(":-("); 
      break; 

     case R.id.btn_tougn: 
      cht.setMyText(":-p"); 
      break; 

     case R.id.btn_grin: 
      cht.setMyText(":-D"); 
      break; 

     case R.id.btn_gasp: 
      cht.setMyText(":-O"); 
      break; 

     case R.id.btn_glasses: 
      cht.setMyText("8-)"); 
      break; 

     case R.id.btn_unsure: 
      cht.setMyText(":-/"); 
      break; 

     case R.id.btn_devil: 
      cht.setMyText("3:-)"); 
      break; 

     case R.id.btn_heart: 
      cht.setMyText("<3"); 
      break; 

     case R.id.btn_pacman: 
      cht.setMyText(":v"); 
      break; 

     case R.id.btn_42: 
      cht.setMyText(":42:"); 
      break; 

     default: 
      Toast.makeText(con, "Sorryyyyyy", Toast.LENGTH_SHORT).show(); 
      break; 
     } 

     dismiss(); 
    } 
} 

// ==================

圖像當在運行時笑臉菜單中,單擊按鈕

enter image description here

======================================== =============

試試這可能是你的幫助..祝你好運..

+0

我的問題是要提高消息列表視圖的速度,因爲列表適配器每次顯示消息時都必須找到表情符號。我實現了某種緩存但用戶可以在應用程序配置中更改字體大小,並重新計算笑臉尺寸,使其與文本具有相同的高度。不管怎麼說,還是要謝謝你! – Igor 2012-08-01 12:16:53

0

試試這種方式,希望這會幫助你解決你的問題。

private static LinkedHashMap<String,Integer> emojisHashMap; 

    public static LinkedHashMap<String, Integer> getEmojisHashMap() { 
     if (emojisHashMap == null || emojisHashMap.size() == 0) { 
      emojisHashMap = new LinkedHashMap<String, Integer>(); 

      emojisHashMap.put(":=q", R.drawable.smiley1); 
      emojisHashMap.put(":=w", R.drawable.smiley2); 
      emojisHashMap.put(":=e", R.drawable.smiley3); 
      emojisHashMap.put(":=r", R.drawable.smiley4); 

      return emojisHashMap; 
     } else { 
      return emojisHashMap; 
     } 

    } 

    public Spannable getSmiledText(CharSequence text) { 
     SpannableStringBuilder builder; 

     try { 
      builder = (SpannableStringBuilder) text; 
     }catch (Exception e){ 
      builder = new SpannableStringBuilder(text); 
     } 
     if (getEmojisHashMap().size() > 0) { 
      int index; 
      for (index = 0; index < builder.length(); index++) { 
       if (Character.toString(builder.charAt(index)).equals(":")) { 
        for (Map.Entry<String, Integer> entry : getEmojisHashMap().entrySet()) { 
         int length = entry.getKey().length(); 
         if (index + length > builder.length()) 
          continue; 
         if (builder.subSequence(index, index + length).toString().equals(entry.getKey())) { 
          builder.setSpan(new ImageSpan(getContext(), entry.getValue()), index, index + length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
          index += length - 1; 
          break; 
         } 
        } 
       } 
      } 
     } 
     return builder; 
    }