当前位置:操作系统 > 安卓/Android >>

Android 实现聊天界面


代码:
 
ChatEntity.java类
 
 
[java]  
package com.ericssonlabs.chatdemo;  
  
public class ChatEntity {  
  
    private int userImage;  
    private String content;  
    private String chatTime;  
    private boolean isComeMsg;  
  
    public int getUserImage() {  
        return userImage;  
    }  
    public void setUserImage(int userImage) {  
        this.userImage = userImage;  
    }  
    public String getContent() {  
        return content;  
    }  
    public void setContent(String content) {  
        this.content = content;  
    }  
    public String getChatTime() {  
        return chatTime;  
    }  
    public void setChatTime(String chatTime) {  
        this.chatTime = chatTime;  
    }  
    public boolean isComeMsg() {  
        return isComeMsg;  
    }  
    public void setComeMsg(boolean isComeMsg) {  
        this.isComeMsg = isComeMsg;  
    }  
      
}  
 
ChatDemoActivity.java
 
[java] 
package com.ericssonlabs.chatdemo;  
  
import java.util.ArrayList;  
import java.util.List;  
  
import android.app.Activity;  
import android.content.Context;  
import android.os.Bundle;  
import android.view.LayoutInflater;  
import android.view.View;  
import android.view.View.OnClickListener;  
import android.view.ViewGroup;  
import android.view.Window;  
import android.widget.BaseAdapter;  
import android.widget.Button;  
import android.widget.EditText;  
import android.widget.ImageView;  
import android.widget.ListView;  
import android.widget.TextView;  
import android.widget.Toast;  
  
public class ChatDemoActivity extends Activity {  
    private Button sendButton = null,button1,button2;  
    private EditText contentEditText = null;  
    private ListView chatListView = null;  
    private List<ChatEntity> chatList = null;  
    private ChatAdapter chatAdapter = null;  
    boolean jianghua;  
      
    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        requestWindowFeature(Window.FEATURE_NO_TITLE);  
        setContentView(R.layout.main);  
          
        contentEditText = (EditText) this.findViewById(R.id.et_content);  
        sendButton = (Button) this.findViewById(R.id.btn_send);  
        button1 = (Button) findViewById(R.id.button1);  
        button2 = (Button) findViewById(R.id.button2);  
        chatListView = (ListView) this.findViewById(R.id.listview);  
          
        button1.setOnClickListener(new OnClickListener() {  
              
            @Override  
            public void onClick(View v) {  
                if (!jianghua) {  
                    // TODO Auto-generated method stub  
                    button2.setVisibility(View.VISIBLE);  
                }else {  
                    button2.setVisibility(View.INVISIBLE);  
                }  
            }  
        });  
          
        button2.setOnClickListener(new OnClickListener() {  
              
            @Override  
            public void onClick(View v) {  
                // TODO Auto-generated method stub  
                  
            }  
        });  
          
          
        chatList = new ArrayList<ChatEntity>();  
        ChatEntity chatEntity = null;  
        for (int i = 0; i < 2; i++) {  
            chatEntity = new ChatEntity();  
            if (i % 2 == 0) {  
                chatEntity.setComeMsg(false);  
                chatEntity.setContent("Hello");  
                chatEntity.setChatTime("2012-09-20 15:12:32");  
            }else {  
                chatEntity.setComeMsg(true);  
                chatEntity.setContent("Hello,nice to meet you!");  
                chatEntity.setChatTime("2012-09-20 15:13:32");  
            }  
            chatList.add(chatEntity);  
        }  
          
        chatAdapter = new ChatAdapter(this,chatList);  
        chatListView.setAdapter(chatAd
补充:移动开发 , Android ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,