完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
package com.michael.chatclient;
import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; 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); } }; } |
|
相关推荐
|
|
只有小组成员才能发言,加入小组>>
物联网工程师必备:怎么选择不同的无线连接技术,本指南帮你忙!
3262 浏览 1 评论
【DFRobot TinkerNode NB-IoT 物联网开发板试用连载】WIFI功能测试
3911 浏览 0 评论
【DFRobot TinkerNode NB-IoT 物联网开发板试用连载】Arduino的替代SublimeText3+STino
3419 浏览 0 评论
使用端口扩展器轻松高效地向IIoT端点添加具有成本效益的子节点
3968 浏览 1 评论
20608 浏览 11 评论
模组有时候复位重启后输出日志为“REBOOT_CAUSE_SECURITY_PMU_POWER_ON_RESET”的原因?
751浏览 2评论
939浏览 2评论
966浏览 1评论
1086浏览 1评论
362浏览 1评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-11-29 02:05 , Processed in 0.851840 second(s), Total 71, Slave 54 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号