Android 用户信息管理程序【SQLite数据库、多选框、单选按钮】
生活随笔
收集整理的這篇文章主要介紹了
Android 用户信息管理程序【SQLite数据库、多选框、单选按钮】
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
創建一個名為“學號+姓名”的用戶信息管理程序,該程序用于輸入、保存、列表展示和選擇刪除。
“用戶信息管理”程序錄入界面對應布局文件的圖形化視圖,如下圖所示。
目? ?錄
1、工程結構圖
1.1、activity_main.xml
1.2、MainActivity.java
1.3、MyHelper.java
2、運行效果圖
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"><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:text="用戶姓名" /><EditTextandroid:id="@+id/name"android:layout_width="match_parent"android:layout_height="wrap_content"android:hint="請輸入姓名!" /><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:text="用戶密碼" /><EditTextandroid:id="@+id/pwd"android:layout_width="match_parent"android:layout_height="wrap_content"android:hint="請輸入密碼!" /><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:text="用戶性別" /><RadioGroupandroid:id="@+id/rdg"android:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><RadioButtonandroid:id="@+id/male"android:layout_width="wrap_content"android:layout_height="wrap_content"android:checked="true"android:text="男" /><RadioButtonandroid:id="@+id/female"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="女" /></RadioGroup><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="興趣領域" /><CheckBoxandroid:id="@+id/cb1_affairs"android:layout_width="wrap_content"android:layout_height="wrap_content"android:checked="true"android:text="時事" /><CheckBoxandroid:id="@+id/cb2_science"android:layout_width="wrap_content"android:layout_height="wrap_content"android:checked="true"android:text="科技" /><CheckBoxandroid:id="@+id/cb3_entertainment"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="文娛" /><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><Buttonandroid:id="@+id/add"android:layout_weight="1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="添加信息" /><Buttonandroid:id="@+id/delete"android:layout_weight="1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="刪除信息" /><Buttonandroid:id="@+id/update"android:layout_weight="1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="更改信息" /><Buttonandroid:id="@+id/query"android:layout_weight="1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="查詢信息" /></LinearLayout><TextViewandroid: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;@Overrideprotected 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() {@Overridepublic 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);}@Overridepublic 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 {@Overridepublic 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);}@Overridepublic 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))");}@Overridepublic void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {} }2、運行效果圖
寫于,2020年6月16日,晚。
咦,好巧。我人生中第一部手機,是在2018年6月16日買的。【高考后,不久~~~】
目前,還有一些小Bug!!!
總結
以上是生活随笔為你收集整理的Android 用户信息管理程序【SQLite数据库、多选框、单选按钮】的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 数据结构Java03【(时间、空间复杂度
- 下一篇: 数据结构Java04【树结构概述、创建、