学习进度条3.14
所花时间:6小时
代码量:300
搏客量:2
了解到的知识点:
1.今日做了一个简单的as项目,用于计算地铁票价
`package com.example.myapplication;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import java.util.HashMap;
import java.util.Map;
public class MainActivity extends AppCompatActivity {
private EditText startStation, endStation, ticketQuantity;
private TextView resultText;
private Button calculateButton, payButton;
// 地铁线路数据
private Map<String, Integer> line1 = new HashMap<>();
private Map<String, Integer> line2 = new HashMap<>();
private Map<String, Integer> line3 = new HashMap<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 初始化UI组件
startStation = findViewById(R.id.startStation);
endStation = findViewById(R.id.endStation);
ticketQuantity = findViewById(R.id.ticketQuantity);
resultText = findViewById(R.id.resultText);
calculateButton = findViewById(R.id.calculateButton);
payButton = findViewById(R.id.payButton);
// 初始化地铁线路数据
initializeSubwayLines();
// 计算费用按钮点击事件
calculateButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
calculateFare();
}
});
// 缴费按钮点击事件
payButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
payFare();
}
});
}
// 初始化地铁线路数据
private void initializeSubwayLines() {
// 1号线
line1.put("福泽", 1);
line1.put("园博园", 2);
line1.put("商务中心", 3);
line1.put("会展中心", 4);
line1.put("东庄", 5);
line1.put("西庄", 6);
line1.put("洨河大道", 7);
line1.put("南村", 8);
line1.put("石家庄东站", 9);
line1.put("火炬广场", 10);
line1.put("留村", 11);
line1.put("白佛", 12);
line1.put("朝晖桥", 13);
line1.put("谈固", 14);
line1.put("北宋", 15);
line1.put("体育场", 16);
line1.put("博物院", 17);
line1.put("北国商城", 18);
line1.put("平安大街", 19);
line1.put("解放广场", 20);
line1.put("新百广场", 21);
line1.put("烈士陵园", 22);
line1.put("和平医院", 23);
line1.put("长城桥", 24);
line1.put("时光街", 25);
line1.put("西王", 26);
// 2号线
line2.put("柳辛庄", 1);
line2.put("铁道大学", 2);
line2.put("义堂", 3);
line2.put("建和桥", 4);
line2.put("长安公园", 5);
line2.put("北国商城", 6);
line2.put("裕华路", 7);
line2.put("槐中路", 8);
line2.put("欧韵公园", 9);
line2.put("元村", 10);
line2.put("石家庄", 11);
line2.put("塔坛", 12);
line2.put("仓丰路留村", 13);
line2.put("南位", 14);
line2.put("嘉华路", 15);
// 3号线
line3.put("乐乡", 1);
line3.put("太行南大街", 2);
line3.put("南豆", 3);
line3.put("中仰陵", 4);
line3.put("西仰陵", 5);
line3.put("东二环南路", 6);
line3.put("位同", 7);
line3.put("南王", 8);
line3.put("东王", 9);
line3.put("塔冢", 10);
line3.put("孙村", 11);
line3.put("汇通路", 12);
line3.put("石家庄", 13);
line3.put("西三教", 14);
line3.put("槐安桥", 15);
line3.put("东里", 16);
line3.put("新百广场", 17);
line3.put("市二中", 18);
line3.put("市庄", 19);
line3.put("柏林庄", 20);
line3.put("高柱", 21);
line3.put("西三庄", 22);
}
// 计算地铁费用
private void calculateFare() {
String start = startStation.getText().toString();
String end = endStation.getText().toString();
String quantityStr = ticketQuantity.getText().toString();
if (start.isEmpty() || end.isEmpty() || quantityStr.isEmpty()) {
Toast.makeText(this, "请输入起点、终点和购票数量", Toast.LENGTH_SHORT).show();
return;
}
int quantity = Integer.parseInt(quantityStr);
if (quantity <= 0) {
Toast.makeText(this, "购票数量必须大于0", Toast.LENGTH_SHORT).show();
return;
}
int startIndex = getStationIndex(start);
int endIndex = getStationIndex(end);
if (startIndex == -1 || endIndex == -1) {
Toast.makeText(this, "输入的站点不存在", Toast.LENGTH_SHORT).show();
return;
}
int distance = Math.abs(endIndex - startIndex);
int farePerTicket = calculateFareBasedOnDistance(distance);
int totalFare = farePerTicket * quantity;
resultText.setText("费用:" + totalFare + "元");
}
// 根据站点名称获取索引
private int getStationIndex(String station) {
if (line1.containsKey(station)) {
return line1.get(station);
} else if (line2.containsKey(station)) {
return line2.get(station);
} else if (line3.containsKey(station)) {
return line3.get(station);
}
return -1;
}
// 根据距离计算费用
private int calculateFareBasedOnDistance(int distance) {
if (distance <= 3) {
return 1;
} else {
return 1+distance/3;
}
}
// 缴费按钮点击事件
private void payFare() {
String fare = resultText.getText().toString();
if (fare.equals("费用:0元")) {
Toast.makeText(this, "请先计算费用", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "缴费成功:" + fare, Toast.LENGTH_SHORT).show();
}
}
<!-- 起点输入框 -->
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/startStationLayout"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="起点站"
app:boxStrokeColor="@color/purple_500"
app:boxStrokeWidth="1dp"
app:boxStrokeWidthFocused="2dp"
android:layout_marginBottom="16dp">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/startStation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:textColor="@color/black"
android:textColorHint="@color/gray_500" />
</com.google.android.material.textfield.TextInputLayout>
<!-- 终点输入框 -->
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/endStationLayout"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="终点站"
app:boxStrokeColor="@color/purple_500"
app:boxStrokeWidth="1dp"
app:boxStrokeWidthFocused="2dp"
android:layout_marginBottom="16dp">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/endStation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:textColor="@color/black"
android:textColorHint="@color/gray_500" />
</com.google.android.material.textfield.TextInputLayout>
<!-- 购票数量输入框 -->
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/ticketQuantityLayout"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="购票数量"
app:boxStrokeColor="@color/purple_500"
app:boxStrokeWidth="1dp"
app:boxStrokeWidthFocused="2dp"
android:layout_marginBottom="24dp">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/ticketQuantity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:textColor="@color/black"
android:textColorHint="@color/gray_500" />
</com.google.android.material.textfield.TextInputLayout>
<!-- 计算费用按钮 -->
<com.google.android.material.button.MaterialButton
android:id="@+id/calculateButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="计算费用"
android:textColor="@color/white"
android:backgroundTint="@color/purple_500"
android:layout_marginBottom="16dp"
app:iconTint="@color/white"
app:iconPadding="8dp"
app:cornerRadius="8dp"
android:elevation="4dp" />
<!-- 显示费用结果 -->
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardBackgroundColor="@color/purple_100"
app:cardCornerRadius="8dp"
app:cardElevation="4dp"
android:layout_marginBottom="16dp">
<TextView
android:id="@+id/resultText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="费用:0元"
android:textSize="18sp"
android:textColor="@color/purple_800"
android:gravity="center"
android:padding="16dp" />
</androidx.cardview.widget.CardView>
<!-- 缴费按钮 -->
<com.google.android.material.button.MaterialButton
android:id="@+id/payButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="缴费"
android:textColor="@color/white"
android:backgroundTint="@color/purple_500"
app:iconTint="@color/white"
app:iconPadding="8dp"
app:cornerRadius="8dp"
android:elevation="4dp" />
`

浙公网安备 33010602011771号