【基于Java语言的Android个人开发笔记,个人工具类笔记】App常见的通用的登陆界面

安卓登陆界面

做一个简单的安卓登陆界面,因为学习安卓不久,也是花了不少时间,至此记录一下。
一个简单的XML文件代码

<?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:background="@drawable/backgrongd"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:orientation="vertical"
    tools:context=".login.Login">


    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/ic_launcher"
        android:layout_gravity="center"
        android:layout_marginTop="80dp"
        />
    <LinearLayout
        android:background="@drawable/border_off"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="10dp"
        >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="用户账号"
            android:textSize="24dp"/>
        <EditText
            android:id="@+id/login_edt_username"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="te01st"
            android:hint="请输入账号"/>
    </LinearLayout>
    <LinearLayout
        android:background="@drawable/border_off"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="10dp">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="用户密码"
            android:textSize="24dp"/>
        <EditText
            android:id="@+id/login_edt_password"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="123"
            android:hint="请输入密码"
            android:inputType="textPassword" />
    </LinearLayout>
    <androidx.appcompat.widget.LinearLayoutCompat
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_marginTop="10dp"
        android:orientation="horizontal">
        <Button
            android:background="@drawable/border_selector"
            android:id="@+id/login_but_login"
            android:layout_marginHorizontal="30dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="登录"/>
        <Button
            android:background="@drawable/border_selector"
            android:id="@+id/login_but_register"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginHorizontal="30dp"
            android:text="注册"/>
    </androidx.appcompat.widget.LinearLayoutCompat>

</LinearLayout>

下面我来编写内部的功能部分


    private String username;
    private String password;

    private void initButton() {
        loginButLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                username = "" + loginEdtUsername.getText().toString().trim();
                password = "" + loginEdtPassword.getText().toString().trim();
                if (username.equals("") || password.equals("")) {
                    Toast.makeText(Login.this, "内容不能为空!", Toast.LENGTH_SHORT).show();
                } else {
                    JSONObject jsonObject = new JSONObject();
                    String url = "http://124.93.196.45:10091/prod-api/api/login";
                    try {
                        jsonObject.put("username", username);
                        jsonObject.put("password", password);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    RequestQueue requestQueue = Volley.newRequestQueue(Login.this);
                    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(1, url, jsonObject, new Response.Listener<JSONObject>() {
                        @Override
                        public void onResponse(JSONObject jsonObject) {
                            try {
                                if (jsonObject.getString("msg").contains("操作成功")) {
                                    AppConfig appConfig = new AppConfig();
                                    appConfig.setTOKEN(jsonObject.getString("token"));
                                    Toast.makeText(getApplicationContext(), "" + jsonObject.getString("msg"), Toast.LENGTH_SHORT).show();
                                    startActivity(new Intent(Login.this, MainActivity.class));
                                } else {
                                    Toast.makeText(getApplicationContext(), "" + jsonObject.getString("msg"), Toast.LENGTH_SHORT).show();
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    }, new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError volleyError) {
                        }
                    });
                    requestQueue.add(jsonObjectRequest);
                }
            }
        });
        loginButRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(Login.this, Register.class));
            }
        });
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值