package com.partynetwork.iparty.imessage;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import com.partynetwork.iparty.R;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class ImessageChatActivity extends Activity {
private Context mContext;
private ListView chatList;
private TextView nameText;
private LinearLayout backLayout;
private LinearLayout inputWayLayout;
private LinearLayout inputByLayout;
private ImessageChatListAdapter messageAdapter;
// 输入的两种方式
// 一:语音输入
// 二:文字输入
private ImageButton imessageChatImageButton;
private EditText imessageChatEditText;
// true:文字输入 false:语音输入
private boolean inputWay = false;
// 按下语音按钮后出现的界面视图
private View popupWindowView;
private RelativeLayout middleLayout;
//录音中间显示的图片控件
private ImageView popupWindowImage;
private List<MessageEntity> mMessageList = new ArrayList<MessageEntity>();
private String[] msgArray = new String[] { "你好!", "您好!", "我是杭州的哦,你呢?",
"我也有,我刚过来上学。", "你要租房子吗?我这里有一个一室一厅,要是你租的话可以便宜点租给你哦……", "好啊,什么位置啊。",
"XXX大街XX号……", "可以", "过来看房的时候打我电话13888888888" };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.imessage_chat);
mContext = getApplicationContext();
finfViewById();
init();
setListener();
}
private void finfViewById() {
nameText = (TextView) findViewById(R.id.imessage_chat_top_name);
chatList = (ListView) this.findViewById(R.id.imessage_chat_list);
backLayout = (LinearLayout) findViewById(R.id.imessage_chat_top_back);
inputWayLayout = (LinearLayout) findViewById(R.id.imessage_chat_foot_input_way);
inputByLayout = (LinearLayout) findViewById(R.id.imessage_chat_foot_input_by);
middleLayout = (RelativeLayout) findViewById(R.id.imessage_chat_middle);
}
private void init() {
LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
imessageChatImageButton = new ImageButton(mContext);
imessageChatImageButton.setLayoutParams(params);
imessageChatImageButton
.setBackgroundResource(R.drawable.imessage_chat_foot_speak_unselected);
imessageChatEditText = new EditText(mContext);
imessageChatEditText.setLayoutParams(params);
popupWindowView = LayoutInflater.from(mContext).inflate(
R.layout.imessage_chat_popupwindow, null);
popupWindowImage = (ImageView) popupWindowView
.findViewById(R.id.imessage_chat_popupwindow_image);
// 默认为语音发送
inputByLayout.removeAllViews();
inputByLayout.addView(imessageChatImageButton);
// 设置聊天对象姓名
String name = getIntent().getStringExtra("name");
nameText.setText(name);
// 处理聊天内容
for (int i = 0; i < msgArray.length; i++) {
MessageEntity entity = new MessageEntity();
if (i % 2 == 0) {
entity.setToUserId(007);
entity.setFromUserId(100);
} else {
entity.setToUserId(100);
entity.setFromUserId(007);
}
entity.setDate(getDate());
entity.setContent(msgArray[i]);
mMessageList.add(entity);
}
// 显示聊天内容
messageAdapter = new ImessageChatListAdapter(this, mMessageList);
chatList.setAdapter(messageAdapter);
}
private void setListener() {
// 退出按钮
backLayout.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
finish();
}
});
// 输入方式切换
inputWayLayout.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
inputByLayout.removeAllViews();
if (inputWay) {
inputByLayout.addView(imessageChatImageButton);
} else {
inputByLayout.addView(imessageChatEditText);
imessageChatEditText.findFocus();
}
inputWay = !inputWay;
}
});
// 文字输入方式发送监听
imessageChatEditText.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View arg0, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_ENTER) {
sendMessage();
}
return true;
}
});
// 语音发送
imessageChatImageButton.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
middleLayout.addView(popupWindowView);
imessageChatImageButton
.setBackgroundResource(R.drawable.imessage_chat_foot_speak_selected);
} else if (event.getAction() == MotionEvent.ACTION_UP) {
middleLayout.removeView(popupWindowView);
imessageChatImageButton
.setBackgroundResource(R.drawable.imessage_chat_foot_speak_unselected);
if (event.getY() > 0) {
sendSound();
}
} else if (event.getAction() == MotionEvent.ACTION_MOVE) {
if (event.getY() > 0) {
popupWindowImage
.setImageResource(R.drawable.imessage_chat_popupwindow_normal);
} else {
popupWindowImage
.setImageResource(R.drawable.imessage_chat_popupwindow_cancel);
}
}
return true;
}
});
}
/**
* 发送文字信息
*/
private void sendMessage() {
String contString = imessageChatEditText.getText().toString();
if (contString.length() > 0) {
MessageEntity entity = new MessageEntity();
entity.setFromUserId(007);
entity.setToUserId(100);
entity.setDate(getDate());
entity.setType(0);
entity.setContent(contString);
mMessageList.add(entity);
messageAdapter.notifyDataSetChanged();
imessageChatEditText.setText("");
chatList.setSelection(chatList.getCount() - 1);
}
}
private void sendSound() {
MessageEntity entity = new MessageEntity();
entity.setFromUserId(007);
entity.setToUserId(100);
entity.setDate(getDate());
entity.setType(1);
mMessageList.add(entity);
messageAdapter.notifyDataSetChanged();
chatList.setSelection(chatList.getCount() - 1);
}
/**
* 获取当前的时间
*
* @return
*/
private String getDate() {
Calendar c = Calendar.getInstance();
String year = String.valueOf(c.get(Calendar.YEAR));
String month = String.valueOf(c.get(Calendar.MONTH));
String day = String.valueOf(c.get(Calendar.DAY_OF_MONTH) + 1);
String hour = String.valueOf(c.get(Calendar.HOUR_OF_DAY));
String mins = String.valueOf(c.get(Calendar.MINUTE));
StringBuffer sbBuffer = new StringBuffer();
sbBuffer.append(year + "-" + month + "-" + day + " " + hour + ":"
+ mins);
return sbBuffer.toString();
}
}