使用Android studio写一个简单的记账本代码
时间: 2024-03-17 13:44:59 浏览: 119
Android Studio实现一个简约记账APP,代码附详细注释
5星 · 资源好评率100%
好的,下面是一个简单的记账本的Android Studio示例代码:
1. 布局文件:activity_main.xml
```
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/btn_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="添加账单"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginRight="16dp"
android:layout_marginBottom="16dp"/>
<ListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/btn_add"
android:layout_marginBottom="16dp"/>
</RelativeLayout>
```
2. 账单项布局文件:list_item_bill.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="wrap_content"
android:orientation="horizontal"
android:padding="16dp">
<TextView
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:textStyle="bold"/>
<TextView
android:id="@+id/tv_amount"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="end"
android:textSize="16sp"/>
</LinearLayout>
```
3. 账单数据模型类:Bill.java
```
public class Bill {
private String name;
private double amount;
public Bill(String name, double amount) {
this.name = name;
this.amount = amount;
}
public String getName() {
return name;
}
public double getAmount() {
return amount;
}
}
```
4. 自定义适配器类:BillAdapter.java
```
public class BillAdapter extends ArrayAdapter<Bill> {
private Context mContext;
private int mResource;
public BillAdapter(Context context, int resource, List<Bill> objects) {
super(context, resource, objects);
mContext = context;
mResource = resource;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(mContext).inflate(mResource, parent, false);
}
Bill bill = getItem(position);
TextView tvName = convertView.findViewById(R.id.tv_name);
TextView tvAmount = convertView.findViewById(R.id.tv_amount);
tvName.setText(bill.getName());
tvAmount.setText(String.format(Locale.getDefault(), "%.2f", bill.getAmount()));
return convertView;
}
}
```
5. 主Activity类:MainActivity.java
```
public class MainActivity extends AppCompatActivity {
private List<Bill> mBillList = new ArrayList<>();
private BillAdapter mAdapter;
private ListView mListView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mListView = findViewById(R.id.list_view);
mAdapter = new BillAdapter(this, R.layout.list_item_bill, mBillList);
mListView.setAdapter(mAdapter);
Button btnAdd = findViewById(R.id.btn_add);
btnAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showAddBillDialog();
}
});
}
private void showAddBillDialog() {
final EditText etName = new EditText(this);
final EditText etAmount = new EditText(this);
etAmount.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
layout.addView(etName);
layout.addView(etAmount);
AlertDialog dialog = new AlertDialog.Builder(this)
.setTitle("添加账单")
.setView(layout)
.setPositiveButton("保存", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String name = etName.getText().toString().trim();
double amount = Double.parseDouble(etAmount.getText().toString().trim());
Bill bill = new Bill(name, amount);
mBillList.add(bill);
mAdapter.notifyDataSetChanged();
}
})
.setNegativeButton("取消", null)
.create();
dialog.show();
}
}
```
这是一个简单的记账本示例代码,当然还有很多其他的实现方式和功能可以添加。希望这可以帮助你开始编写一个记账本应用程序。
阅读全文