(因为时间原因功能尚不全面,之后会继续更新)
放一下目录结构,大体会用到这些——(有对应activity.xxx.xml的,创建的时候要创建activity,不要单独创建java文件)
——这一篇先放登录功能的部分代码——
(不然就太长啦)
activity_main.xml 登录界面设计
(注意,android:background=里的@mipmap/bg1是我自己找的图片,这个图片应该放在res下的drawable-xhdpi里,没有drawable-xhdpi的新建一个)
<?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:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp"
android:background="@mipmap/bg1">
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="@string/app_name"
android:textColor="@color/orange"
android:textSize="35sp"
android:gravity="center"
android:layout_marginTop="180dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="80dp">
<EditText
android:id="@+id/et_userid"
android:layout_width="match_parent"
android:layout_height="50dp"
android:textSize="16sp"
android:textColor="@color/black"
android:hint=" 学号/教师编号"
android:maxLines="1"
android:background="@drawable/bg_frame"
/>
<EditText
android:id="@+id/et_password"
android:layout_width="match_parent"
android:layout_height="50dp"
android:textSize="16dp"
android:textColor="@color/black"
android:hint=" 密 码"
android:inputType="textPassword"
android:layout_marginTop="5dp"
android:maxLines="1"
android:background="@drawable/bg_frame"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="30dp">
<Button
android:id="@+id/btn_start"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="登录"
android:textColor="@color/black"
android:background="@drawable/bg_frame"
android:layout_marginRight="2dp"
/>
<Button
android:id="@+id/btn_login"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="注册"
android:textColor="@color/black"
android:background="@drawable/bg_frame"
android:layout_marginLeft="2dp"
/>
</LinearLayout>
</LinearLayout>
MainActivity登录界面后台代码
package com.example.text002;
import static db.DBManager.getUserstart;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import db.DBManager;
import db.UserBean;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
//声明控件
private EditText mainusrid, mainpassword;//id和密码
private Button mainbtnlogin, mainbtnstart;//注册,登录
private DBManager db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
find();
}
private void find() {
mainbtnlogin = findViewById(R.id.btn_login);
mainbtnstart = findViewById(R.id.btn_start);
mainusrid = findViewById(R.id.et_userid);
mainpassword = findViewById(R.id.et_password);
mainbtnstart.setOnClickListener(MainActivity.this);
mainbtnlogin.setOnClickListener(MainActivity.this);
}
@Override
public void onClick(View view) {
int vid = view.getId();
switch (vid){
case R.id.btn_start:
//登录
String uid = mainusrid.getText().toString();
String uname = mainpassword.getText().toString();
Boolean flag = getUserstart(uid, uname);
if(flag){
Toast.makeText(this,"登录成功OvO",Toast.LENGTH_SHORT).show();
Intent intent = new Intent(MainActivity.this,FunctionActivity.class);
startActivity(intent);
} else {
Toast.makeText(this,"登录失败TAT",Toast.LENGTH_SHORT).show();
}
break;
case R.id.btn_login:
//注册
Toast.makeText(this,"HELLO~",Toast.LENGTH_SHORT).show();
Intent intent = new Intent(MainActivity.this,LoginActivity.class);
startActivity(intent);
break;
}
}
}
下一篇放注册代码
标签:layout,app1,height,width,简单,import,android,id,记事本 From: https://www.cnblogs.com/yansans/p/17232177.html