Android中编写简单的聊天应用程序

来源:爱站网时间:2020-12-25编辑:网友分享
在Android开发中,很多简单的功能都会使用到Android平台上,但是我们也要不断的进行创新,那么Android中编写简单的聊天应用程序,大家会操作吗?别着急,爱站技术频道小编带你细细了解。

在Android开发中,很多简单的功能都会使用到Android平台上,但是我们也要不断的进行创新,那么Android中编写简单的聊天应用程序,大家会操作吗?别着急,爱站技术频道小编带你细细了解。

这里废话不多说,下面是效果图:

 这里主要讲下怎样把文本替换到表情,先说下思路,首先我们的图片是保存在本地资源目录drawable中而所有的资源文件都是R这个类来管理,所以我们可以利用正则表达式找出图片id包装成ImageSpan然后把ImageSpan放到SpannableString中,最后把SpannableString放入edittext中,下面是源码:

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; 
 
public class ExpressionUtil { 
  /** 
   * 对spanableString进行正则判断,如果符合要求,则以表情图片代替 
   * 
   * @param context 
   * @param spannableString 
   * @param patten 
   * @param start 
   */ 
  public static void matchExpression(Context context, 
      SpannableString spannableString, Pattern patten, int start) 
      throws Exception { 
    Matcher matcher = patten.matcher(spannableString); 
    while (matcher.find()) { 
      String key = matcher.group(); 
      if (matcher.start() 

下面是聊天记录列表的adapter,这里主要是动态的改变每个Item的布局来区分是自己还是他人的发言,具体源码如下:

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 data; 
 
  private DateFormat df; 
 
  public ChartListAdapter(Context context, List 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.LayoutParams) 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())) { 
        // 根据本地的mac地址来判断该条信息是属于本人所说还是对方所说 
        // 如果是自己说的,则显示在右边;如果是对方所说,则显示在左边 
        rl_buttom.addRule(RelativeLayout.ALIGN_PARENT_TOP); 
 
        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 
            .getHeadImage())); // 设置头像 
      } else { 
        image.setImageResource(R.drawable.im); 
      } 
      String str = msg.getMessage(); // 消息具体内容 
      try { 
        SpannableString spannableString = ExpressionUtil 
            .getExpressionString(context, str, CommonUtils.PATTERN); 
        text.setText(spannableString); 
      } catch (Exception e) { 
        e.printStackTrace(); 
      } 
    } 
  } 
 
} 

以上就是关于Android中编写简单的聊天应用程序,由此可见,要开发出优质的项目是有一定的难度的,所以学习是必要的,爱站技术频道就是不错的选择。

上一篇:Android开发中实现轮播图片的效果展现

下一篇:Android开发中页面数据的操作技巧

您可能感兴趣的文章

相关阅读

热门软件源码

最新软件源码下载