上一篇我们已经将登陆服务器端写完。接下来我们来完成Android客户端的实现。如果没有Android 基础,建议先学习Android的知识,推荐《第一行代码——Android》第二版。
打开Android Studio软件,新建一个项目。
首先是布局文件activity_main
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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=".MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="134dp"
android:ems="10"
android:inputType="textPersonName"
android:hint="请输入用户名"
/>
<EditText
android:id="@+id/password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="207dp"
android:ems="10"
android:hint="请输入密码"
android:inputType="textPassword" />
<Button
android:id="@+id/login"
android:layout_width="215dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="171dp"
android:text="登陆" />
</RelativeLayout>
</android.support.constraint.ConstraintLayout>
接下里是MainActivity
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
EditText username;
EditText password;
Button Login;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
username = (EditText)findViewById(R.id.username);
password = (EditText)findViewById(R.id.password);
Login = (Button)findViewById(R.id.login);
Login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String Username = username.getText().toString();
String Password = password.getText().toString();
Toast.makeText(MainActivity.this,"用户名:"+Username+"密码:"+Password,Toast.LENGTH_SHORT).show();;
}
});
}
}
我们在这里先创建了两个EditText和一个Button ,然后进行绑定ID。给出了登陆按钮的点击事件。当点击Login的时候以Toast的形式将EditText中的数据展示出来。
效果如下图
本篇就先讲到这里,接下来我们就应该将获得数据发送给服务器端进行解析。将在下一篇中展示。欢迎大家批评指正。
【超越梦想】Android+Servlet搭建自己的app(3-2)——Android端登陆实现