我的第一个程序——登录界面

在这里插入图片描述
这是我的登录界面截图,构成并不复杂。

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".loginActivity"

    >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:layout_editor_absoluteY="-103dp">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="30dp"
            android:gravity="center"
            android:background="@color/silver"
            android:text="登录"
            android:textSize="20sp"/>
        <TextView
            android:id="@+id/skip_login"
            android:onClick="skip"
            android:layout_marginTop="10dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:textColor="@color/teal_200"
            android:text="管理员"
            tools:ignore="OnClick" />
        <TextView
            android:id="@+id/user_login"
            android:onClick="user"
            android:layout_marginTop="10dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:textColor="@color/teal_200"
            android:text="用户"
            tools:ignore="OnClick" />

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:orientation="horizontal"
            android:layout_marginTop="100dp">

            <EditText
                android:id="@+id/login_name"
                android:layout_width="321dp"
                android:layout_height="wrap_content"
                android:digits="0123456789"
                android:hint="请输入你的账号"
                android:maxLines="1" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:orientation="horizontal">
            <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">
                <ToggleButton
                    android:checked="false"
                    android:id="@+id/eye"
                    android:layout_width="40dp"
                    android:layout_height="30dp"
                    android:background="@drawable/eyes"
                    android:layout_marginTop="5dp"
                    android:layout_alignRight="@id/login_password"
                    android:textOff=""
                    android:textOn=""
                    />

                <EditText
                    android:id="@+id/login_password"
                    android:layout_width="321dp"
                    android:layout_height="wrap_content"
                    android:hint="请输入你的密码"
                    android:inputType="textPassword"
                    android:maxLines="1" />

            </RelativeLayout>
        </LinearLayout>
        <TextView
            android:onClick="forget"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:text="忘记密码?"
            android:textColor="@color/orange" />
        <CheckBox
            android:id="@+id/remember"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:text="记住账号密码"/>


        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal">

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="30dp"
                android:background="@drawable/shape_button"
                android:onClick="login"
                android:text="登录" />
        </LinearLayout>

        <TextView
            android:id="@+id/register"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="10dp"
            android:onClick="register"
            android:text="还没有账号?点击注册!" />
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:orientation="horizontal"
            android:layout_marginTop="150dp"
            android:paddingBottom="20dp">

            <CheckBox
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:checked="true"
                android:text="我已同意" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="《用户协议》"
                android:textColor="@color/blue" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text=" 《隐私协议》"
                android:textColor="@color/blue" />
        </LinearLayout>


    </LinearLayout>



</androidx.constraintlayout.widget.ConstraintLayout>
package com.example.boolstore;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Paint;
import android.os.Bundle;
import android.os.Environment;
import android.os.Looper;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.util.Log;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;

import com.example.admin.adminActivity;
import com.example.fragment.mineFragment;
import com.example.sql.DBopenHelper;
import com.example.sql.UserService;
import com.example.boolstore.Http;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class loginActivity extends AppCompatActivity {
    private EditText login_name,login_password;
    private TextView tv_login,register;
    private CheckBox remember;
    private ToggleButton eye;
    private static final String TAG = "LoginActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        //getSupportActionBar().hide();
        //getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        //getSupportActionBar().hide();

        login_name = findViewById(R.id.login_name);
        login_password = findViewById(R.id.login_password);

        register = findViewById(R.id.register);
        register.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
        register.getPaint().setAntiAlias(true);

        tv_login = findViewById(R.id.skip_login);
        remember = findViewById(R.id.remember);
        eye = findViewById(R.id.eye);
        //密码显示与隐藏
        eye.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked){
                    login_password.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                }else{
                    login_password.setTransformationMethod(PasswordTransformationMethod.getInstance());
                }
            }
        });
        //读取保存用户名和密码的文件
        try {
            //getExternalStorageDirectory  获取外存路径
            //this.getFilesDir() == /data/data/包名/files/  私有目录
            //getCacheDir 缓存文件,保存临时数据 == /data/data/包名/cache/
            File file = new File(this.getFilesDir(),"info.txt");
            FileInputStream fis = new FileInputStream(file);
            BufferedReader br = new BufferedReader(new InputStreamReader(fis));
            String info = br.readLine();
            String userName = info.split("==")[0];
            String password = info.split("==")[1];
            login_name.setText(userName);
            login_password.setText(password);
            Log.i(TAG,"已填入账号密码");
        } catch (IOException e) {
            e.printStackTrace();
        }
        //创建数据库
       /* DBopenHelper helper = new DBopenHelper(this);
        SQLiteDatabase db = helper.getWritableDatabase();*/
    }

    @SuppressLint("WrongConstant")
    public void login(View view) {
        final String Username = login_name.getText().toString().trim();
        final String Password = login_password.getText().toString().trim();
        final String path = "http://192.168.19.1:8080/androidlogin2/Login?Username=" + Username + "&Password=" + Password;
        if(TextUtils.isEmpty(Username)||TextUtils.isEmpty(Password)){
            Toast toast = Toast.makeText(this,"账号或密码未输入!",0);
            //toast提示框的位置
            toast.setGravity(Gravity.CENTER,0,0);
            toast.show();
        }else{
            Log.i(TAG,"username="+Username+"  password="+Password);
            Log.i("MainActivity",path);
            String result = Http.HttpGet(path);
            if (result.equals("登录失败")){
                Log.i(TAG,"用户名或密码错误!");
                Toast.makeText(loginActivity.this,"用户名或密码错误",Toast.LENGTH_SHORT).show();
            } else {
                if (result.equals("登录成功2")) {
                    Toast.makeText(loginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                    Log.i(TAG, "登录成功");
                    Intent intent = new Intent(loginActivity.this, totalActivity.class);
                    intent.putExtra("Username", Username);
                    startActivity(intent);
                    Log.i(TAG, "进入用户页面!");
                } else {
                    //访问失败
                    if (result.equals("登录成功1")){
                        Intent intent = new Intent(loginActivity.this, adminActivity.class);
                        startActivity(intent);
                        Log.i(TAG, "进入管理员页面!");
                    }else{
                        Log.i(TAG,"连接失败");
                    }
                }
            }

           /* //提交账号密码到服务器
            new Thread(){
                public void run(){
                    URL url = null;
                    try {
                        url = new URL(path);
                        Log.i(TAG,path);
                        HttpURLConnection conn = (HttpURLConnection)url.openConnection();
                        Log.i(TAG,"正在访问.....");
                        conn.setRequestMethod("GET");
                        conn.setConnectTimeout(10000);//设置连接超时时间
                        conn.setReadTimeout(5000);//设置读取超时时间
                        conn.connect();//开始连接
                        Log.i(TAG,"开始连接 ");
                        int responseCode=conn.getResponseCode();//获取响应吗
                        if(responseCode==200){
                            //访问成功
                            Log.i(TAG,"访问成功!");
                            InputStream in = conn.getInputStream();//
                            BufferedReader reader = new BufferedReader(new InputStreamReader(in));
                            String result = reader.readLine();//读取服务器进行逻辑处理后页面显示的数据
                            Looper.prepare();
                            if (result.equals("登录失败")){
                                Log.i(TAG,"用户名或密码错误!");
                                Toast.makeText(loginActivity.this,"用户名或密码错误",Toast.LENGTH_SHORT).show();
                            } else {
                                if (result.equals("登录成功2")) {
                                    Toast.makeText(loginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                                    Log.i(TAG, "登录成功");
                                    Intent intent = new Intent(loginActivity.this, totalActivity.class);
                                    intent.putExtra("Username", Username);
                                    startActivity(intent);
                                    Log.i(TAG, "进入用户页面!");
                                } else {
                                    //访问失败
                                    if (result.equals("登录成功1")){
                                    Intent intent = new Intent(loginActivity.this, adminActivity.class);
                                    startActivity(intent);
                                    Log.i(TAG, "进入管理员页面!");
                                }
                                }
                            }
                            Looper.loop();

                        }
                    } catch (MalformedURLException e) {
                        e.printStackTrace();
                        Log.i(TAG,"访问失败2!");
                    } catch (IOException e) {
                        e.printStackTrace();
                        Log.i(TAG,"访问失败3!");
                    }
                }
            }.start();
*/
            //记住用户名和密码
            if(remember.isChecked()){
                Log.i(TAG, "记住密码");
                try {
                    //判断Sd卡是否可以被读写,是否存在
                    String status = Environment.getExternalStorageState();
                    if(Environment.MEDIA_MOUNTED.equals(status)){
                        File file = new File(this.getFilesDir(),"info.txt");
                        FileOutputStream fos = new FileOutputStream(file);
                        String info = Username+"=="+Password;
                        fos.write(info.getBytes());
                        fos.close();
                        Toast.makeText(loginActivity.this, "保存用户名和密码成功",Toast.LENGTH_SHORT).show();
                        Log.i(TAG,"保存用户名和密码成功!");
                    }else{
                        Toast.makeText(loginActivity.this, "SD卡不可用", 0).show();
                        Log.i(TAG,"SD卡不可用");
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                    Toast.makeText(loginActivity.this, "保存用户名和密码失败", Toast.LENGTH_SHORT).show();
                    Log.i(TAG,"保存用户名和密码失败");
                }


            }else{
                Log.i(TAG, "不记住密码");
                File file = new File(this.getFilesDir(),"info.txt");
                try {
                    FileOutputStream fos = new FileOutputStream(file);
                    String info = " "+"=="+" ";
                    fos.write(info.getBytes());
                    fos.close();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
    }

    public void skip(View view){
        Intent intent = new Intent(loginActivity.this, adminActivity.class);
        startActivity(intent);


    }
    public void user(View view){
        Intent intent = new Intent(loginActivity.this,totalActivity.class);
        startActivity(intent);

    }
    public void forget(View view){
        Intent intent = new Intent(loginActivity.this,forgetActivity.class);
        startActivity(intent);
    }

    public void register(View view) {
        Intent intent = new Intent(loginActivity.this,registerActivity.class);
        startActivity(intent);
    }
}

上面是布局代码,下面是java代码。
我的登录界面中在右上角给自己留了两个后门按钮,因为我自己不想一遍遍登录测试。
输入账号密码点击登录后,会将账号密码通过get方式提交到服务器,根据服务器返回值判读账号是否已经存在,并判断权限是普通用户还是管理员。get方式提交到服务器可以直接用java代码中被注释的那一块。
可以记住上一次登录的账号密码,如果用户勾选记住账号密码会将用户的账号密码保存在本地,下一次登录时会自动从本地检索并填入账号密码。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值