【超越梦想】Android+Servlet搭建自己的app(3-1)——Android端登录实现

上一篇我们已经将登陆服务器端写完。接下来我们来完成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端登陆实现

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值