gpt4 book ai didi

java - 我无法将 Java 完美地转换为 Kotlin

转载 作者:行者123 更新时间:2023-11-29 22:48:51 26 4
gpt4 key购买 nike

我是 Kotlin 和 Java 的新手。

我一直在使用 Kotlin 构建 GPA 成绩计算器并在 Android Studio 上用图表显示它们。

我使用 hellochart 库。

我无法将示例从 java 转换为 kotlin。

这是我尝试使用的示例链接 https://www.codingdemos.com/draw-android-line-chart/

    package com.codingdemos.codingdemos;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import java.util.ArrayList;
import java.util.List;
import lecho.lib.hellocharts.model.Axis;
import lecho.lib.hellocharts.model.AxisValue;
import lecho.lib.hellocharts.model.Line;
import lecho.lib.hellocharts.model.LineChartData;
import lecho.lib.hellocharts.model.PointValue;
import lecho.lib.hellocharts.model.Viewport;
import lecho.lib.hellocharts.view.LineChartView;
public class MainActivity extends AppCompatActivity {
LineChartView lineChartView;
String[] axisData = {"Jan", "Feb", "Mar", "Apr", "May", "June", "July",
"Aug", "Sept","Oct", "Nov", "Dec"};
int[] yAxisData = {50, 20, 15, 30, 20, 60, 15, 40, 45, 10, 90, 18};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lineChartView = findViewById(R.id.chart);
List yAxisValues = new ArrayList();
List axisValues = new ArrayList();

Line line = new Line(yAxisValues).setColor(Color.parseColor("#9C27B0"));
for (int i = 0; i < axisData.length; i++) {
axisValues.add(i, new AxisValue(i).setLabel(axisData[i]));
}

for (int i = 0; i < yAxisData.length; i++) {
yAxisValues.add(new PointValue(i, yAxisData[i]));
}

List lines = new ArrayList();
lines.add(line);

LineChartData data = new LineChartData();
data.setLines(lines);

Axis axis = new Axis();
axis.setValues(axisValues);
axis.setTextSize(16);
axis.setTextColor(Color.parseColor("#03A9F4"));
data.setAxisXBottom(axis);

Axis yAxis = new Axis();
yAxis.setName("Sales in millions");
yAxis.setTextColor(Color.parseColor("#03A9F4"));
yAxis.setTextSize(16);
data.setAxisYLeft(yAxis);

lineChartView.setLineChartData(data);
Viewport viewport = new Viewport(lineChartView.getMaximumViewport());
viewport.top = 110;
lineChartView.setMaximumViewport(viewport);
lineChartView.setCurrentViewport(viewport);
}

}

请帮助我将它正确无误地转换为 Kotlin :(。谢谢

最佳答案

试试这个代码..希望它对你有用

class MainActivity : AppCompatActivity() {
private var axisData: Array<String> = arrayOf(
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"June",
"July",
"Aug",
"Sept",
"Oct",
"Nov",
"Dec"
)
private var yAxisData: IntArray = intArrayOf(50, 20, 15, 30, 20, 60, 15, 40, 45, 10, 90, 18)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val yAxisValues:ArrayList<PointValue> = ArrayList()
val axisValues:ArrayList<AxisValue> = ArrayList()

val line = Line(yAxisValues).setColor(Color.parseColor("#9C27B0"))
for (i in axisData.indices) {
axisValues.add(i, AxisValue(i.toFloat()).setLabel(axisData[i]))
}

for (i in yAxisData.indices) {
yAxisValues.add(PointValue(i.toFloat(), yAxisData[i].toFloat()))
}

val lines:ArrayList<Line> = ArrayList()
lines.add(line)

val data = LineChartData()
data.lines = lines

val axis = Axis()
axis.values = axisValues
axis.textSize = 16
axis.textColor = Color.parseColor("#03A9F4")
data.axisXBottom = axis

val yAxis = Axis()
yAxis.name = "Sales in millions"
yAxis.textColor = Color.parseColor("#03A9F4")
yAxis.textSize = 16
data.axisYLeft = yAxis

chart.lineChartData = data
val viewport = Viewport(chart.maximumViewport)
viewport.top = 110f
chart.maximumViewport = viewport
chart.currentViewport = viewport
}
}

关于java - 我无法将 Java 完美地转换为 Kotlin,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58115056/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com