2011-09-01 74 views
4

我想製作一個需要表情圖標的聊天應用程序。我試圖編寫我自己的TextBox來處理一些unicode字符,並用微笑和表情符號替換它們,但這是一項艱鉅的工作,我遇到了許多問題。黑莓:聊天應用中的表情圖標

然後我試圖做的是將在它的表情符號自定義字體,但我發現,字體是黑白的,不能着色。

我試圖然後使用不同的EditFields所以當我找到一個位圖我畫,並開始不同的EditField中,但幾行發生和選擇問題沒有工作。

最好的方法是編寫我自己的textField,它將找到一個特殊的unicode caracter,添加2個空格,獲取unicode caracter的位置,然後在該位置繪製位圖圖像。但是,這時候的表情符號的數量變得更大

是否有工具或API還是有辦法,我錯過了,會做的表情對我來說BlackBerry設備上的很慢?請這個話題我到處搜尋,並沒有發現尚未

在幫助
+0

搜索周圍(http://supportforums.blackberry.com/t5/Java-Development/bd-p/java_dev) ,很多人都問過這個問題,你可能會在那裏得到一些很好的建議。 – jprofitt

+0

我是那些詢問他們3個以上問題的人之一,但仍無法得到答案 –

+3

我在http://stackoverflow.com/questions/6175545/blackberry-smileys上給出了一個相當長的可能解決方案的描述在文本字段/ 6176555#6176555它可能是helfpul – jprofitt

回答

3

我們可以嘗試使用一個網絡論壇的方法:

  • 允許用戶爲表情符號輸入ASCII碼
  • 在消息編輯模式下顯示錶情
  • 爲文本代碼
  • 在消息歷史記錄中顯示錶情的圖像(使用BrowserField用於此目的)
  • 使用額外的HTML格式

simulator screen shot with chat application

試試這個代碼:在Java發展論壇RIM

public final class ChatScreen extends MainScreen implements FieldChangeListener { 
    static final int INT_MSG_MAX_LEN = 200; 
    static final DateFormat FRMT_TIME = DateFormat 
      .getInstance(DateFormat.TIME_SHORT); 
    static final String STR_FRMT_IMG = "<img src=\"{0}\"></img>"; 
    static final String STR_FRMT_MSG = "<b>{0}</b> [{1}]: {2}</p>"; 

    // several emoticon ASCII codes 
    static final String STR_CODE_SMILE = ":)"; 
    static final String STR_CODE_SADSMILE = ":("; 
    static final String STR_CODE_WINK = ";)"; 

    // use emoticons from http://www.skypeemoticonslist.com 
    static final String STR_IMG_URL = "http://www.skypeemoticonslist.com/images/"; 
    static final String STR_IMG_NAME_SMILE = "emoticon-0100-smile.png"; 
    static final String STR_IMG_NAME_SADSMILE = "emoticon-0101-sadsmile.png"; 
    static final String STR_IMG_NAME_WINK = "emoticon-0105-wink.png"; 

    static final Hashtable TABLE_URL = new Hashtable(); 
    static final Hashtable TABLE_IMG = new Hashtable(); 

    private static void initCode(String code, String imgFName) { 
     String tag; 
     // prepare table for online use 
     // generate img tag with live url 
     tag = MessageFormat.format(STR_FRMT_IMG, new Object[] { STR_IMG_URL 
       + imgFName }); 
     TABLE_URL.put(code, tag); 

     // prepare table for offline use 
     // retrieve image from project resources 
     try { 
      EncodedImage img = EncodedImage.getEncodedImageResource(imgFName); 
      // generate img tag with embedded data url 
      String dataStr = getDataUrl(img.getData(), img.getMIMEType()); 
      tag = MessageFormat.format(STR_FRMT_IMG, new Object[] { dataStr }); 
      TABLE_IMG.put(code, tag); 
     } catch (IOException e) { 
      System.out.println("\n Troubles preparing res for code " + code 
        + " \n"); 
     } 
    } 

    static { 
     initCode(STR_CODE_SMILE, STR_IMG_NAME_SMILE); 
     initCode(STR_CODE_SADSMILE, STR_IMG_NAME_SADSMILE); 
     initCode(STR_CODE_WINK, STR_IMG_NAME_WINK); 
    } 

    boolean mIsOffline = true; 
    String mChatHistory = ""; 

    BrowserField mBrowserField = new BrowserField(); 
    EditField mTextField = new EditField("Input message: ", "", 
      INT_MSG_MAX_LEN, Field.USE_ALL_WIDTH); 
    ButtonField mBtnUserLeft = new ButtonField("Send as Mr. Left", 
      Field.FIELD_LEFT | ButtonField.CONSUME_CLICK); 
    ButtonField mBtnUserRight = new ButtonField("Send as Mr. Right", 
      Field.FIELD_RIGHT | ButtonField.CONSUME_CLICK); 

    public ChatScreen() { 
     super(Manager.NO_VERTICAL_SCROLL | Manager.NO_HORIZONTAL_SCROLLBAR); 
     add(mTextField); 
     HorizontalFieldManager hfm = new HorizontalFieldManager(
       Field.USE_ALL_WIDTH | Field.FIELD_HCENTER); 
     mBtnUserLeft.setChangeListener(this); 
     mBtnUserRight.setChangeListener(this); 
     hfm.add(mBtnUserLeft); 
     hfm.add(mBtnUserRight); 
     add(hfm); 
     VerticalFieldManager vfm = new VerticalFieldManager(Field.USE_ALL_WIDTH 
       | Manager.VERTICAL_SCROLL | Manager.VERTICAL_SCROLLBAR); 
     vfm.add(mBrowserField); 
     add(vfm); 
    } 

    protected void makeMenu(Menu menu, int instance) { 
     menu.add(new MenuItem(mIsOffline ? "Go Online" : "Go Offline", 0, 0) { 
      public void run() { 
       mIsOffline = !mIsOffline; 
      } 
     }); 
     super.makeMenu(menu, instance); 
    } 

    public void fieldChanged(final Field field, int context) { 
     if (field == mBtnUserLeft) { 
      addMessage("Mr. Left"); 
     } else if (field == mBtnUserRight) { 
      addMessage("Mr. Right"); 
     } 
    } 

    private void addMessage(String userName) { 
     String message = mTextField.getText(); 

     // update message with emoticons 
     message = replaceCodeWithImg(message, STR_CODE_SMILE); 
     message = replaceCodeWithImg(message, STR_CODE_SADSMILE); 
     message = replaceCodeWithImg(message, STR_CODE_WINK); 
     String timeStr = FRMT_TIME.format(new Date(System.currentTimeMillis())); 
     String text = MessageFormat.format(STR_FRMT_MSG, new Object[] { 
       userName, timeStr, message }); 
     mChatHistory = text + mChatHistory; 
     mTextField.setText(""); 
     // fix IllegalStateException up to 
     // http://supportforums.blackberry.com/t5/Java-Development/IllegalStateException-at-displayContent-on-browserfield/td-p/1071991 
     mBrowserField.setFocus(); 

     mBrowserField.displayContent("<html>" + mChatHistory + "</html>", ""); 
    } 

    private String replaceCodeWithImg(String message, String code) { 
     Hashtable table = mIsOffline ? TABLE_IMG : TABLE_URL; 
     int index = message.indexOf(code); 
     while (index != -1) { 
      String begin = message.substring(0, index); 
      String end = message.substring(index + code.length()); 
      String tag = (String) table.get(code); 
      message = begin + tag + end; 

      index = message.indexOf(code, index + tag.length()); 
     } 
     return message; 
    } 

    // src taken from http://bfo.com/blog/files/src/DataStreamHandler.java 
    private static final String getDataUrl(byte[] data, String mimetype) 
      throws IOException { 
     final StringBuffer sb = new StringBuffer(); 
     sb.append("data:"); 
     sb.append(mimetype); 
     sb.append(";base64,"); 
     OutputStream out = new OutputStream() { 
      public void write(int c) { 
       sb.append((char) (c & 0xFF)); 
      } 
     }; 
     Base64OutputStream fout = new Base64OutputStream(out); 
     fout.write(data); 
     fout.close(); 
     return sb.toString(); 
    } 
} 
+0

好吧,沒有它不是適當的方案我想在編輯字段和富文本字段中使用表情符號。我不想使用瀏覽器字段。我想要選擇事件和複製事件 –

+2

@Farid如果你想複製/粘貼/修改功能 - 你仍然可以做到這一點。將每條消息顯示爲單獨的瀏覽器字段。然後在編輯或查看將文本版本放入文本框中。 –

+0

我沒有清楚地告訴你,你怎麼能從瀏覽器域複製文本? –