android+studio登陆注册页面源码
时间: 2023-11-27 15:45:00 浏览: 112
Android Studio 登录注册页面的源码可以分为以下几个部分:
1. 布局文件:包括登录和注册页面的布局,可以使用 XML 文件来定义。
2. Java 代码:包括登录和注册页面的逻辑处理,可以使用 Java 代码来实现。
3. 数据库:用于存储用户信息,可以使用 SQLite 数据库来实现。
以下是一个简单的登录注册页面的源码示例:
布局文件 login_activity.xml:
```
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="@+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Username" />
<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:inputType="textPassword" />
<Button
android:id="@+id/login_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Login" />
<TextView
android:id="@+id/register_link"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Don't have an account? Register here." />
</LinearLayout>
```
Java 代码 LoginActivity.java:
```
public class LoginActivity extends AppCompatActivity {
private EditText mUsername;
private EditText mPassword;
private Button mLoginButton;
private TextView mRegisterLink;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login_activity);
mUsername = findViewById(R.id.username);
mPassword = findViewById(R.id.password);
mLoginButton = findViewById(R.id.login_button);
mRegisterLink = findViewById(R.id.register_link);
mLoginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 登录逻辑处理
}
});
mRegisterLink.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 跳转到注册页面
}
});
}
}
```
数据库 DBHelper.java:
```
public class DBHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "mydatabase.db";
private static final int DATABASE_VERSION = 1;
private static final String TABLE_NAME = "users";
private static final String COLUMN_ID = "id";
private static final String COLUMN_USERNAME = "username";
private static final String COLUMN_PASSWORD = "password";
private static final String CREATE_TABLE =
"CREATE TABLE " + TABLE_NAME + " (" +
COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
COLUMN_USERNAME + " TEXT, " +
COLUMN_PASSWORD + " TEXT)";
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
public boolean addUser(String username, String password) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(COLUMN_USERNAME, username);
values.put(COLUMN_PASSWORD, password);
long result = db.insert(TABLE_NAME, null, values);
return result != -1;
}
public boolean checkUser(String username, String password) {
SQLiteDatabase db = this.getReadableDatabase();
String[] columns = {COLUMN_ID};
String selection = COLUMN_USERNAME + " = ?" + " AND " + COLUMN_PASSWORD + " = ?";
String[] selectionArgs = {username, password};
Cursor cursor = db.query(TABLE_NAME, columns, selection, selectionArgs, null, null, null);
int count = cursor.getCount();
cursor.close();
return count > 0;
}
}
```
阅读全文