第三周总结
本周老师向我们布置了个人作业,在一周内我完成了登录注册的功能,代码如下:
<EditText
android:id="@+id/username_edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="用户名"
android:inputType="text"
android:maxLines="1"/>
<EditText
android:id="@+id/password_edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="密码"
android:inputType="textPassword"
android:maxLines="1"/>
<Button
android:id="@+id/login_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登录" />
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
Button loginButton = findViewById(R.id.login_button);
loginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EditText usernameEdittext = findViewById(R.id.username_edittext);
EditText passwordEdittext = findViewById(R.id.password_edittext);
String username = usernameEdittext.getText().toString();
String password = passwordEdittext.getText().toString();
SQLiteDatabase db = dbHelper.getWritableDatabase();
Cursor cursor = db.rawQuery("select * from user where username=? and password=?",
new String[]{username, password});
if (cursor.moveToFirst()) {
int id = cursor.getInt(cursor.getColumnIndex("id"));
String nickname = cursor.getString(cursor.getColumnIndex("nickname"));
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
intent.putExtra("id",id);
intent.putExtra("username",username);
intent.putExtra("nickname",nickname);
startActivity(intent);
finish();
} else {
Toast.makeText(LoginActivity.this, "用户名或密码错误", Toast.LENGTH_SHORT).show();
}
}
});
}
<EditText
android:id="@+id/username_edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="用户名"
android:inputType="text"
android:maxLines="1"/>
<EditText
android:id="@+id/password_edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="密码"
android:inputType="textPassword"
android:maxLines="1"/>
<EditText
android:id="@+id/nickname_edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="昵称"
android:inputType="text"
android:maxLines="1"/>
<Button
android:id="@+id/register_button"
android:layout_width="match_parent"
android:layout_height="wrap_content
"
android:text="注册" />
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
Button registerButton = findViewById(R.id.register_button);
registerButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EditText usernameEdittext = findViewById(R.id.username_edittext);
EditText passwordEdittext = findViewById(R.id.password_edittext);
EditText nicknameEdittext = findViewById(R.id.nickname_edittext);
String username = usernameEdittext.getText().toString();
String password = passwordEdittext.getText().toString();
String nickname = nicknameEdittext.getText().toString();
SQLiteDatabase db = dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("username", username);
values.put("password", password);
values.put("nickname", nickname);
db.insert("user", null, values);
Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
finish();
}
});
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 【杂谈】分布式事务——高大上的无用知识?