用android studio做一个汽车推荐app的实现代码
时间: 2023-12-03 20:04:54 浏览: 141
这里是一个简单的汽车推荐App的实现代码示例,它是基于Android Studio开发的。这个App可以根据用户的需求和偏好来推荐适合他们的汽车品牌和型号。
*布局文件:activity_main.xml*
```xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="请选择您的需求和偏好:" />
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="您的预算(万元):" />
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:text="20" />
<TextView
android:id="@+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="您的用途:" />
<RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RadioButton
android:id="@+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="城市代步" />
<RadioButton
android:id="@+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="长途出行" />
<RadioButton
android:id="@+id/radioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="越野" />
</RadioGroup>
<TextView
android:id="@+id/textView4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="您的喜好:" />
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="外观" />
<CheckBox
android:id="@+id/checkBox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="车内空间" />
<CheckBox
android:id="@+id/checkBox3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="燃油经济性" />
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="推荐汽车" />
<TextView
android:id="@+id/textView5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="推荐结果:" />
<TextView
android:id="@+id/textView6"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
```
*Java代码:MainActivity.java*
```java
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private EditText editText1;
private RadioGroup radioGroup1;
private CheckBox checkBox1;
private CheckBox checkBox2;
private CheckBox checkBox3;
private TextView textView6;
private Button button1;
private List<Car> carList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initData();
initEvent();
}
private void initView() {
editText1 = findViewById(R.id.editText1);
radioGroup1 = findViewById(R.id.radioGroup1);
checkBox1 = findViewById(R.id.checkBox1);
checkBox2 = findViewById(R.id.checkBox2);
checkBox3 = findViewById(R.id.checkBox3);
textView6 = findViewById(R.id.textView6);
button1 = findViewById(R.id.button1);
}
private void initData() {
//初始化汽车数据
carList = new ArrayList<>();
carList.add(new Car("Benz", "C-Class", 25, "city", "appearance", "fuel"));
carList.add(new Car("BMW", "3 Series", 24, "city", "appearance", "fuel"));
carList.add(new Car("Audi", "A4", 23, "city", "appearance", "fuel"));
carList.add(new Car("Lexus", "ES", 26, "city", "interior", "fuel"));
carList.add(new Car("Honda", "Accord", 20, "city", "interior", "fuel"));
carList.add(new Car("Toyota", "Camry", 18, "city", "interior", "fuel"));
carList.add(new Car("Jeep", "Wrangler", 35, "off-road", "appearance", "fuel"));
carList.add(new Car("Land Rover", "Defender", 30, "off-road", "appearance", "fuel"));
carList.add(new Car("Toyota", "Land Cruiser", 32, "off-road", "interior", "fuel"));
carList.add(new Car("Tesla", "Model S", 70, "long distance", "appearance", "electric"));
carList.add(new Car("Porsche", "Taycan", 80, "long distance", "appearance", "electric"));
carList.add(new Car("Nissan", "Leaf", 40, "long distance", "interior", "electric"));
}
private void initEvent() {
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String budgetStr = editText1.getText().toString();
if (budgetStr.isEmpty()) {
textView6.setText("请填写预算!");
return;
}
int budget = Integer.parseInt(budgetStr);
String usage = "";
int usageId = radioGroup1.getCheckedRadioButtonId();
if (usageId == R.id.radioButton1) {
usage = "city";
} else if (usageId == R.id.radioButton2) {
usage = "long distance";
} else if (usageId == R.id.radioButton3) {
usage = "off-road";
} else {
textView6.setText("请选择用途!");
return;
}
String preference = "";
if (checkBox1.isChecked()) {
preference += "appearance";
}
if (checkBox2.isChecked()) {
preference += "interior";
}
if (checkBox3.isChecked()) {
preference += "fuel";
}
if (preference.isEmpty()) {
textView6.setText("请选择偏好!");
return;
}
List<Car> recommendList = new ArrayList<>();
for (Car car : carList) {
if (car.getUsage().equals(usage) && car.getPrice() <= budget && car.getPreference().indexOf(preference) >= 0) {
recommendList.add(car);
}
}
if (recommendList.size() > 0) {
StringBuilder sb = new StringBuilder();
for (Car car : recommendList) {
sb.append(car.getBrand()).append(" ").append(car.getModel()).append("\n");
}
textView6.setText(sb.toString());
} else {
textView6.setText("没有找到合适的汽车!");
}
}
});
}
private class Car {
private String brand;
private String model;
private int price;
private String usage;
private String preference;
private String fuelType;
public Car(String brand, String model, int price, String usage, String preference, String fuelType) {
this.brand = brand;
this.model = model;
this.price = price;
this.usage = usage;
this.preference = preference;
this.fuelType = fuelType;
}
public String getBrand() {
return brand;
}
public String getModel() {
return model;
}
public int getPrice() {
return price;
}
public String getUsage() {
return usage;
}
public String getPreference() {
return preference;
}
public String getFuelType() {
return fuelType;
}
}
}
```
在这个示例中,我们使用了EditText、RadioGroup、CheckBox、Button和TextView等常见的UI组件来实现汽车推荐的功能。我们在Java代码中定义了一个Car类,用于表示汽车的品牌、型号、价格、用途、偏好和燃料类型等属性。在初始化时,我们创建了一个包含汽车数据的List,并在点击“推荐汽车”按钮时根据用户的需求和偏好来筛选出符合条件的汽车,并在TextView中显示推荐结果。
阅读全文