第一阶段验收时我们的项目仍然没有大的进展,老师说我们可以做一个leisiyuqq空间发表动态的软件,加上定位,在发布之后能够根据地点查看曾今上传过的照片,于是将之前所作的项目推翻,c重新开始了新项目的完成
首先写了登录页面

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">99999

<!--使用线性布局-->
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#F5F5F5">

    <!--Logo-->
    <ImageView
        android:id="@+id/LogoImage"
        android:layout_width="match_parent"
        android:layout_height="120dp"
        android:layout_marginTop="100dp"
        />

    <!--标题-->
    <TextView
        android:id="@+id/TitleText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="25dp"
        android:text="登录"
        android:gravity="center"
        android:textStyle="italic"
        android:textColor="#808080"
        android:textSize="30dp" />

    <!--嵌套线性布局-->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <!--嵌套线性布局-->
        <LinearLayout
            android:id="@+id/UserNameLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <!--用户名输入-->
            <EditText
                android:id="@+id/UserNameEdit"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:padding="15dp"
                android:layout_marginLeft="20dp"
                android:layout_marginRight="20dp"
                android:layout_marginTop="10dp"
                android:layout_marginBottom="15dp"
                android:background="@drawable/translucent_edit"
                android:hint="输入用户名"
                android:textSize="24dp"
                android:singleLine="true" />

        </LinearLayout>

        <!--嵌套线性布局-->
        <LinearLayout
            android:id="@+id/PassWordLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <!--密码输入-->
            <EditText
                android:id="@+id/PassWordEdit"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:padding="15dp"
                android:layout_marginLeft="20dp"
                android:layout_marginRight="20dp"
                android:layout_marginTop="10dp"
                android:layout_marginBottom="15dp"
                android:background="@drawable/translucent_edit"
                android:hint="输入用户密码"
                android:textSize="24dp"
                android:maxLength="16"
                android:singleLine="true"
                android:inputType="textPassword" />

        </LinearLayout>

        <!--嵌套线性布局-->
        <LinearLayout
            android:id="@+id/LayoutButton"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <!--登录按钮-->
            <androidx.appcompat.widget.AppCompatButton
                android:id="@+id/LoginButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="15dp"
                android:layout_margin="15dp"
                android:layout_weight="1"
                android:textColor="@color/white"
                android:background="@drawable/translucent_button"
                android:text="登   录"
                android:textSize="24dp" />

            <!--注册按钮-->
            <androidx.appcompat.widget.AppCompatButton
                android:id="@+id/SignUpButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="15dp"
                android:layout_margin="15dp"
                android:layout_weight="1"
                android:textColor="@color/white"
                android:background="@drawable/translucent_button"
                android:text="注   册"
                android:textSize="24dp" />

        </LinearLayout>

    </LinearLayout>

</LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>
登录功能
package com.example.share;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

//import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.example.share.register;
import com.example.share.dao.UserDao;
import com.example.share.utils.CommonUtils;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

private Button LoginButton,SignUpButton;
private EditText UserNameEdit,PassWordEdit;
UserDao userDao;
private Handler mainHandler;


@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    LoginButton = findViewById(R.id.LoginButton);
    SignUpButton=findViewById(R.id.SignUpButton);

    UserNameEdit = findViewById(R.id.UserNameEdit);
    PassWordEdit = findViewById(R.id.PassWordEdit);

    userDao=new UserDao();
    mainHandler=new Handler(getMainLooper());

    LoginButton.setOnClickListener(this);
    SignUpButton.setOnClickListener(this);
}


@Override
public void onClick(View v) {
    if (v.getId() == R.id.LoginButton) {
        String name=UserNameEdit.getText().toString().trim();
        String password=PassWordEdit.getText().toString().trim();

        new Thread(new Runnable() {
            @Override
            public void run() {

                String Password="";
                String TeacherPassword="";
                Password=userDao.login(name);

                if(password.equals(Password)){
                    Intent intent=new Intent(getApplicationContext(), DongtaiActivity.class);
                    Bundle bundle=new Bundle();
                    bundle.putString("name",name);
                    intent.putExtras(bundle);
                    startActivity(intent);
                    mainHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            CommonUtils.showShortMsg(MainActivity.this,"登陆成功");
                            SharedPreferences sharedPref = getSharedPreferences("app_prefs", Context.MODE_PRIVATE);
                            SharedPreferences.Editor editor = sharedPref.edit();
                            editor.putString("username", name);
                            editor.apply();
                            Intent intent=new Intent(getApplicationContext(), DongtaiActivity.class);
                        }
                    });

                }
                else if(password.equals(TeacherPassword)){
                    Intent intent=new Intent(getApplicationContext(), DongtaiActivity.class);
                    startActivity(intent);
                    mainHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            CommonUtils.showShortMsg(MainActivity.this,"登陆成功");
                            Intent intent=new Intent(getApplicationContext(), DongtaiActivity.class);
                        }
                    });

                }
                else{
                    mainHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            CommonUtils.showShortMsg(MainActivity.this,"登陆失败");
                        }
                    });
                }
            }
        }).start();
    }
    else if(v.getId()==R.id.SignUpButton){
        Intent intent=new Intent(getApplicationContext(), DongtaiActivity.class);
        startActivity(intent);
    }
}




@Override
public void onPointerCaptureChanged(boolean hasCapture) {
    super.onPointerCaptureChanged(hasCapture);
}

}

posted on 2024-06-28 21:56  一点都不难  阅读(16)  评论(0编辑  收藏  举报