android 仿微信demo——持续更新(实现移动端,服务端)
Android仿微信demo——持续更新
一、Android Studio创建移动端项目
首先,我们需要在Android Studio中创建一个新的移动端项目。打开Android Studio,选择"Start a new Android Studio project"选项。
填写基本信息:
* Name:输入项目名称,如"微信仿demo"
* Package name:输入包名,如"com.example.wechatdemo"
* Save location:选择保存位置* Language:选择Java或Kotlin(本例中使用Java)
* Minimum SDK:选择最低支持的SDK版本,如API21(5.0)
点击"Finish"按钮,创建项目。
二、微信启动界面显示页面延迟
在`activity_main.xml`文件中添加一个布局:
```xml
android:layout_height="match_parent" android:orientation="vertical"> android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@mipmap/ic_launcher" />
```
在`MainActivity.java`文件中添加代码:
```javapublic class MainActivity extends AppCompatActivity {
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 添加延迟显示页面的逻辑 Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
Intent intent = new Intent(MainActivity.this, LoginActivity.class);
startActivity(intent);
finish();
}
},2000); //2秒后跳转到LoginActivity }
}
```
三、activity启动页
在`activity_login.xml`文件中添加一个布局:
```xml
android:layout_height="match_parent" android:orientation="vertical"> android:layout_width="wrap_content" android:layout_height="wrap_content" android:hint="请输入用户名" /> android:layout_width="wrap_content" android:layout_height="wrap_content" android:hint="请输入密码" />
```
在`LoginActivity.java`文件中添加代码:
```javapublic class LoginActivity extends AppCompatActivity {
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
Button button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// 添加登录逻辑 EditText editText = findViewById(R.id.editText);
String username = editText.getText().toString();
EditText editText2 = findViewById(R.id.editText2);
String password = editText2.getText().toString();
if (username.equals("admin") && password.equals("123456")) {
Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
startActivity(intent);
finish();
} else {
Toast.makeText(LoginActivity.this, "登录失败", Toast.LENGTH_SHORT).show();
}
}
});
}
}
```
四、activity测试
在`MainActivity.java`文件中添加代码:
```javapublic class MainActivity extends AppCompatActivity {
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 添加测试逻辑 Button button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast.makeText(MainActivity.this, "点击了按钮", Toast.LENGTH_SHORT).show();
}
});
}
}
```
五、移动端注册相关功能实现
在`RegisterActivity.java`文件中添加代码:
```javapublic class RegisterActivity extends AppCompatActivity {
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
// 添加注册逻辑 Button button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
EditText editText = findViewById(R.id.editText);
String username = editText.getText().toString();
EditText editText2 = findViewById(R.id.editText2);
String password = editText2.getText().toString();
if (username.equals("admin") && password.equals("123456")) {
Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
startActivity(intent);
finish();
} else {
Toast.makeText(RegisterActivity.this, "注册失败", Toast.LENGTH_SHORT).show();
}
}
});
}
}
```
六、服务端
在`Server.java`文件中添加代码:
```javaimport java.net.*;
import java.io.*;
public class Server {
public static void main(String[] args) throws IOException {
ServerSocket serverSocket = new ServerSocket(8080);
System.out.println("服务器启动成功");
Socket socket = serverSocket.accept();
System.out.println("客户端连接成功");
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String message = bufferedReader.readLine();
if (message.equals("admin") && "123456".equals(message)) {
PrintWriter printWriter = new PrintWriter(socket.getOutputStream());
printWriter.println("登录成功");
socket.close();
} else {
PrintWriter printWriter = new PrintWriter(socket.getOutputStream());
printWriter.println("登录失败");
socket.close();
}
}
}
```
七、测试
在`MainActivity.java`文件中添加代码:
```javapublic class MainActivity extends AppCompatActivity {
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 添加测试逻辑 Button button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, LoginActivity.class);
startActivity(intent);
finish();
}
});
}
}
```
八、总结
本文介绍了如何在Android Studio中创建一个移动端项目,并实现了微信启动界面显示页面延迟、activity启动页、activity测试、移动端注册相关功能实现和服务端。