Android Studio将代码从Kotlin转换为Java

时间:2018-11-21 05:44:09

标签: java android

我是android studio的新手,但来自通过大学学习Eclipse来学习Java。我一直在遵循一个用kotlin编写的简单教程,但是我想用Java创建它(尽管从我学到的kotlin重复性不高)。我想将以下代码从kotlin转换为java:

val rollButton = findViewByID<Button>(R.id.rollButton)
val resultsTextView= findViewByID<TextView>(R.id.resultsTextView)
val seekBar= findViewByID<seekBar>(R.id.seekBar)

rollButton.setOnClickListener {
    val rand = Random().nextInt(seekBar.progress) + 1
    resultsTextView.text = rand.toString()

4 个答案:

答案 0 :(得分:1)

在Android Studio中,您可以从菜单>工具> Kotlin->将Kotlin反编译为Java来将Kotlin反编译为Java。

答案 1 :(得分:0)

你在这里

Button rollButton=findViewById(R.id.rollButton);
TextView resultTexView=findViewById(R.id.resultsTextView);
SeekBar seekBar=findViewById(R.id.seekBar);

button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int rand = Random().nextInt(seekBar.progress) + 1
    resultsTextView.setText(rand.toString());
            }
        });

您正在使用按钮单击侦听器内部的resultsTextView.setText,因此可能需要在类内部进行全局声明。

答案 2 :(得分:0)

这是一个简单的转换,只需执行;-)

Button rollButton = findViewByID(R.id.rollButton);
TextView resultsTextView = findViewByID(R.id.resultsTextView);
seekBar seekBar = findViewByID(R.id.seekBar);

rollButton.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        int rand = Random().nextInt(seekBar.progress) + 1 ;
        resultsTextView.setText(String.valueOf(rand));
    }
});

答案 3 :(得分:0)

Button rollButton = findViewById(R.id.rollButton);
TextView resultsTextView = findViewById(R.id.resultsTextView);
Seekbar seekbar = findViewById(R.id.seekbar);

rollButton.setOnClickListener(new View.OnClickListener {
    @Override
    public void onClick (View view){
        int rand = Random().nextInt(seekBar.progress) + 1;
        resultsTextView.setText(String.valueOf(rand));
    }
});