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

时间:2019-09-26 10:43:06

标签: java android android-layout kotlin converters

我是Kotlin和Java的新手。

我一直在构建GPA等级计算器,并在带有Kotlin的Android Studio上用图形显示它们。

我使用hellochart lib。

我无法将示例从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,没有错误:(。 谢谢

4 个答案:

答案 0 :(得分:1)

尝试此代码。希望它对您有用

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
}
}

答案 1 :(得分:0)

我们可以轻松地将Java项目转换为Kotlin 请按照以下步骤操作

  

将单个Java文件转换为Kotlin

打开Java类/从项目中选择>转到工具栏>单击代码>选择“将Java文件转换为Kotlin文件”

  

将Java项目转换为Kotlin

选择应用程序表单侧菜单(如屏幕截图所示)>转到工具栏>单击代码>选择“将Java文件转换为kotlin文件”

  

快捷键是Ctrl + Alt + Shift + K

kotlin文件中的某些时间显示一些错误,要解决这些错误,请单击错误行,然后按 Alt + Enter 以显示带有解决提示的错误消息 Convert Single java file to kotlin Convert java project into kotlin

答案 2 :(得分:0)

通常,我习惯使用this

将JAVA代码转换为kotlin

选择从Java转换(从右上方)

使用它,您的MainActivity看起来像这样。

package com.codingdemos.codingdemos
import android.graphics.Color
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import java.util.ArrayList
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
class MainActivity:AppCompatActivity() {
  internal var lineChartView:LineChartView
  internal var axisData = arrayOf<String>("Jan", "Feb", "Mar", "Apr", "May", "June", "July", "Aug", "Sept", "Oct", "Nov", "Dec")
  internal var yAxisData = intArrayOf(50, 20, 15, 30, 20, 60, 15, 40, 45, 10, 90, 18)
  protected fun onCreate(savedInstanceState:Bundle) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    lineChartView = findViewById(R.id.chart)
    val yAxisValues = ArrayList()
    val axisValues = ArrayList()
    val line = Line(yAxisValues).setColor(Color.parseColor("#9C27B0"))
    for (i in axisData.indices)
    {
      axisValues.add(i, AxisValue(i).setLabel(axisData[i]))
    }
    for (i in yAxisData.indices)
    {
      yAxisValues.add(PointValue(i, yAxisData[i]))
    }
    val lines = ArrayList()
    lines.add(line)
    val data = LineChartData()
    data.setLines(lines)
    val axis = Axis()
    axis.setValues(axisValues)
    axis.setTextSize(16)
    axis.setTextColor(Color.parseColor("#03A9F4"))
    data.setAxisXBottom(axis)
    val yAxis = Axis()
    yAxis.setName("Sales in millions")
    yAxis.setTextColor(Color.parseColor("#03A9F4"))
    yAxis.setTextSize(16)
    data.setAxisYLeft(yAxis)
    lineChartView.setLineChartData(data)
    val viewport = Viewport(lineChartView.getMaximumViewport())
    viewport.top = 110
    lineChartView.setMaximumViewport(viewport)
    lineChartView.setCurrentViewport(viewport)
  }
}

答案 3 :(得分:0)

我试图在这里转换成惯用的Kotlin-

let mut mat = Array2::<f64>::zeros((1000, 1000);
Zip::from(mat.axis_iter(Axis(0))).par_apply(|row| {

    row.into_par_iter().map(...).collect() ;
});