登录功能完成,花费了两个小时
涉及到SQLite数据库的操作以及一些服务端和用户界面的交互等都是比较生疏的知识点
User.java
package com.example.component; public class User { private String username; private String password; public User() { } public User(String username, String password) { this.username = username; this.password = password; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
DatabaseHelper.java
package com.example.component; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; public class DatabaseHelper extends SQLiteOpenHelper { static String name="user"; static int dbVersion=1; public DatabaseHelper(Context context) { super(context, name, null, dbVersion); } //只在创建的时候用一次 public void onCreate(SQLiteDatabase db) { String sql="create table user(id integer primary key autoincrement,username varchar(20),password varchar(20))"; db.execSQL(sql); System.out.println("--数据库创建成功--"); } public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } }
UserDao.java
其中注册的功能不在这里,只是利用注册的方法向数据库中存了数据
package com.example.component; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; public class UserDao { private DatabaseHelper dbHelper; public UserDao(Context context){ dbHelper=new DatabaseHelper(context); } //登录用 public boolean login(String username,String password){ SQLiteDatabase sdb=dbHelper.getReadableDatabase(); String sql="select * from user where username=? and password=?"; Cursor cursor=sdb.rawQuery(sql, new String[]{username,password}); if(cursor.moveToFirst()==true){ cursor.close(); return true; } return false; } //注册用 public boolean register(User user){ SQLiteDatabase sdb=dbHelper.getReadableDatabase(); String sql="insert into user(username,password) values(?,?)"; Object obj[]={user.getUsername(),user.getPassword()}; sdb.execSQL(sql, obj); return true; } }
布局文件
activity_login.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:layout_height="match_parent" android:gravity="center" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:gravity="center_horizontal"> <EditText android:id="@+id/et_username" android:layout_width="160dp" android:layout_height="wrap_content" android:hint="手机号" android:singleLine="true" android:layout_marginBottom="10dp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:gravity="center_horizontal"> <EditText android:id="@+id/et_password" android:layout_width="160dp" android:layout_height="wrap_content" android:hint="密码" android:inputType="textPassword" android:layout_marginTop="5dp" /> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" android:gravity="center_horizontal"> <Button android:id="@+id/bt_login" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="登录" android:paddingRight="20dp" android:layout_marginTop="30dp"/> <Button android:id="@+id/bt_bos" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="注册" android:layout_marginTop="30dp" android:layout_marginLeft="20dp"/> </LinearLayout> </LinearLayout>
LoginActivity.java
Intent对象中的参数一定要是下一个活动
package com.example.component; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.text.TextUtils; import android.util.Log; import android.view.View; import android.widget.EditText; import android.widget.Toast; import androidx.annotation.Nullable; public class LoginActivity extends Activity { private EditText password; private EditText username; private View login; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); DatabaseHelper helper=new DatabaseHelper(this); helper.getWritableDatabase(); //找到控件 initView(); initListener(); } private void initListener() { login.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { handlerLogin(); } }); } private void initView() { //成员变量 c+s+v username = (EditText) this.findViewById(R.id.et_username); password = (EditText) this.findViewById(R.id.et_password); login=this.findViewById(R.id.bt_login); } private void handlerLogin() { String usernameText = username.getText().toString().trim(); String pwdText = password.getText().toString().trim(); //空/长度 if (TextUtils.isEmpty(usernameText)) { //Toast.makeText(this,"用户名不能为空",Toast.LENGTH_SHORT).show(); Toast.makeText(this, "用户名不能为空", Toast.LENGTH_SHORT).show(); return; } if (TextUtils.isEmpty(pwdText)) { Toast.makeText(this, "密码不能为空", Toast.LENGTH_SHORT).show(); return; } //检测是否能够登陆 //参数用this UserDao dao = new UserDao(this); boolean flag = dao.login(usernameText, pwdText); //成功登录才会跳转 if (flag) { Intent in = new Intent(this, LoginResultActivity.class); in.putExtra("username", usernameText); in.putExtra("pwd", pwdText); startActivity(in); Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show(); Log.i("TAG", "登录成功"); } else { Log.i("TAG", "登录失败"); } } }
activity_result.xml
只是简单的显示
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/login_result" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:textSize="20sp"/> </LinearLayout>
LoginResultActivity.java
如果还有其他 功能稍微改改就好了
package com.example.component; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.widget.TextView; import androidx.annotation.Nullable; public class LoginResultActivity extends Activity { @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_result); TextView result = this.findViewById(R.id.login_result); //获取内容 Intent in = getIntent(); if (in != null) { String un=in.getStringExtra("username"); String pwd=in.getStringExtra("pwd"); result.setText("恭喜-----"+un+"-----登录成功"); } } }
在AndroidManifest.xml文件中将LoginActivity注册成为程序的入口
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.component"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <!-- <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter>--> </activity> <activity android:name=".LoginActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".LoginResultActivity"/> </application> </manifest>