Android-对话式聊天效果实现


使用Android的短信息软件如有米短信,微信等,都有对话式的聊天效果,个人感觉挺好的,现在简单模仿实现下。

效果如下:

为了实现这种效果,需要弄两个不同的xml布局文件

我:list_say_me_item.xml:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout  
  3.   xmlns:android="http://schemas.android.com/apk/res/android"  
  4.   android:layout_width="fill_parent"  
  5.   android:layout_height="wrap_content"  
  6.   android:orientation="horizontal"  
  7.   >  
  8.   <ImageView  
  9.       android:layout_width="42px"  
  10.       android:layout_height="42px"  
  11.       android:layout_gravity="bottom"  
  12.       android:id="@+id/messagegedetail_rov_icon"  
  13.       android:background="@drawable/image1"  
  14.   />  
  15.   <LinearLayout  
  16.       android:orientation="vertical"  
  17.       android:layout_width="249dp"  
  18.       android:layout_height="wrap_content"  
  19.       android:background="@drawable/incoming"  
  20.       android:layout_marginLeft="5dp"  
  21.   >      
  22.       <LinearLayout  
  23.           android:orientation="horizontal"  
  24.           android:layout_width="fill_parent"  
  25.           android:layout_height="22dip"  
  26.       >  
  27.           <TextView  
  28.               android:id="@+id/messagedetail_row_name"  
  29.               android:layout_width="wrap_content"  
  30.               android:layout_height="wrap_content"  
  31.               android:textColor="#000000"  
  32.               android:paddingTop="2px"  
  33.               android:textSize="16dip"  
  34.           />  
  35.           <TextView  
  36.               android:id="@+id/messagedetail_row_date"  
  37.               android:layout_width="wrap_content"  
  38.               android:layout_height="wrap_content"  
  39.               android:textColor="#000000"  
  40.               android:paddingTop="2px"  
  41.               android:textSize="16dip"  
  42.               android:layout_marginLeft="60dip"  
  43.           />  
  44.           </LinearLayout>  
  45.       <TextView  
  46.           android:id="@+id/messagedetail_row_text"  
  47.           android:layout_width="fill_parent"  
  48.           android:layout_height="wrap_content"  
  49.           android:paddingLeft="2px"  
  50.           android:textColor="#0000DD"  
  51.           android:textSize="16dip"  
  52.       />  
  53.   </LinearLayout>  
  54. </LinearLayout>  
对方:list_say_he_item.xml:
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout  
  3.   xmlns:android="http://schemas.android.com/apk/res/android"  
  4.   android:layout_width="fill_parent"  
  5.   android:layout_height="wrap_content"  
  6.   android:orientation="horizontal"  
  7.   android:layout_marginLeft="10px"  
  8.   >  
  9.   <LinearLayout  
  10.       android:orientation="vertical"  
  11.       android:layout_width="249px"  
  12.       android:layout_height="wrap_content"  
  13.       android:background="@drawable/outgoing"  
  14.       android:layout_marginLeft="25px"  
  15.   >      
  16.       <LinearLayout  
  17.           android:orientation="horizontal"  
  18.           android:layout_width="fill_parent"  
  19.           android:layout_height="22dip"  
  20.       >  
  21.           <TextView  
  22.               android:id="@+id/messagedetail_row_name"  
  23.               android:layout_width="wrap_content"  
  24.               android:layout_height="wrap_content"  
  25.               android:textColor="#000000"  
  26.               android:paddingTop="2px"  
  27.               android:textSize="16dip"  
  28.           />  
  29.           <TextView  
  30.               android:id="@+id/messagedetail_row_date"  
  31.               android:layout_width="wrap_content"  
  32.               android:layout_height="wrap_content"  
  33.               android:textColor="#000000"  
  34.               android:paddingTop="2px"  
  35.               android:textSize="16dip"  
  36.               android:layout_marginLeft="60dip"  
  37.           />  
  38.           </LinearLayout>  
  39.       <TextView  
  40.           android:id="@+id/messagedetail_row_text"  
  41.           android:layout_width="fill_parent"  
  42.           android:layout_height="wrap_content"  
  43.           android:paddingLeft="2px"  
  44.           android:textColor="#0000DD"  
  45.           android:textSize="16dip"  
  46.       />  
  47.   </LinearLayout>  
  48.   <ImageView  
  49.       android:layout_width="42px"  
  50.       android:layout_height="42px"  
  51.       android:layout_gravity="bottom"  
  52.       android:id="@+id/messagegedetail_rov_icon"  
  53.       android:background="@drawable/image2"  
  54.   />  
  55. </LinearLayout>  

主Activity文件:

  1. import java.text.SimpleDateFormat;  
  2. import java.util.ArrayList;  
  3. import java.util.Date;  
  4.   
  5. import android.app.Activity;  
  6. import android.os.Bundle;  
  7. import android.view.View;  
  8. import android.view.View.OnClickListener;  
  9. import android.widget.Button;  
  10. import android.widget.EditText;  
  11. import android.widget.ListView;  
  12.   
  13. public class ChatActivity extends Activity {  
  14.     private  ListView  talkView;  
  15.     private  Button  messageButton;  
  16.     private  EditText  messageText;  
  17.     private  ArrayList<ChatMsg>  list = new ArrayList<ChatMsg>();  
  18.     @Override  
  19.     public void onCreate(Bundle savedInstanceState) {  
  20.         super.onCreate(savedInstanceState);  
  21.         setContentView(R.layout.main);  
  22.         System.out.println("AAAAAAAAAA");  
  23.         init();  
  24.     }  
  25.     private void init(){  
  26.           talkView = (ListView) findViewById(R.id.list);  
  27.           messageButton = (Button) findViewById(R.id.MessageButton);  
  28.           messageText = (EditText) findViewById(R.id.MessageText);  
  29.           messageButton.setOnClickListener(new OnClickListener() {  
  30.               
  31.             @Override  
  32.             public void onClick(View v) {  
  33.                 // TODO Auto-generated method stub   
  34.             String  name = getName(R.string.myDisplayName);  
  35.             String  date  =getDate();  
  36.             String  msgText  =getText();  
  37.             int RIdA = R.layout.list_say_me_item;  
  38.             ChatMsg   newMsg = new ChatMsg(name,date,msgText,RIdA);  
  39.             list.add(newMsg);  
  40.             int RIdB = R.layout.list_say_he_item;  
  41.             String  othername = getName(R.string.otherDisplayName);  
  42.             ChatMsg   backMsg = new ChatMsg(othername,date,"自动回复(for test!)",RIdB);  
  43.             list.add(backMsg);  
  44.             talkView.setAdapter(new ChatMsgViewAdapter(ChatActivity.this,list));  
  45.             messageText.setText("");  
  46.             }  
  47.         }) ;   
  48.     }  
  49.     private  String getName(int id){  
  50.         return getResources().getString(id);  
  51.     }  
  52.     private   String  getDate(){  
  53.         SimpleDateFormat  sdf  =new SimpleDateFormat("MM-dd HH:mm");  
  54.         Date  d = new Date();  
  55.         return   sdf.format(d);  
  56.     }  
  57.     private  String  getText(){  
  58.         return messageText.getText().toString();  
  59.     }  
  60.     @Override  
  61.     protected void onDestroy() {  
  62.         // TODO Auto-generated method stub   
  63.         super.onDestroy();  
  64.     }  
  65. }  
  • 1
  • 2
  • 下一页

相关内容