源代码:
package com.example.last;import java.io.BufferedWriter;
import java.io.DataOutputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;public class input extends Activity {
private ImageButton send1=null;
private ImageButton back1=null;
private EditText text=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.input);
this.send1=(ImageButton)super.findViewById(R.id.send1);
this.back1=(ImageButton)super.findViewById(R.id.back);
this.text=(EditText)super.findViewById(R.id.text);
this.send1.setOnClickListener(new OnClickListenerlmppp());
this.back1.setOnClickListener(new OnClickListenerlmpppp());
}
private class OnClickListenerlmppp implements OnClickListener{
public void onClick(View v)
{
String message = input.this.text.getText().toString();
Socket socket;
try {
String s1 = "#";
String mes =s1+message+s1;
//String mess=getAscii(mes); //转换为ascii码
socket= new Socket("192.168.0.100", 10000);
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true);
out.print(mes.getBytes()); //转换为字节,并发出
out.close();
}catch (Exception e){
e.printStackTrace();
}
//这里就是发送的代码
}
}
private class OnClickListenerlmpppp implements OnClickListener{
public void onClick(View v)
{
finish();
}
}
/*public static String getAscii(String str) {
String tmp;
StringBuffer sb = new StringBuffer(1000);
char c;
int i, j;
sb.setLength(0);
for (i = 0; i < str.length(); i++) {
c = str.charAt(i);
if (c > 255) {
sb.append("//u");
j = (c >>> 8);
tmp = Integer.toHexString(j);
if (tmp.length() == 1)
sb.append("0");
sb.append(tmp);
j = (c & 0xFF);
tmp = Integer.toHexString(j);
if (tmp.length() == 1)
sb.append("0");
sb.append(tmp);
} else {
sb.append(c);
}
}
return (new String(sb));
} */
@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;
}
}
Xml文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/home"> <RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.85" > <EditText
android:id="@+id/text"
android:layout_width="407px"
android:layout_height="218px"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="108px"
android:text="我的名字叫小魔方"
android:ems="10"
android:inputType="textMultiLine"
android:gravity="left|top"
android:minLines="6" /> <ImageButton
android:id="@+id/send1"
android:layout_width="407px"
android:layout_height="67px"
android:background="#00ffffff"
android:layout_marginTop="388px"
android:layout_marginLeft="38px"
android:layout_marginRight="38px"
android:layout_centerHorizontal="true"
android:src="@drawable/send051" />
<ImageButton
android:id="@+id/back"
android:layout_width="407px"
android:layout_height="42px"
android:background="#00ffffff"
android:layout_marginTop="497px"
android:layout_marginLeft="38px"
android:layout_marginRight="38px"
android:layout_centerHorizontal="true"
android:src="@drawable/send111" /> </RelativeLayout></LinearLayout>Android通信PCsocketTCP
package com.example.last;import java.io.BufferedWriter;
import java.io.DataOutputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;public class input extends Activity {
private ImageButton send1=null;
private ImageButton back1=null;
private EditText text=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.input);
this.send1=(ImageButton)super.findViewById(R.id.send1);
this.back1=(ImageButton)super.findViewById(R.id.back);
this.text=(EditText)super.findViewById(R.id.text);
this.send1.setOnClickListener(new OnClickListenerlmppp());
this.back1.setOnClickListener(new OnClickListenerlmpppp());
}
private class OnClickListenerlmppp implements OnClickListener{
public void onClick(View v)
{
String message = input.this.text.getText().toString();
Socket socket;
try {
String s1 = "#";
String mes =s1+message+s1;
//String mess=getAscii(mes); //转换为ascii码
socket= new Socket("192.168.0.100", 10000);
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true);
out.print(mes.getBytes()); //转换为字节,并发出
out.close();
}catch (Exception e){
e.printStackTrace();
}
//这里就是发送的代码
}
}
private class OnClickListenerlmpppp implements OnClickListener{
public void onClick(View v)
{
finish();
}
}
/*public static String getAscii(String str) {
String tmp;
StringBuffer sb = new StringBuffer(1000);
char c;
int i, j;
sb.setLength(0);
for (i = 0; i < str.length(); i++) {
c = str.charAt(i);
if (c > 255) {
sb.append("//u");
j = (c >>> 8);
tmp = Integer.toHexString(j);
if (tmp.length() == 1)
sb.append("0");
sb.append(tmp);
j = (c & 0xFF);
tmp = Integer.toHexString(j);
if (tmp.length() == 1)
sb.append("0");
sb.append(tmp);
} else {
sb.append(c);
}
}
return (new String(sb));
} */
@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;
}
}
Xml文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/home"> <RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.85" > <EditText
android:id="@+id/text"
android:layout_width="407px"
android:layout_height="218px"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="108px"
android:text="我的名字叫小魔方"
android:ems="10"
android:inputType="textMultiLine"
android:gravity="left|top"
android:minLines="6" /> <ImageButton
android:id="@+id/send1"
android:layout_width="407px"
android:layout_height="67px"
android:background="#00ffffff"
android:layout_marginTop="388px"
android:layout_marginLeft="38px"
android:layout_marginRight="38px"
android:layout_centerHorizontal="true"
android:src="@drawable/send051" />
<ImageButton
android:id="@+id/back"
android:layout_width="407px"
android:layout_height="42px"
android:background="#00ffffff"
android:layout_marginTop="497px"
android:layout_marginLeft="38px"
android:layout_marginRight="38px"
android:layout_centerHorizontal="true"
android:src="@drawable/send111" /> </RelativeLayout></LinearLayout>Android通信PCsocketTCP
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货