本周老师向我们布置了个人作业,在一周内我完成了登录注册的功能,代码如下:
<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();
}
});
}
标签:总结,username,layout,edittext,第三周,android,password,id From: https://www.cnblogs.com/srz123/p/17471862.html