先不多直接看软件截图
这个是首页等陆界面
xml代码如下
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/port" tools:context=".MainActivity" android:orientation="vertical"> <LinearLayout android:layout_width="wrap_content" android:layout_height="60dp" android:layout_marginTop="20dp" android:gravity="center"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/jxlkjlog" /> <TextView android:layout_width="wrap_content" android:layout_height="match_parent" android:gravity="center" android:text="@string/welcome" android:textColor="#000000" android:textSize="20.5sp" android:textStyle="bold" /> </LinearLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="70dp" android:layout_marginStart="50dp" android:layout_marginTop="200dp" android:layout_marginEnd="50dp" > <TextView android:id="@+id/text_username" android:layout_width="wrap_content" android:layout_height="60dp" android:text="@string/username" android:textColor="#000000" android:textSize="25sp" android:textStyle="bold"> </TextView> <EditText android:id="@+id/Edit_username" android:layout_width="match_parent" android:layout_height="50dp" android:layout_toEndOf="@id/text_username" android:hint="用户名不少于三位" android:inputType="text" android:autofillHints=""></EditText> </RelativeLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="70dp" android:layout_marginStart="50dp" android:layout_marginEnd="50dp"> <TextView android:id="@+id/text_passwold" android:layout_width="wrap_content" android:layout_height="60dp" android:text="@string/passwold" android:textColor="#000000" android:textSize="25sp" android:textStyle="bold"> </TextView> <EditText android:id="@+id/Edit_passwold" android:layout_width="match_parent" android:layout_height="50dp" android:layout_toEndOf="@id/text_passwold" android:inputType="textPassword" android:hint="密码不少于四位" ></EditText> </RelativeLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center"> <Button android:id="@+id/btn_login" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/login" tools:ignore="ButtonStyle"> </Button> <Button android:id="@+id/btn_enroll" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/enroll" tools:ignore="ButtonStyle"> </Button> </LinearLayout> </LinearLayout>
MainActivity文件
public class MainActivity extends AppCompatActivity { private Button mButtonLongin; private Button mButtonEnroll; private EditText mEditTextUsername; private EditText mEditTextPassword; private String username; private String password; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mButtonLongin=findViewById(R.id.btn_login); mButtonEnroll=findViewById(R.id.btn_enroll); mEditTextUsername=findViewById(R.id.Edit_username); mEditTextPassword=findViewById(R.id.Edit_passwold); UserDao userDao=new UserDao(); mButtonEnroll.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent=new Intent(MainActivity.this,MainActivity3.class); startActivity(intent); } }); mButtonLongin.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { EditText editTextusername=findViewById(R.id.Edit_username); EditText editTextpassword=findViewById(R.id.Edit_passwold); if (editTextusername.length() < 3) { // Toast.makeText(MainActivity.this, "用户名不能小于三位", Toast.LENGTH_LONG).show(); CommonUtils.showLontMsg(MainActivity.this, "用户名不能小于三位"); } else if (editTextpassword.length() < 4) { CommonUtils.showLontMsg(MainActivity.this, "密码不能小于四位"); }else { new Thread() { public void run() { boolean aa= userDao.longin(editTextusername.getText().toString(),editTextpassword.getText().toString()); if(aa==true){ Intent intent=new Intent(MainActivity.this,MainActivity2.class); startActivity(intent); }else{ Looper.prepare(); CommonUtils.showDlgMsg(MainActivity.this, "账号密码不正确"); Looper.loop(); } } }.start(); } } }); } 以下是注册界面
注册界面的xlm文件 这个界面比较简单就不介绍了
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity3"> <LinearLayout android:layout_width="wrap_content" android:layout_height="60dp" android:layout_marginTop="20dp" android:layout_gravity="center" android:gravity="center" > <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/jxlkjlog" /> <TextView android:layout_width="wrap_content" android:layout_height="match_parent" android:gravity="center" android:text="@string/welcome" android:textColor="#000000" android:textSize="20.5sp" android:textStyle="bold" /> </LinearLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="70dp" android:layout_marginStart="50dp" android:layout_marginTop="200dp" android:layout_marginEnd="50dp" android:layout_margin="5dp" > <TextView android:id="@+id/text_username" android:layout_width="wrap_content" android:layout_height="50dp" android:text="@string/username" android:textColor="#000000" android:textSize="25sp" android:textStyle="bold" > </TextView> <EditText android:id="@+id/Edit_username" android:layout_width="match_parent" android:layout_height="50dp" android:layout_toEndOf="@id/text_username" android:hint="用户名不少于三位" android:inputType="text" android:autofillHints=""></EditText> </RelativeLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="70dp" android:layout_marginStart="50dp" android:layout_marginEnd="50dp" android:layout_margin="5dp" > <TextView android:id="@+id/text_passwold" android:layout_width="wrap_content" android:layout_height="50dp" android:text="@string/passwold" android:textColor="#000000" android:textSize="25sp" android:textStyle="bold"> </TextView> <EditText android:id="@+id/Edit_passwold" android:layout_width="match_parent" android:layout_height="50dp" android:layout_toEndOf="@id/text_passwold" android:inputType="textPassword" android:hint="密码不少于四位" ></EditText> </RelativeLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="70dp" android:layout_marginStart="50dp" android:layout_marginEnd="50dp" android:layout_margin="5dp"> <TextView android:id="@+id/text_name" android:layout_width="wrap_content" android:layout_height="50dp" android:text="@string/name" android:textColor="#000000" android:textSize="25sp" android:textStyle="bold" > </TextView> <EditText android:id="@+id/Edit_name" android:layout_width="match_parent" android:layout_height="50dp" android:layout_toEndOf="@id/text_name" android:inputType="text" android:hint="请输入姓名" ></EditText> </RelativeLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="70dp" android:layout_marginStart="50dp" android:layout_margin="5dp" android:layout_marginEnd="50dp"> <TextView android:id="@+id/text_idcard" android:layout_width="wrap_content" android:layout_height="50dp" android:text="@string/idcard" android:textColor="#000000" android:textSize="25sp" android:textStyle="bold" > </TextView> <EditText android:id="@+id/Edit_idcard" android:layout_width="match_parent" android:layout_height="50dp" android:layout_toEndOf="@id/text_idcard" android:inputType="text" android:hint="输入身份证号码" ></EditText> </RelativeLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="70dp" android:layout_marginStart="50dp" android:layout_margin="5dp" android:layout_marginEnd="50dp"> <TextView android:id="@+id/text_phonenumber" android:layout_width="wrap_content" android:layout_height="50dp" android:text="@string/phonenumber" android:textColor="#000000" android:textSize="25sp" android:textStyle="bold" > </TextView> <EditText android:id="@+id/Edit_phonenumber" android:layout_width="match_parent" android:layout_height="50dp" android:layout_toEndOf="@id/text_phonenumber" android:inputType="text" android:hint="请输入手机号" ></EditText> </RelativeLayout> <LinearLayout android:layout_width="280dp" android:layout_height="60dp" android:layout_gravity="center" android:layout_marginLeft="18dp"> <Button android:id="@+id/register" android:layout_width="130dp" android:layout_height="60dp" android:text="注册" tools:ignore="ButtonStyle"> </Button> <Button android:id="@+id/cancel" android:layout_width="130dp" android:layout_height="60dp" android:text="返回" tools:ignore="ButtonStyle"> </Button> </LinearLayout> </LinearLayout>
MainActivity文件
package com.example.longin_demo; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.os.Handler; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import com.example.longin_demo.tcp.ConnectThread; import com.example.longin_demo.tcp.Send; public class MainActivity2 extends AppCompatActivity { private Button mBtn_send; private Button mBtn_connect; private EditText mEt_send; private static TextView mTv_recv; private String ip; private int port; private String msg; private ConnectThread ct; private Send send; private Handler handler; static Receive re = new Receive(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); mTv_recv = findViewById(R.id.receive); mBtn_connect = findViewById(R.id.connect); mBtn_send = findViewById(R.id.btn_1); final EditText mEt_ip = this.findViewById(R.id.ip); final EditText mEt_port = this.findViewById(R.id.port); final EditText mEt_send = this.findViewById(R.id.send); /* 设置一个点击事件用以连接线程 */ mBtn_connect.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { ip = mEt_ip.getText().toString();//获取输入的ip port = Integer.parseInt(mEt_port.getText().toString());//获取输入的端口号 ct = new ConnectThread(ip, port,re);//创建一个线程来处理消息的收发 ct.start(); } }); /* 设计一个点击事件用以发送消息 */ mBtn_send.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { msg = mEt_send.getText().toString(); send = new Send(msg, port, ip); send.start(); } }); } /* 这里是和连接接收线程中的 re.setString(string) 联动的,自己理解一下怎么走在脑子里面走一次就差不多了。 */ public static class Receive{ private String string; public String getString(){ return string; } public void setString(String s){ this.string = s; } } /* 下面的callback和连接接收线程中的 MainActivity.callback() 关联,线程运行到那一步后结束回到MainActivity中。 */ public static void callback(){ System.out.println("连接线程执行结束"); mTv_recv.setText(re.getString());//这里是将接收到的文字显示在接收框内。 } }
这是一个注册失败的界面,如果数据库内有相同的账号密码则显示注册失败
话不多说直接上代码
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".FailRegister" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="50dp"> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:text="@string/no_register" android:gravity="center" android:textColor="#fff" android:background="@color/design_default_color_primary_dark" android:textSize="30sp" android:textStyle="bold" /> </LinearLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="70dp" android:layout_marginStart="50dp" android:layout_marginTop="200dp" android:layout_marginEnd="50dp" > <ImageView android:layout_width="wrap_content" android:layout_height="60dp" android:src="@drawable/cw"> </ImageView> </RelativeLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="70dp" android:layout_marginStart="50dp" android:layout_marginTop="10dp" android:layout_marginEnd="50dp" android:gravity="center" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/no_register" android:gravity="center" android:textSize="30dp" android:textStyle="bold" /> </RelativeLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="50dp" android:layout_marginTop="210dp" android:layout_marginEnd="10dp" android:gravity="center"> <Button android:id="@+id/home_back" android:layout_width="150dp" android:layout_height="50dp" android:text="@string/backtrack" tools:ignore="ButtonStyle" /> <Button android:id="@+id/go_continue" android:layout_width="150dp" android:layout_height="50dp" android:text="@string/continue_register" tools:ignore="ButtonStyle"/> </LinearLayout> </LinearLayout>
MainActivity里面的代码
package com.example.longin_demo; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; public class FailRegister extends AppCompatActivity { private Button home_back; private Button go_continue; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.fail_register); home_back=findViewById(R.id.home_back); go_continue=findViewById(R.id.go_continue); home_back.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(FailRegister.this, MainActivity.class); startActivity(intent); } }); go_continue.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(FailRegister.this, MainActivity3.class); startActivity(intent); } }); } }
这里是user类用于定义数据的参数
package com.example.longin_demo.entity; import java.util.Date; public class User { private int id; private String username; private String password; private String idcard; private String name; private Date birthdat; private String phonenumber; public User() { } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getIdcard() { return idcard; } public void setIdcard(String idcard) { this.idcard = idcard; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Date getBirthdat() { return birthdat; } public void setBirthdat(Date birthdat) { this.birthdat = birthdat; } public String getPhonenumber() { return phonenumber; } public void setPhonenumber(String phonenumber) { this.phonenumber = phonenumber; } public User(int id, String username, String password, String idcard, String name, Date birthdat, String phonenumber) { this.id = id; this.username = username; this.password = password; this.idcard = idcard; this.name = name; this.birthdat = birthdat; this.phonenumber = phonenumber; } }
dao类用来写SQL语句的
package com.example.longin_demo.dao; import android.util.Log; import androidx.annotation.Nullable; import com.example.longin_demo.entity.User; import com.example.longin_demo.untils.JDBCUtils; import java.sql.Connection; import java.sql.Date; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; public class UserDao { public boolean register(String username){ String sql="select * from user1 where username=?"; Connection con =JDBCUtils.getConn(); try{ PreparedStatement pst =con.prepareStatement(sql); pst.setString(1,username); if(pst.executeQuery().next()){ return true; } } catch (Exception e) { e.printStackTrace(); }finally { JDBCUtils.close(con); } return false; } public boolean longin(String username,String password) { String sql = "select * from user1 where username=? and password=?"; Connection con = JDBCUtils.getConn(); Log.d("UserDao", "连接数据库成功"); try { PreparedStatement pst=con.prepareStatement(sql); pst.setString(1,username); pst.setString(2,password); if (pst.executeQuery().next()){ return true; } } catch (SQLException throwables) { throwables.printStackTrace(); Log.d("UserDao", "连接数据库失败"); }finally { JDBCUtils.close(con); } return false; } public boolean register(User user){ String sql = "insert into user1(username,password,idcard,name,phonenumber) values (?,?,?,?,?)"; Connection con = JDBCUtils.getConn(); try { PreparedStatement pst=con.prepareStatement(sql); pst.setString(1,user.getUsername()); pst.setString(2,user.getPassword()); pst.setString(3,user.getIdcard()); pst.setString(4,user.getName()); pst.setString(5, user.getPhonenumber()); int value = pst.executeUpdate(); if(value>0){ return true; } } catch (SQLException throwables) { throwables.printStackTrace(); }finally { JDBCUtils.close(con); } return false; } }
JDBC连接数据库
package com.example.longin_demo.untils; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; public class JDBCUtils { static { try{Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { e.printStackTrace(); } } public static Connection getConn(){ Connection conn = null; try { conn= DriverManager.getConnection("jdbc:mysql://192.168.1.158:3306/test","root","root"); }catch (Exception exception){ exception.printStackTrace(); } return conn; } public static void close(Connection conn){ try { conn.close(); } catch (SQLException throwables) { throwables.printStackTrace(); } } }
我还定义了一个类,用来提示信息直接调用弹出提示框,以及报错,用的定义这个主要是代码复用率
package com.example.longin_demo.untils; import android.app.AlertDialog; import android.content.Context; import android.widget.Toast; import java.util.Set; public class CommonUtils { public static void showShortMsg(Context context,String msg){ Toast.makeText(context,msg,Toast.LENGTH_SHORT).show(); } public static void showLontMsg(Context context,String msg){ Toast.makeText(context,msg,Toast.LENGTH_LONG).show(); } public static void showDlgMsg(Context context,String msg){ new AlertDialog.Builder(context) .setTitle("提示信息") .setMessage(msg) .setPositiveButton("确定",null) .setNegativeButton("取消",null) .create().show(); } }
在AndroidManifest里面一定要加入这一行代码。
<uses-permission android:name="android.permission.INTERNET" />
下面是通过tcp协议传输的几个类
ConnectThread类
package com.example.longin_demo.tcp; import android.util.Log; import com.example.longin_demo.MainActivity2; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.Socket; public class ConnectThread extends Thread{ //Socket msg = null;//定义socket private OutputStream out_ip=null;//定义输出流(ip) OutputStream outputStream=null; private InputStream inputStream; private StringBuffer stringBuffer; private String ip; private int port; private Receive receive; private String string; private boolean isRun = true; private MainActivity2.Receive re; public ConnectThread(String ip, int port, MainActivity2.Receive re) { this.ip = ip; this.port = port; this.re = re; } @Override public void run(){ Socket so = null; try { so = new Socket(ip, port); } catch (IOException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName() + ": Hello"); try { inputStream = so.getInputStream(); out_ip = so.getOutputStream(); Log.v("AndroidChat","开始连接服务器:"+ip+"/"+port); sleep(1000); } catch (IOException | InterruptedException e) { Log.v("AndroidChat","连接服务器失败"+e.getMessage()); e.printStackTrace(); return; } Log.v("AndroidChat","成功连接上服务器"); /* 下面是接收模块,你可以尝试探究如何将这个模块放在接收线程中。 */ try { inputStream = so.getInputStream(); final byte[] buffer = new byte[1024]; final int len = inputStream.read(buffer); System.out.println(new String(buffer,0,len)); Log.v("AndroidChat","接收成功:"+new String(buffer,0,len)); string = new String(buffer,0,len); re.setString(string); //上下两行会和MainActivity关联,是回调在显示屏的关键步骤。 MainActivity2.callback(); System.out.println(new String(buffer,0,len)); } catch (IOException e) { e.printStackTrace(); } } }
Receive类
package com.example.longin_demo.tcp; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; import com.example.longin_demo.MainActivity2; import com.example.longin_demo.R; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.InputStream; import java.net.Socket; public class Receive extends Thread { private InputStream inputStream; private InputStreamReader inputStreamReader; private BufferedReader bufferedReader; private int port; private String ip; private String string; private MainActivity2 mainActivity; public Receive(int port, String ip) { this.port = port; this.ip = ip; } @Override public void run() { try { //sleep(1000); Socket socket = new Socket(ip,port); inputStream = socket.getInputStream(); final byte[] buffer = new byte[1024]; final int len = inputStream.read(buffer); System.out.println(new String(buffer,0,len)); Log.v("AndroidChat","接收成功:"+new String(buffer,0,len)); string = new String(buffer,0,len); } catch (IOException e) { e.printStackTrace(); } //catch (InterruptedException e) { //e.printStackTrace(); //} //} /*try { Socket socket = new Socket(ip,port); socket.shutdownOutput(); inputStream = socket.getInputStream(); inputStreamReader = new InputStreamReader(inputStream); bufferedReader = new BufferedReader(inputStreamReader); String msg = null; msg = bufferedReader.readLine(); while((msg=bufferedReader.readLine()) != null) { inputStreamReader.close(); Log.v("AndroidChat","服务器说:"+msg); System.out.println("服务器说:"+msg); } } catch (IOException e) { e.printStackTrace(); }*/ /*new Runnable(){ @Override public void run() { } };*/ } public class MainActivity extends AppCompatActivity{ private TextView mTv_recive; private Button mBtn_recive; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); mTv_recive = findViewById(R.id.receive); mBtn_recive = findViewById(R.id.btn_1); mBtn_recive.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { new Thread(new Runnable() { @Override public void run() { runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(getApplicationContext(),"123",Toast.LENGTH_SHORT).show(); System.out.println(string); } }); } }).start(); } }); } } }
Send类
package com.example.longin_demo.tcp; import android.util.Log; import java.io.InputStream; import java.io.OutputStream; import java.net.Socket; public class Send extends Thread { private String send; private OutputStream outputStream; private InputStream inputStream; private String ip; private int port; public Send(String msg, int port, String ip) { this.send = msg; this.ip = ip; this.port = port; } @Override public void run() { try { Socket socket = new Socket(ip, port); send = "客户端发来:" + send; inputStream = socket.getInputStream(); outputStream = socket.getOutputStream(); outputStream.write((send + "\n").getBytes("utf-8")); outputStream.flush(); Log.v("AndroidChat", "发送成功:" + send); } catch (Exception e) { Log.v("AndroidChat", "发送失败:" + send + "error" + e.getMessage()); e.printStackTrace(); } } }
登录后的界面
xlm文件
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity2" android:orientation="vertical"> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_alignParentTop="true"> <EditText android:id="@+id/ip" android:layout_width="200dp" android:layout_height="50dp" android:background="#7CB342" android:hint="@string/ip" android:textColor="#ffffff" /> <EditText android:id="@+id/port" android:layout_width="match_parent" android:layout_height="50dp" android:hint="@string/port" android:background="#C0CA33" android:layout_toEndOf="@id/ip" android:inputType="number" /> <Button android:id="@+id/connect" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/port" android:text="@string/connection_server"/> </RelativeLayout> <RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" > <TextView android:id="@+id/receive" android:layout_width="match_parent" android:layout_height="290dp" android:background="#00ACC1" android:textColor="#FFFFFF" android:hint="@string/receive" android:textSize="18sp" android:gravity="center_horizontal" /> <EditText android:id="@+id/send" android:layout_width="match_parent" android:layout_height="290dp" android:background="#3949AB" android:hint="@string/send" android:textColor="#ffffff" android:layout_below="@id/receive"/> <Button android:id="@+id/btn_1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/send1" android:layout_below="@+id/send" /> </RelativeLayout> <!-- <LinearLayout--> <!-- android:layout_width="wrap_content"--> <!-- android:layout_height="wrap_content"--> <!-- android:gravity="left"--> <!-- >--> <!-- <TextView--> <!-- android:layout_width="410dp"--> <!-- android:layout_height="match_parent"--> <!-- android:text="对话窗口"--> <!-- android:gravity="center"--> <!-- android:textSize="30dp"/>--> <!-- </LinearLayout>--> <!-- <LinearLayout--> <!-- android:layout_width="match_parent"--> <!-- android:layout_height="200dp"--> <!-- android:layout_marginStart="0dp"--> <!-- android:layout_marginTop="5dp"--> <!-- android:layout_marginEnd="10dp"--> <!-- android:gravity="center">--> <!-- <EditText--> <!-- android:id="@+id/edit_text"--> <!-- android:layout_width="fill_parent"--> <!-- android:layout_height="fill_parent"/>--> <!-- </LinearLayout>--> <!--<!– <TextView–>--> <!--<!– android:layout_width="match_parent"–>--> <!--<!– android:layout_height="wrap_content"–>--> <!--<!– android:gravity="center"–>--> <!--<!– android:text="登录成功"–>--> <!--<!– android:textSize="25dp"></TextView>–>--> <!-- <LinearLayout--> <!-- android:layout_width="wrap_content"--> <!-- android:layout_height="wrap_content"--> <!-- android:gravity="left"--> <!-- >--> <!-- <TextView--> <!-- android:layout_width="410dp"--> <!-- android:layout_height="match_parent"--> <!-- android:text="发送窗口"--> <!-- android:gravity="center"--> <!-- android:textSize="30dp"/>--> <!-- </LinearLayout>--> <!-- <LinearLayout--> <!-- android:layout_width="match_parent"--> <!-- android:layout_height="200dp"--> <!-- android:layout_marginStart="0dp"--> <!-- android:layout_marginTop="5dp"--> <!-- android:layout_marginEnd="10dp"--> <!-- android:gravity="center">--> <!-- <EditText--> <!-- android:id="@+id/edit_text1"--> <!-- android:layout_width="fill_parent"--> <!-- android:layout_height="fill_parent"/>--> <!-- </LinearLayout>--> <!--<LinearLayout--> <!-- android:layout_width="match_parent"--> <!-- android:layout_height="50dp"--> <!-- >--> <!-- <Button--> <!-- android:layout_width="match_parent"--> <!-- android:layout_height="match_parent"--> <!-- android:text="发送"/>--> <!--</LinearLayout>--> </RelativeLayout>
MainActivity类
package com.example.longin_demo; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.os.Handler; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import com.example.longin_demo.tcp.ConnectThread; import com.example.longin_demo.tcp.Send; public class MainActivity2 extends AppCompatActivity { private Button mBtn_send; private Button mBtn_connect; private EditText mEt_send; private static TextView mTv_recv; private String ip; private int port; private String msg; private ConnectThread ct; private Send send; private Handler handler; static Receive re = new Receive(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); mTv_recv = findViewById(R.id.receive); mBtn_connect = findViewById(R.id.connect); mBtn_send = findViewById(R.id.btn_1); final EditText mEt_ip = this.findViewById(R.id.ip); final EditText mEt_port = this.findViewById(R.id.port); final EditText mEt_send = this.findViewById(R.id.send); /* 设置一个点击事件用以连接线程 */ mBtn_connect.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { ip = mEt_ip.getText().toString();//获取输入的ip port = Integer.parseInt(mEt_port.getText().toString());//获取输入的端口号 ct = new ConnectThread(ip, port,re);//创建一个线程来处理消息的收发 ct.start(); } }); /* 设计一个点击事件用以发送消息 */ mBtn_send.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { msg = mEt_send.getText().toString(); send = new Send(msg, port, ip); send.start(); } }); } /* 这里是和连接接收线程中的 re.setString(string) 联动的,自己理解一下怎么走在脑子里面走一次就差不多了。 */ public static class Receive{ private String string; public String getString(){ return string; } public void setString(String s){ this.string = s; } } /* 下面的callback和连接接收线程中的 MainActivity.callback() 关联,线程运行到那一步后结束回到MainActivity中。 */ public static void callback(){ System.out.println("连接线程执行结束"); mTv_recv.setText(re.getString());//这里是将接收到的文字显示在接收框内。 } }
这个就是大部分的代码了,我也上传了demo可以下载demo安装好环境是可以直接运行的。有什么不懂的欢迎评论区留言,多多交流
https://download.csdn.net/download/m0_61438105/87953707?spm=1001.2014.3001.5503文章来源:https://www.toymoban.com/news/detail-785888.html
下载地址,这个有个小BUG每次连接发送完都需要从新连接一下,有没有大佬指导一下文章来源地址https://www.toymoban.com/news/detail-785888.html
到了这里,关于Android studio 通过mysql连接数据库完成注册登录,登陆后通过tcp协议与电脑的网络调试助手互发信息的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!