首页 > 其他分享 >安卓应用开发日记3

安卓应用开发日记3

时间:2024-02-24 11:58:21浏览次数:17  
标签:findViewById 安卓 widget 日记 应用 id import android void

给添加账单的部分输入框做了一些限制和提示,时间没做限制只是个普通的输入框 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();
}
}

标签:findViewById,安卓,widget,日记,应用,id,import,android,void
From: https://www.cnblogs.com/dddjm/p/18030906

相关文章

  • 安卓应用开发日记5
    优化一下删除功能,根据角色删除全部数据packagecom.example.helloworld;importandroidx.appcompat.app.AppCompatActivity;importandroid.content.Context;importandroid.database.sqlite.SQLiteDatabase;importandroid.os.Bundle;importandroid.view.View;importandroid.w......
  • 安卓应用开发日记10
    修正主界面显示资产,并且每次切入主界面重新计算总资产,简易记账本完工packagecom.example.helloworld;importstaticcom.example.helloworld.util.DateUtil.getTime;importandroidx.appcompat.app.AppCompatActivity;importandroid.content.Intent;importandroid.os.Bundle;i......
  • P4119 Ynoi2018 未来日记
    P4119Ynoi2018未来日记lxl出的题好duliu啊。感谢来自fr200110217102的博客题解P4119【Ynoi2018未来日记】。下标分块+值域分块+并查集其实一开始的方向应该是尝试线段树或者其它的动态维护的算法,直到时间复杂度和空间复杂度对不上,你才会想到——要分块!区间第\(k\)......
  • appium进行windows桌面应用自动化及启动windows驱动报错解决方案
    安装appium环境参考文档:https://www.cnblogs.com/simon1993/p/16273390.htmlappium安装驱动找到官方驱动安装秘钥http://appium.io/docs/en/latest/ecosystem/drivers/打开cmd执行安装windows驱动命令安装windows驱动windows开发的驱动:https://github.com/Microsoft/WinA......
  • 单调栈的定义与应用
    定义:单调栈是一种特殊的栈结构,通常用于解决一类特定的问题,如找到数组中元素的下一个更大(或更小)元素。它的核心特性是维护栈内元素的单调性,即栈内元素按照从栈底到栈顶的顺序,要么严格递增,要么严格递减。也即:单调递增栈:从栈底到栈顶,依次递增的顺序单调递减栈:从栈底到栈顶,依次递......
  • 安卓家庭记账本开发笔记8(补2月4日,2月5日,2月7日,2月8日)
    完成收支记录界面的按钮的监听以及点击事件的逻辑编写在后端模块创建一个名为frag_record的软件包,在其中创建三个java类,其中两个分别对应支出和收入,因为两者基本逻辑相同,所以将两者相同的部分提取出来写一个通用java类。然后支出和收入的java类继承于通用类,然后相应的不同功能在......
  • 猜字谜|构建生成式 AI 应用实践(一)
    在2023亚马逊云科技re:Invent之后,细心的开发者们也许已经发现有一个很有趣的动手实验:开发一款可部署的基于大语言模型的字谜游戏:该款游戏使用了文生图模型为玩家提供一个未知的提示词,玩家需要根据模型生成的图像来猜测该提示词,来完成游戏。该动手实验完整地展示了如何在亚马......
  • 记录 re:Invent 大会,使用 PartyRock 编写我们第一个 AI 应用以及心得
    如果说2023年什么应用技术最火,那么说是OpenAI为代表的ChatGPT在AI方面的突破和发展,是完全没有任何的争议的。随后,各大云厂商以及应用集成商甚至垂直领域的服务提供商都有了对应的AI模型。我们开玩笑的说,这个好比多年前的百团大战一样,各种的AI相关的应用奔涌出现、百......
  • 第9章操作系统和应用的关系
    操作系统构成:1控制程序:硬件控制、程序运行控制2编程语言处理器:汇编、编译、解析3实用程序:文本编辑器、调试工具、Dump程序等操作系统本身不是单独的程序,而是多个程序的集合体,这个运行环境下,应用不直接控制硬件,而是通过操作系统来间接控制。系统调用:操作系统的硬件控制功......
  • 操作系统于应用程序的关系
    大家在计算机上运行程序大多是为了提高工作效率。而对于作为应用程序运行环境的操作系统,人们则是直接使用市场上成型的参评。但是,我们不能忽略操作系统的情况下编写应用程序。很久之前,操作系统还不存在,程序员需要从零开始编写能够完成各种操作的程序。这实在太麻烦了。于是,有人开......