package com.michael.chatclient;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOExcep
tion;
import java.net.Socket;
import java.sql.Date;
import java.text.SimpleDateFormat;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity implements Runnable{
private EditText usernameEdit;
private EditText ipEdit;
private EditText historyEdit;
private EditText messageEdit;
private Button loginButton;
private Button sendButton;
private Button leaveButton;
private String username,ip,chat_txt,chat_in;
public static final int PORT = 8888;
Socket socket;
Thread thread;
DataInputStream in;
DataOutputStream out;
boolean flag = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
usernameEdit = (EditText) findViewById(R.id.username1);
ipEdit = (EditText) findViewById(R.id.ip);
historyEdit = (EditText) findViewById(R.id.history);
messageEdit = (EditText) findViewById(R.id.message);
loginButton = (Button) findViewById(R.id.LoginButton);
leaveButton = (Button) findViewById(R.id.LeaveButton);
sendButton = (Button) findViewById(R.id.SendButton);
//绑定监听器
loginButton.setOnClickListener(listener);
leaveButton.setOnClickListener(listener);
sendButton.setOnClickListener(listener);
}
View.OnClickListener listener = new View.OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
switch (arg0.getId()){ //进入聊天室按钮的处理
case R.id.LoginButton:
if(flag == true){
Toast.makeText(MainActivity.this,""
+ "已经登陆过了!",Toast.LENGTH_SHORT).show();
return;
}
username = usernameEdit.getText().toString();
ip = ipEdit.getText().toString();
if(username != "" && username != null
&& username != "用户名输入" && ip != null){
try{
socket = new Socket(ip,PORT);
in = new DataInputStream(socket.getInputStream());
out = new DataOutputStream(socket.getOutputStream());
Date now = new Date(System.currentTimeMillis());
SimpleDateFormat format = new SimpleDateFormat
("hh:mm:ss");
String nowStr = format.format(now);
out.writeUTF("$$" + username + " " + nowStr
+ "上线了!");
}catch(IOException e1){
System.out.println("can't connect");
}
thread = new Thread(MainActivity.this);
thread.start();
flag = true;
}
break;
case R.id.SendButton:
if(flag == false){
Toast.makeText(MainActivity.this,""
+ "没有登录,请登录",Toast.LENGTH_SHORT).show();
return;
}
chat_txt = messageEdit.getText().toString();
if(chat_txt != null){
Date now = new Date(System.currentTimeMillis());
SimpleDateFormat format = new SimpleDateFormat
("hh:mm:ss");
String nowStr = format.format(now);
try{ out.writeUTF("^_^" + username + " " + nowStr +
"说/n" + chat_txt);}
catch(IOException e2){}
}
else
{
try{ out.writeUTF("请说话");}
catch(IOException e3){}
}
break;
case R.id.LeaveButton:
if(flag == true){
if(flag != false){
Toast.makeText(MainActivity.this,""
+ "没有登录,请登录!",Toast.LENGTH_SHORT).show();
return;
}
try{
out.writeUTF("==" + username + "下线了!");
out.close();
in.close();
socket.close();
}catch(IOException e4){}
}
flag = false;
Toast.makeText(MainActivity.this,""
+ "已退出!",Toast.LENGTH_SHORT).show();
break;
}
}
};
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void run() {
// TODO Auto-generated method stub
while(true){
try{
chat_in = in.readUTF();
chat_in = chat_in + "/n";
mHandler.sendMessage(mHandler.obtainMessage());
}catch(IOException e){}
}
}
Handler mHandler = new Handler(){
public void handleMessage(Message msg){
historyEdit.append(chat_in);
super.handleMessage(msg);
}
};
}