Android to write simple chat room applications _android

Source: Internet
Author: User
Tags dateformat getmessage

Recently wrote a simple chat room application, can send expression, change avatar these functions. The main technical point is how to put the expression picture into the TextView and other UI controls to display. Here is not much nonsense to say, the following is the effect chart:

Here mainly talk about how to replace the text to the expression, first of all, our pictures are stored in the local resources directory drawable and all the resource files are r This class to manage, So we can use regular expressions to find the picture ID wrapped into imagespan and then put the Imagespan into the spannablestring, and finally put spannablestring into the EditText, the following is the source code:

Package com.coreandroid.util; 
Import Java.lang.reflect.Field; 
Import Java.util.regex.Matcher; 
 
Import Java.util.regex.Pattern; 
Import Android.content.Context; 
Import android.text.Spannable; 
Import android.text.SpannableString; 
Import Android.text.style.ImageSpan; 
 
Import Android.util.Log; 
 
Import COM.COREANDROID.CHART.R; The public class Expressionutil {/** * makes a regular judgment of the spanablestring and, if it meets the requirements, replaces the * @param context * @param s with the expression picture 
      Pannablestring * @param patten * @param start/public static void MatchExpression Spannablestring spannablestring, Pattern patten, int start) throws Exception {Matcher Matcher = Patten.match 
    ER (spannablestring); 
      while (Matcher.find ()) {String key = Matcher.group (); 
      if (Matcher.start () < start) {continue; 
      Field field = R.drawable.class.getdeclaredfield (key); int resid = Field.getint (null); Generates a picture resource ID if (re) by matching the string aboveSId!= 0) {Imagespan Imagespan = new Imagespan (context, resid);//through the picture resource ID to get bitmap, with a imagespan to wrap in T end = Matcher.start () + key.length (); Calculates the length of the name of the picture, which is the length of the string to be replaced Spannablestring.setspan (Imagespan, Matcher.start (), End, Spannable.span_ exclusive_exclusive); 
          Replaces the picture in the specified position in the string if (End < Spannablestring.length ()) {//If the entire string has not been validated, continue ... 
        MatchExpression (context, spannablestring, Patten, end); 
      } break; }}/** * Gets a Spanablestring object, through the passed-in string, and makes a regular judgment * @param context * @param str * @retu RN spannablestring */public static spannablestring getexpressionstring (context, string str, string 
    Zhengze) {spannablestring spannablestring = new spannablestring (str); Pattern Sinapatten = Pattern.compile (Zhengze); 
    Generates a pattern try {matchexpression (context, spannablestring, Sinapatten, 0) by passing in a regular expression; catch (Exception e){LOG.E ("Dealexpression", E.getmessage ()); 
  return spannablestring; 
 } 
 
}

The following is the adapter of the chat list, here is the dynamic change of the layout of each item to distinguish is the statement of their own or others, the specific source code is as follows:

Package com.coreandroid.adapter; 
Import Java.text.DateFormat; 
Import Java.text.SimpleDateFormat; 
Import Java.util.Date; 
 
Import java.util.List; 
Import Android.content.Context; 
Import android.text.SpannableString; 
Import Android.text.TextUtils; 
Import Android.view.LayoutInflater; 
Import Android.view.View; 
Import Android.view.ViewGroup; 
Import Android.widget.BaseAdapter; 
Import Android.widget.ImageView; 
Import Android.widget.RelativeLayout; 
 
Import Android.widget.TextView; 
Import COM.COREANDROID.CHART.R; 
Import Com.coreandroid.entity.MessageInfo; 
Import Com.coreandroid.util.CommonUtils; 
 
Import Com.coreandroid.util.ExpressionUtil; 
 
  public class Chartlistadapter extends Baseadapter {private context context; 
 
  Private Layoutinflater Inflater; 
 
  Private list<messageinfo> data; 
 
  Private DateFormat DF; 
    Public Chartlistadapter (context context, list<messageinfo> data) {super (); 
    This.context = context; Inflater = LAYOUTINFLAter.from (context); 
    This.data = data; 
  DF = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); 
  @Override public int GetCount () {return data.size (); 
  @Override public Object getitem (int position) {return data.get (position); 
  @Override public long getitemid (int position) {return position;  
    @Override public View getview (int position, View Convertview, ViewGroup parent) {Viewholder holder = null; 
      if (Convertview = = null) {Convertview = inflater.inflate (R.layout.chart_list_item, NULL); 
      Holder = new Viewholder (Convertview); 
    Convertview.settag (holder); 
    else {holder = (Viewholder) convertview.gettag (); 
    } holder.setdata ((messageinfo) GetItem (position)); 
  return convertview; 
    Private class Viewholder {private ImageView image; 
    private TextView text; 
    Private TextView title; 
 
    Private Relativelayout RL; Public Viewholder (View CONVERTVIew) {image = (ImageView) convertview. Findviewbyid (R.id.chart_list_item_headicon); 
      Text = (TextView) convertview. Findviewbyid (R.id.chart_list_item_message); 
      title = (TextView) convertview. Findviewbyid (R.id.chart_list_item_title); 
    RL = (relativelayout) convertview. Findviewbyid (R.id.rl_chart_list_bottom); } public void SetData (Messageinfo msg) {relativelayout.layoutparams Rl_tv_msg_left = (relativelayout.layout 
      Params) text. Getlayoutparams (); 
      Relativelayout.layoutparams rl_iv_headicon_left = (relativelayout.layoutparams) image. Getlayoutparams (); 
      Relativelayout.layoutparams Rl_tv_title = (relativelayout.layoutparams) title. Getlayoutparams (); 
      Relativelayout.layoutparams Rl_buttom = (relativelayout.layoutparams) RL. Getlayoutparams (); if (! Commonutils.getdeviceid (). Equalsignorecase (Msg.getusermac ())) {//based on Local MacAddress to determine whether the message belongs to me or the other party said//If it is said by himself, it is shown on the right; if it is said by the other person, it is displayed on the left Rl_buttom.addrule (relativelayout.align_parent_to 
 
        P); 
        Rl_tv_title.addrule (Relativelayout.align_parent_bottom); 
        Rl_tv_title.addrule (Relativelayout.align_parent_left); 
 
        Rl_tv_title.addrule (Relativelayout.below, R.id.rl_chart_list_bottom); 
        Rl_iv_headicon_left.addrule (Relativelayout.align_parent_left); 
        Rl_iv_headicon_left.addrule (Relativelayout.align_parent_bottom); 
        Rl_tv_msg_left.addrule (relativelayout.right_of, R.id.chart_list_item_headicon); 
        Text.setbackgroundresource (r.drawable.incoming); 
        String titlestr = Msg.getusermac () + "-" + Df.format (new Date ()); 
      Title.settext (TITLESTR); 
 
        else {rl_buttom.addrule (relativelayout.align_parent_top); 
        Rl_tv_title.addrule (Relativelayout.align_parent_bottom); 
        Rl_tv_title.addrule (Relativelayout.align_parent_right); RL_tv_title.addrule (Relativelayout.below, R.id.rl_chart_list_bottom); 
        Rl_iv_headicon_left.addrule (Relativelayout.align_parent_right); 
        Rl_iv_headicon_left.addrule (Relativelayout.align_parent_bottom); 
        Rl_tv_msg_left.addrule (relativelayout.left_of, R.id.chart_list_item_headicon); 
        Text.setbackgroundresource (r.drawable.outgoing); 
        String titlestr = Df.format (New Date ()) + "-" + Msg.getusermac (); 
      Title.settext (TITLESTR); } if (! Textutils.isempty (Msg.getheadimage ())) {Image.setimagebitmap (Commonutils.strconvertbitmap) (Msg. GetH Eadimage ())); 
      Set Avatar} else {image.setimageresource (r.drawable.im); String str = msg.getmessage (); Message details try {spannablestring spannablestring = expressionutil. Getexpressionstring (context 
        , str, commonutils.pattern); 
      Text.settext (spannablestring); catch (exception e) {e.printstacktrace (); 
 } 
    } 
  } 
 
}

SOURCE Download: Android Chat room application

The above is the entire content of this article, I hope to learn about Android software programming help, but also hope that we support the cloud habitat community.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.