安卓写一个寝室充电费、水费、空调费的功能,可以选择充值的寝室,在点击充值按钮,充值到对应的寝室,并且可以点击查询就可以看到所有寝室的充值的金额,连接数据库Sqlite,需要详细代码和xml布局
时间: 2024-05-04 19:22:14 浏览: 116
寝室专用小空调
好的,这里是一个简单的实现,包含了寝室充值和查询功能。首先,我们需要创建一个包含寝室号、充值金额、充值时间的数据库表,可以使用 SQLite 数据库进行存储。
1. 创建数据库表
在 Android 项目中,可以使用 SQLiteOpenHelper 类来创建和管理数据库。以下是一个简单的实现:
```java
public class DBHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "dormitory.db";
private static final int DATABASE_VERSION = 1;
private static final String TABLE_NAME = "charge";
private static final String COLUMN_ROOM = "room";
private static final String COLUMN_AMOUNT = "amount";
private static final String COLUMN_TIME = "time";
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String sql = "CREATE TABLE " + TABLE_NAME + " (" +
COLUMN_ROOM + " TEXT PRIMARY KEY, " +
COLUMN_AMOUNT + " INTEGER, " +
COLUMN_TIME + " TEXT)";
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Not required in this example
}
}
```
2. 编写布局文件和逻辑代码
接下来,我们需要创建一个界面,让用户输入寝室号和充值金额,以及选择充值类型(电费、水费、空调费)。在按钮点击事件中,我们将数据插入到数据库中。同时,我们需要创建另一个界面,用于显示所有寝室的充值金额。以下是布局文件和逻辑代码:
activity_main.xml:
```xml
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">
<TextView
android:text="寝室号"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/edit_room"
android:inputType="text"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:text="充值金额"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp" />
<EditText
android:id="@+id/edit_amount"
android:inputType="number"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:text="充值类型"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp" />
<RadioGroup
android:id="@+id/radio_group"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/radio_electricity"
android:text="电费"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RadioButton
android:id="@+id/radio_water"
android:text="水费"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RadioButton
android:id="@+id/radio_aircon"
android:text="空调费"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RadioGroup>
<Button
android:id="@+id/btn_charge"
android:text="充值"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp" />
</LinearLayout>
```
activity_main2.xml:
```xml
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">
<TextView
android:text="寝室号 充值金额 充值时间"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
```
MainActivity.java:
```java
public class MainActivity extends AppCompatActivity {
private EditText editRoom;
private EditText editAmount;
private RadioGroup radioGroup;
private Button btnCharge;
private DBHelper dbHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editRoom = findViewById(R.id.edit_room);
editAmount = findViewById(R.id.edit_amount);
radioGroup = findViewById(R.id.radio_group);
btnCharge = findViewById(R.id.btn_charge);
dbHelper = new DBHelper(this);
btnCharge.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String room = editRoom.getText().toString();
int amount = Integer.parseInt(editAmount.getText().toString());
String type = ((RadioButton)findViewById(radioGroup.getCheckedRadioButtonId())).getText().toString();
String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
SQLiteDatabase db = dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(DBHelper.COLUMN_ROOM, room);
values.put(DBHelper.COLUMN_AMOUNT, amount);
values.put(DBHelper.COLUMN_TIME, time);
db.insert(DBHelper.TABLE_NAME, null, values);
Toast.makeText(MainActivity.this, "充值成功", Toast.LENGTH_SHORT).show();
}
});
}
public void onClickQuery(View view) {
Intent intent = new Intent(this, MainActivity2.class);
startActivity(intent);
}
}
```
MainActivity2.java:
```java
public class MainActivity2 extends AppCompatActivity {
private ListView listView;
private DBHelper dbHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
listView = findViewById(R.id.list_view);
dbHelper = new DBHelper(this);
SQLiteDatabase db = dbHelper.getReadableDatabase();
Cursor cursor = db.query(DBHelper.TABLE_NAME, null, null, null, null, null, null);
ArrayList<String> dataList = new ArrayList<>();
if (cursor.moveToFirst()) {
do {
String room = cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_ROOM));
int amount = cursor.getInt(cursor.getColumnIndex(DBHelper.COLUMN_AMOUNT));
String time = cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_TIME));
String data = room + " " + amount + " " + time;
dataList.add(data);
} while (cursor.moveToNext());
}
cursor.close();
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, dataList);
listView.setAdapter(adapter);
}
}
```
3. 运行程序
完成以上步骤后,运行程序即可看到界面。在第一个界面中,用户输入寝室号和充值金额,选择充值类型,点击“充值”按钮即可完成充值。在第二个界面中,显示所有寝室的充值金额。
阅读全文