创建一个名为“学号+姓名”的用户信息管理程序,该程序用于输入、保存、列表展示和选择删除。
“用户信息管理”程序录入界面对应布局文件的图形化视图,如下图所示。
目 录
1、工程结构图
1.1、activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="用户姓名" />
<EditText
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入姓名!" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="用户密码" />
<EditText
android:id="@+id/pwd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入密码!" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="用户性别" />
<RadioGroup
android:id="@+id/rdg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/male"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="男" />
<RadioButton
android:id="@+id/female"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女" />
</RadioGroup>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="兴趣领域" />
<CheckBox
android:id="@+id/cb1_affairs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="时事" />
<CheckBox
android:id="@+id/cb2_science"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="科技" />
<CheckBox
android:id="@+id/cb3_entertainment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="文娱" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/add"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="添加信息" />
<Button
android:id="@+id/delete"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="删除信息" />
<Button
android:id="@+id/update"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="更改信息" />
<Button
android:id="@+id/query"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="查询信息" />
</LinearLayout>
<TextView
android:id="@+id/show"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
1.2、MainActivity.java
package cn.lwx.my;
import androidx.appcompat.app.AppCompatActivity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private EditText et_name;
private EditText et_pwd;
private RadioGroup radioGroup;
private CheckBox cb1_affairs;
private CheckBox cb2_science;
private CheckBox cb3_entertainment;
private String Gender = "男";
private String[] checkBox = {"时事", "科技", ""};//多选框字符数组
private Button btnAdd;
private Button btnDelete;
private Button btnUpdate;
private Button btnQuery;
private TextView mTvShow;
MyHelper myHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myHelper = new MyHelper(this);
initView();//初始化控件
}
private void initView() {
et_name = (EditText) findViewById(R.id.name);
et_pwd = (EditText) findViewById(R.id.pwd);
radioGroup = (RadioGroup) findViewById(R.id.rdg);
// 判断用户选择的性别
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (checkedId == R.id.male) {
Toast.makeText(MainActivity.this, "男", Toast.LENGTH_SHORT).show();
Gender = "男";
} else {
Toast.makeText(MainActivity.this, "女", Toast.LENGTH_SHORT).show();
Gender = "女";
}
}
});
cb1_affairs = (CheckBox) findViewById(R.id.cb1_affairs);
cb2_science = (CheckBox) findViewById(R.id.cb2_science);
cb3_entertainment = (CheckBox) findViewById(R.id.cb3_entertainment);
btnAdd = (Button) findViewById(R.id.add);
btnDelete = (Button) findViewById(R.id.delete);
btnUpdate = (Button) findViewById(R.id.update);
btnQuery = (Button) findViewById(R.id.query);
mTvShow = (TextView) findViewById(R.id.show);
//多选框监听器
OnBoxClickListener listener = new OnBoxClickListener();
cb1_affairs.setOnClickListener(listener);
cb2_science.setOnClickListener(listener);
cb3_entertainment.setOnClickListener(listener);
//增删改查
btnAdd.setOnClickListener(this);
btnDelete.setOnClickListener(this);
btnUpdate.setOnClickListener(this);
btnQuery.setOnClickListener(this);
}
@Override
public void onClick(View v) {
String name;
String pwd;
String interest = checkBox[0] + "、" + checkBox[1] + "、" + checkBox[2];
SQLiteDatabase db;
ContentValues values;
switch (v.getId()) {
case R.id.add:
name = et_name.getText().toString();
pwd = et_pwd.getText().toString();
db = myHelper.getWritableDatabase();
values = new ContentValues();
values.put("name", name);
values.put("pwd", pwd);
values.put("gender", Gender);
values.put("interest", interest);
db.insert("person", null, values);
Toast.makeText(this, "信息已添加!", Toast.LENGTH_SHORT).show();
db.close();
break;
case R.id.delete:
db = myHelper.getWritableDatabase();
db.delete("person", null, null);
Toast.makeText(this, "信息已删除!", Toast.LENGTH_SHORT).show();
mTvShow.setText("");
db.close();
break;
case R.id.update:
db = myHelper.getWritableDatabase();
values = new ContentValues(); // 要修改的数据
values.put("pwd", pwd = et_pwd.getText().toString());
values.put("gender", Gender);
values.put("interest", interest);
db.update("person", values, "name=?",
new String[]{et_pwd.getText().toString()}); // 更新并得到行数
Toast.makeText(this, "信息已修改", Toast.LENGTH_SHORT).show();
db.close();
break;
case R.id.query:
db = myHelper.getReadableDatabase();
Cursor cursor = db.query("person", null, null,
null, null, null, null);
if (cursor.getCount() == 0) {
Toast.makeText(this, "没有数据!", Toast.LENGTH_SHORT).show();
} else {
cursor.moveToFirst();
mTvShow.setText("Name: " + cursor.getString(1) + " ; Pwd: " + cursor.getString(2) +
" ; Gender: " + cursor.getString(3) + " ; Interest: " + cursor.getString(4));
}
while (cursor.moveToNext()) {
mTvShow.append("\n" + "Name: " + cursor.getString(1) + " ; Pwd: " + cursor.getString(2) +
" ; Gender: " + cursor.getString(3) + " ; Interest: " + cursor.getString(4));
}
cursor.close();
db.close();
break;
}
}
class OnBoxClickListener implements View.OnClickListener {
@Override
public void onClick(View v) {
CheckBox box = (CheckBox) v;
if (box.isChecked()) {
if (box.getId() == R.id.cb1_affairs) {
Toast.makeText(MainActivity.this, "1-时事-被选中!", Toast.LENGTH_SHORT).show();
checkBox[0] = "时事";
} else if (box.getId() == R.id.cb2_science) {
Toast.makeText(MainActivity.this, "2-科技-被选中!", Toast.LENGTH_SHORT).show();
checkBox[1] = "科技";
} else if (box.getId() == R.id.cb3_entertainment) {
Toast.makeText(MainActivity.this, "3-文娱-被选中!", Toast.LENGTH_SHORT).show();
checkBox[2] = "文娱";
}
} else {
if (box.getId() == R.id.cb1_affairs) {
Toast.makeText(MainActivity.this, "1-时事-未被选中!", Toast.LENGTH_SHORT).show();
checkBox[0] = "";
} else if (box.getId() == R.id.cb2_science) {
Toast.makeText(MainActivity.this, "2-科技-未被选中!", Toast.LENGTH_SHORT).show();
checkBox[1] = "";
} else { // if (box.getId() == R.id.cb3_entertainment)
Toast.makeText(MainActivity.this, "3-文娱-未被选中!", Toast.LENGTH_SHORT).show();
checkBox[2] = "";
}
}
}
}
}
1.3、MyHelper.java
package cn.lwx.my;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import androidx.annotation.Nullable;
public class MyHelper extends SQLiteOpenHelper {
public MyHelper(Context context) {
super(context, "lwx.db", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create Table person(id INTEGER PRIMARY KEY AUTOINCREMENT, " +
"name VARCHAR(20), pwd VARCHAR(20), gender VARCHAR(6), interest VARCHAR(20))");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
2、运行效果图
写于,2020年6月16日,晚。
咦,好巧。我人生中第一部手机,是在2018年6月16日买的。【高考后,不久~~~】
目前,还有一些小Bug!!!