家庭记账本4
MainActivity.java
1、通过Calender获取系统时间
定义控件
private EditText et_date;
private Calendar calendar; // 通过Calendar获取系统时间
在oncreate()中初始化控件
et_date = view.findViewById(R.id.et_date);
calendar = Calendar.getInstance();
点击”日期“按钮布局,设置日期
et_date.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new DatePickerDialog(MainActivity.this,
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int day) {
// TODO Auto-generated method stub
mYear = year;
mMonth = month;
mDay = day;
// 更新EditText控件日期 小于10加0
et_date.setText(new StringBuilder()
.append(mYear)
.append("-")
.append((mMonth + 1) < 10 ? "0"
+ (mMonth + 1) : (mMonth + 1))
.append("-")
.append((mDay < 10) ? "0" + mDay : mDay));
}
}, calendar.get(Calendar.YEAR), calendar
.get(Calendar.MONTH), calendar
.get(Calendar.DAY_OF_MONTH)).show();
}
});
2、点击添加弹出对话框
private void showDialog() {
//创建对话框对象
AlertDialog.Builder builder = new AlertDialog.Builder(this);
//设置自定义对话框
final AlertDialog dialog = builder.create();
//点击对话框以外不会退出,但返回会退出
dialog.setCanceledOnTouchOutside(false);
//加载xml布局
View view = View.inflate(this, R.layout.activity_add, null);
//初始化控件
et_money = view.findViewById(R.id.et_money);
et_date = view.findViewById(R.id.et_date);
calendar = Calendar.getInstance();
// 点击"日期"按钮布局 设置日期
/* et_date.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new DatePickerDialog(MainActivity.this,
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int day) {
// TODO Auto-generated method stub
mYear = year;
mMonth = month;
mDay = day;
// 更新EditText控件日期 小于10加0
et_date.setText(new StringBuilder()
.append(mYear)
.append("-")
.append((mMonth + 1) < 10 ? "0"
+ (mMonth + 1) : (mMonth + 1))
.append("-")
.append((mDay < 10) ? "0" + mDay : mDay));
}
}, calendar.get(Calendar.YEAR), calendar
.get(Calendar.MONTH), calendar
.get(Calendar.DAY_OF_MONTH)).show();
}
*/
});
et_title = view.findViewById(R.id.et_title);
RadioGroup type_group = view.findViewById(R.id.typegroup);
Button tijiao = view.findViewById(R.id.tijiao);
//将View对象加载到dialog上
dialog.setView(view);
//显示dialog
dialog.show();
MyAdapter
class MyAdapter extends BaseAdapter {
@Override
//1.ListView的长度,即集合长度
public int getCount() {
return moneyInfoList.size();
}
//getItem(int position)和getItemId(int position)也必须重写(因为这是是BaseAdapter中的抽象方法),在调用ListView的响应方法的时候才会被调用到,这里不影响布局
@Override
public Object getItem(int position) {
//根据索引获取当前集合里的对象
return moneyInfoList.get(position);
}
@Override
public long getItemId(int position) {
//返回索引值
return position;
}
//根据长度绘制item
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
//把布局文件转换成View对象
//View.inflate加载xml布局,getApplicationContext() 上下文环境
View view = View.inflate(getApplicationContext(), R.layout.activity_chaxun, null);
//初始化
TextView tv_money = view.findViewById(R.id.show_money);
TextView tv_date = view.findViewById(R.id.show_date);
TextView tv_title = view.findViewById(R.id.show_title);
TextView tv_type = view.findViewById(R.id.show_type);
ImageView iv_delete = view.findViewById(R.id.iv_delete);
tv_money.setText(moneyInfoList.get(position).getMoney());
tv_date.setText(moneyInfoList.get(position).getDate());
tv_title.setText(moneyInfoList.get(position).getTitle());
//获取到拦截模式并转换成int类型
type = (moneyInfoList.get(position).getType());
switch (type) {
case "支出":
tv_type.setText("支出");
break;
case "收入":
tv_type.setText("收入");
break;
}
}

浙公网安备 33010602011771号