java - 需要从 Java 到 Kotlin 的正确转换的建议

标签 java android kotlin

Android Studio 有一个自动化的 Java 到 Kotlin 转换器。在某些情况下,它不能顺利工作,这意味着需要一些手动工作。

这是一个例子:

private TextView[] dots;

 private void addBottomDots(int currentPage) {
        dots = new TextView[layouts.length];

        int[] colorsActive = getResources().getIntArray(R.array.array_dot_active);
        int[] colorsInactive = getResources().getIntArray(R.array.array_dot_inactive);

        dotsLayout.removeAllViews();
        for (int i = 0; i < dots.length; i++) {
            dots[i] = new TextView(getContext());
            dots[i].setText(Html.fromHtml("&#8226;"));
            dots[i].setTextSize(35);
            dots[i].setTextColor(colorsInactive[currentPage]);
            dotsLayout.addView(dots[i]);
        }

        if (dots.length > 0)
            dots[currentPage].setTextColor(colorsActive[currentPage]);
    }

自动转换+手动调整的结果是:

private var dots: Array<TextView?>? = null  

    private fun addBottomDots(currentPage: Int) {
        dots = arrayOfNulls(layouts!!.size)

        val colorsActive = getResources().getIntArray(R.array.array_dot_active)
        val colorsInactive = getResources().getIntArray(R.array.array_dot_inactive)

        dotsLayout!!.removeAllViews()
        for (i in dots!!.indices) {
            dots[i] = TextView(getContext()) // part A
            dots!![i].text = Html.fromHtml("&#8226;") // part B
            dots!![i].textSize = 35f
            dots!![i].setTextColor(colorsInactive[currentPage])
            dotsLayout!!.addView(dots!![i])
        }

        if (dots!!.size > 0)
            dots!![currentPage].setTextColor(colorsActive[currentPage])
    }   

在 A 部分,Android studio 给出了以下错误消息:

Only safe (?.) or non-null asserted (!!.) calls are allowed on a nullable receiver of type TextView?

B 部分:

Smart cast to 'Array' is impossible, because 'dots' is a mutable property that could have been changed by this time.

另一个案例:

public class MyViewPagerAdapter extends PagerAdapter {
        private LayoutInflater layoutInflater;

        public MyViewPagerAdapter() {
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            layoutInflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            View view = layoutInflater.inflate(layouts[position], container, false);
            container.addView(view);

            return view;
        }
}

转换成:

inner class MyViewPagerAdapter : PagerAdapter() {
        private var layoutInflater: LayoutInflater? = null

        override fun instantiateItem(container: ViewGroup, position: Int): Any {
            layoutInflater = getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE) // part C

            val view = layoutInflater!!.inflate(layouts!![position], container, false)
            container.addView(view)

            return view
        }

在 C 部分:

Type mismatch. Required: LayoutInflater? Found: Any!

如何解决这个问题?

最佳答案

尝试以下操作

private var dots: ArrayList<TestView> = ArrayList()

private fun addBottomDots(currentPage: Int) {
    val size = layouts?.size ?: 0

    val colorsActive = getResources().getIntArray(R.array.array_dot_active)
    val colorsInactive = getResources().getIntArray(R.array.array_dot_inactive)

    dotsLayout?.removeAllViews()

    for (i in 0 until size) {
        val textView = TextView(getContext()) // part A
        textView.text = Html.fromHtml("&#8226;") // part B
        textView.textSize = 35f
        textView.setTextColor(colorsInactive[currentPage])
        dots.add(textView)
        dotsLayout?.addView(dots[i])
    }

    if (dots.size > 0)
        dots[currentPage].setTextColor(colorsActive[currentPage])
}

关于java - 需要从 Java 到 Kotlin 的正确转换的建议,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51089926/

相关文章:

android - 在 Android 中,如何创建 Material 设计文档指定的轮廓下拉菜单(微调器)?

android - Okhttp Authenticator addHeader() vs header()

java - 在这种情况下,Observable 会不会因为被垃圾收集而中途停止发射?

java - readObject() 在特定对象上阻塞

java - 为什么 Java 必须采用类型删除来保持向后兼容性?

Android-从 URI 获取文件路径?

android - 如何在 Kotlin Android 的 DatePicker 中禁用 future 日期

node.js - Firebase 实时数据库规则拒绝权限

java - 正则表达式匹配 Java 字符串开头的一个单词

java - AlarmManager 不会启动服务