歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android-對話式聊天效果實現

Android-對話式聊天效果實現

日期:2017/3/1 10:55:39   编辑:Linux編程

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