给添加账单的部分输入框做了一些限制和提示,时间没做限制只是个普通的输入框
package com.example.helloworld;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.Toolbar;
import com.example.helloworld.database.UserDbHelper;
import com.example.helloworld.enity.User;
import com.example.helloworld.util.ToastUtil;
public class AddBill extends AppCompatActivity {
EditText amountEditText, noteEditText,timeEditText;
RadioGroup personRadioGroup, transactionTypeRadioGroup;
Button addButton, clearButton;
private UserDbHelper mHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_bill);
// 初始化视图元素
amountEditText = findViewById(R.id.money);
noteEditText = findViewById(R.id.note);
personRadioGroup = findViewById(R.id.personRadioGroup);
timeEditText = findViewById(R.id.timeInput);
transactionTypeRadioGroup = findViewById(R.id.transactionTypeRadioGroup);
addButton = findViewById(R.id.addButton);
clearButton = findViewById(R.id.clearButton);
// 添加按钮点击事件监听器
addButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
addTransaction();
}
});
// 清除按钮点击事件监听器
clearButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
clearFields();
}
});
}
private void setSupportActionBar(Toolbar toolbar) {
}
@Override
protected void onStart(){
super.onStart();
mHelper = UserDbHelper.getInstance(this);
mHelper.openReadLink();
mHelper.openWriteLink();
}
@Override
protected void onStop(){
super.onStop();
mHelper.closeLink();
}
// 添加交易的方法
private void addTransaction() {
// 获取金额和备注输入的值
String amount = amountEditText.getText().toString();
String note = noteEditText.getText().toString();
String time=timeEditText.getText().toString();
// 检查金额和备注是否为空
if (amount.isEmpty() || note.isEmpty()) {
Toast.makeText(getApplicationContext(), "请填写金额和备注", Toast.LENGTH_SHORT).show();
return;
}
// 转换金额为 double 类型
double amountValue = Double.parseDouble(amount);
// 获取选中的人物和交易类型
RadioButton selectedPersonRadioButton = findViewById(personRadioGroup.getCheckedRadioButtonId());
String person = selectedPersonRadioButton.getText().toString();
RadioButton selectedTransactionTypeRadioButton = findViewById(transactionTypeRadioGroup.getCheckedRadioButtonId());
String transactionType = selectedTransactionTypeRadioButton.getText().toString();
if (transactionType.equals("出账")){
amountValue=-amountValue;
}
// 执行添加到数据库或其他操作
User user=new User(person,note, (float) amountValue,time);
mHelper.insert(user);
ToastUtil.show(this, "成功");
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("添加成功")
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// 用户点击确定按钮后返回主界面
dialog.dismiss();
Intent intent = new Intent(AddBill.this, MainActivity.class);
startActivity(intent);
finish(); // 结束当前活动,防止用户通过返回键再次回到当前活动
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
// 清除输入框的方法
private void clearFields() {
amountEditText.setText("");
noteEditText.setText("");
personRadioGroup.clearCheck();
transactionTypeRadioGroup.clearCheck();
}
}
package com.example.helloworld;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.Toolbar;
import com.example.helloworld.database.UserDbHelper;
import com.example.helloworld.enity.User;
import com.example.helloworld.util.ToastUtil;
public class AddBill extends AppCompatActivity {
EditText amountEditText, noteEditText,timeEditText;
RadioGroup personRadioGroup, transactionTypeRadioGroup;
Button addButton, clearButton;
private UserDbHelper mHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_bill);
// 初始化视图元素
amountEditText = findViewById(R.id.money);
noteEditText = findViewById(R.id.note);
personRadioGroup = findViewById(R.id.personRadioGroup);
timeEditText = findViewById(R.id.timeInput);
transactionTypeRadioGroup = findViewById(R.id.transactionTypeRadioGroup);
addButton = findViewById(R.id.addButton);
clearButton = findViewById(R.id.clearButton);
// 添加按钮点击事件监听器
addButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
addTransaction();
}
});
// 清除按钮点击事件监听器
clearButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
clearFields();
}
});
}
private void setSupportActionBar(Toolbar toolbar) {
}
@Override
protected void onStart(){
super.onStart();
mHelper = UserDbHelper.getInstance(this);
mHelper.openReadLink();
mHelper.openWriteLink();
}
@Override
protected void onStop(){
super.onStop();
mHelper.closeLink();
}
// 添加交易的方法
private void addTransaction() {
// 获取金额和备注输入的值
String amount = amountEditText.getText().toString();
String note = noteEditText.getText().toString();
String time=timeEditText.getText().toString();
// 检查金额和备注是否为空
if (amount.isEmpty() || note.isEmpty()) {
Toast.makeText(getApplicationContext(), "请填写金额和备注", Toast.LENGTH_SHORT).show();
return;
}
// 转换金额为 double 类型
double amountValue = Double.parseDouble(amount);
// 获取选中的人物和交易类型
RadioButton selectedPersonRadioButton = findViewById(personRadioGroup.getCheckedRadioButtonId());
String person = selectedPersonRadioButton.getText().toString();
RadioButton selectedTransactionTypeRadioButton = findViewById(transactionTypeRadioGroup.getCheckedRadioButtonId());
String transactionType = selectedTransactionTypeRadioButton.getText().toString();
if (transactionType.equals("出账")){
amountValue=-amountValue;
}
// 执行添加到数据库或其他操作
User user=new User(person,note, (float) amountValue,time);
mHelper.insert(user);
ToastUtil.show(this, "成功");
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("添加成功")
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// 用户点击确定按钮后返回主界面
dialog.dismiss();
Intent intent = new Intent(AddBill.this, MainActivity.class);
startActivity(intent);
finish(); // 结束当前活动,防止用户通过返回键再次回到当前活动
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
// 清除输入框的方法
private void clearFields() {
amountEditText.setText("");
noteEditText.setText("");
personRadioGroup.clearCheck();
transactionTypeRadioGroup.clearCheck();
}
}
package com.example.helloworld;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.Toolbar;
import com.example.helloworld.database.UserDbHelper;
import com.example.helloworld.enity.User;
import com.example.helloworld.util.ToastUtil;
public class AddBill extends AppCompatActivity {
EditText amountEditText, noteEditText,timeEditText;
RadioGroup personRadioGroup, transactionTypeRadioGroup;
Button addButton, clearButton;
private UserDbHelper mHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_bill);
// 初始化视图元素
amountEditText = findViewById(R.id.money);
noteEditText = findViewById(R.id.note);
personRadioGroup = findViewById(R.id.personRadioGroup);
timeEditText = findViewById(R.id.timeInput);
transactionTypeRadioGroup = findViewById(R.id.transactionTypeRadioGroup);
addButton = findViewById(R.id.addButton);
clearButton = findViewById(R.id.clearButton);
// 添加按钮点击事件监听器
addButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
addTransaction();
}
});
// 清除按钮点击事件监听器
clearButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
clearFields();
}
});
}
private void setSupportActionBar(Toolbar toolbar) {
}
@Override
protected void onStart(){
super.onStart();
mHelper = UserDbHelper.getInstance(this);
mHelper.openReadLink();
mHelper.openWriteLink();
}
@Override
protected void onStop(){
super.onStop();
mHelper.closeLink();
}
// 添加交易的方法
private void addTransaction() {
// 获取金额和备注输入的值
String amount = amountEditText.getText().toString();
String note = noteEditText.getText().toString();
String time=timeEditText.getText().toString();
// 检查金额和备注是否为空
if (amount.isEmpty() || note.isEmpty()) {
Toast.makeText(getApplicationContext(), "请填写金额和备注", Toast.LENGTH_SHORT).show();
return;
}
// 转换金额为 double 类型
double amountValue = Double.parseDouble(amount);
// 获取选中的人物和交易类型
RadioButton selectedPersonRadioButton = findViewById(personRadioGroup.getCheckedRadioButtonId());
String person = selectedPersonRadioButton.getText().toString();
RadioButton selectedTransactionTypeRadioButton = findViewById(transactionTypeRadioGroup.getCheckedRadioButtonId());
String transactionType = selectedTransactionTypeRadioButton.getText().toString();
if (transactionType.equals("出账")){
amountValue=-amountValue;
}
// 执行添加到数据库或其他操作
User user=new User(person,note, (float) amountValue,time);
mHelper.insert(user);
ToastUtil.show(this, "成功");
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("添加成功")
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// 用户点击确定按钮后返回主界面
dialog.dismiss();
Intent intent = new Intent(AddBill.this, MainActivity.class);
startActivity(intent);
finish(); // 结束当前活动,防止用户通过返回键再次回到当前活动
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
// 清除输入框的方法
private void clearFields() {
amountEditText.setText("");
noteEditText.setText("");
personRadioGroup.clearCheck();
transactionTypeRadioGroup.clearCheck();
}
}