Android-对话式聊天效果实现
Android-对话式聊天效果实现
使用Android的短信息软件如有米短信,微信等,都有对话式的聊天效果,个人感觉挺好的,现在简单模仿实现下。
效果如下:
为了实现这种效果,需要弄两个不同的xml布局文件
我:list_say_me_item.xml:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:orientation="horizontal"
- >
- <ImageView
- android:layout_width="42px"
- android:layout_height="42px"
- android:layout_gravity="bottom"
- android:id="@+id/messagegedetail_rov_icon"
- android:background="@drawable/image1"
- />
- <LinearLayout
- android:orientation="vertical"
- android:layout_width="249dp"
- android:layout_height="wrap_content"
- android:background="@drawable/incoming"
- android:layout_marginLeft="5dp"
- >
- <LinearLayout
- android:orientation="horizontal"
- android:layout_width="fill_parent"
- android:layout_height="22dip"
- >
- <TextView
- android:id="@+id/messagedetail_row_name"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textColor="#000000"
- android:paddingTop="2px"
- android:textSize="16dip"
- />
- <TextView
- android:id="@+id/messagedetail_row_date"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textColor="#000000"
- android:paddingTop="2px"
- android:textSize="16dip"
- android:layout_marginLeft="60dip"
- />
- </LinearLayout>
- <TextView
- android:id="@+id/messagedetail_row_text"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:paddingLeft="2px"
- android:textColor="#0000DD"
- android:textSize="16dip"
- />
- </LinearLayout>
- </LinearLayout>
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:orientation="horizontal"
- android:layout_marginLeft="10px"
- >
- <LinearLayout
- android:orientation="vertical"
- android:layout_width="249px"
- android:layout_height="wrap_content"
- android:background="@drawable/outgoing"
- android:layout_marginLeft="25px"
- >
- <LinearLayout
- android:orientation="horizontal"
- android:layout_width="fill_parent"
- android:layout_height="22dip"
- >
- <TextView
- android:id="@+id/messagedetail_row_name"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textColor="#000000"
- android:paddingTop="2px"
- android:textSize="16dip"
- />
- <TextView
- android:id="@+id/messagedetail_row_date"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textColor="#000000"
- android:paddingTop="2px"
- android:textSize="16dip"
- android:layout_marginLeft="60dip"
- />
- </LinearLayout>
- <TextView
- android:id="@+id/messagedetail_row_text"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:paddingLeft="2px"
- android:textColor="#0000DD"
- android:textSize="16dip"
- />
- </LinearLayout>
- <ImageView
- android:layout_width="42px"
- android:layout_height="42px"
- android:layout_gravity="bottom"
- android:id="@+id/messagegedetail_rov_icon"
- android:background="@drawable/image2"
- />
- </LinearLayout>
主Activity文件:
- import java.text.SimpleDateFormat;
- import java.util.ArrayList;
- import java.util.Date;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.ListView;
- public class ChatActivity extends Activity {
- private ListView talkView;
- private Button messageButton;
- private EditText messageText;
- private ArrayList<ChatMsg> list = new ArrayList<ChatMsg>();
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- System.out.println("AAAAAAAAAA");
- init();
- }
- private void init(){
- talkView = (ListView) findViewById(R.id.list);
- messageButton = (Button) findViewById(R.id.MessageButton);
- messageText = (EditText) findViewById(R.id.MessageText);
- messageButton.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- String name = getName(R.string.myDisplayName);
- String date =getDate();
- String msgText =getText();
- int RIdA = R.layout.list_say_me_item;
- ChatMsg newMsg = new ChatMsg(name,date,msgText,RIdA);
- list.add(newMsg);
- int RIdB = R.layout.list_say_he_item;
- String othername = getName(R.string.otherDisplayName);
- ChatMsg backMsg = new ChatMsg(othername,date,"自动回复(for test!)",RIdB);
- list.add(backMsg);
- talkView.setAdapter(new ChatMsgViewAdapter(ChatActivity.this,list));
- messageText.setText("");
- }
- }) ;
- }
- private String getName(int id){
- return getResources().getString(id);
- }
- private String getDate(){
- SimpleDateFormat sdf =new SimpleDateFormat("MM-dd HH:mm");
- Date d = new Date();
- return sdf.format(d);
- }
- private String getText(){
- return messageText.getText().toString();
- }
- @Override
- protected void onDestroy() {
- // TODO Auto-generated method stub
- super.onDestroy();
- }
- }
|
评论暂时关闭