新建虚拟手机:
效果:
res->layout->activity_main.xml:
点击code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android
xmlns:app
xmlns:tools
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_height="200dp"
android:layout_width="match_parent"
android:layout_marginTop="150dp"
android:background="#dddddd"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="用户登录"
android:textAlignment="center"
android:textSize="20dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:paddingLeft="50dp"
android:paddingRight="50dp">
<TextView
android:layout_width="60dp"
android:layout_height="match_parent"
android:gravity="center"
android:text="用户名" />
<EditText
android:id="@+id/txt_user"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:inputType="text"
android:singleLine="true"
android:textSize="14dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:paddingLeft="50dp"
android:paddingRight="50dp">
<TextView
android:layout_width="60dp"
android:layout_height="match_parent"
android:gravity="center"
android:text="密码" />
<EditText
android:id="@+id/txt_pwd"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:inputType="textPassword"
android:singleLine="true"
android:textSize="14dp" />
</LinearLayout>
<LinearLayout
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="50dp">
<Button
android:id="@+id/btn_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:text="登录"/>
<Button
android:id="@+id/btn_reset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="重置"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
Logcat:
package com.example.demo;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
public Button btnLogin;
public Button btnReset;
public TextView txtUser, txtPwd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
//绑定事件
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.e("事件", "点击登录");
}
});
btnReset.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.e("事件", "点击登录");
}
});
}
public void initView(){
btnLogin = findViewById(R.id.btn_login);
btnReset = findViewById(R.id.btn_reset);
txtUser = findViewById(R.id.txt_user);
txtPwd = findViewById(R.id.txt_pwd);
}
}
简单功能:
重置:
public void doReset(){
//清空输入框
txtUser.setText("");
txtPwd.setText("");
}
登录功能:
引入模块:
implementation ("com.squareup.okhttp3:okhttp:4.9.1")
这里:
记得要下载:
给权限:
<uses-permission android:name="android.permission.INTERNET"/>
这里我搞了一个flask项目,不能用127.0.0.1
from flask import Flask, request
app = Flask(__name__)
@app.route("/login", methods=['GET', 'POST'])
def login():
print(request.form)
user = request.form.get('user')
pwd = request.form.get('pwd')
return "OOOK"
if __name__ == '__main__':
app.run(host="0.0.0.0", port=8000)
报错:
not permitted by network security policy