創建一個名爲“學號+姓名”的用戶信息管理程序,該程序用於輸入、保存、列表展示和選擇刪除。
“用戶信息管理”程序錄入界面對應佈局文件的圖形化視圖,如下圖所示。
目 錄
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!!!