简体   繁体   English

从Java转换为Kotlin OnClickListener

[英]Translate from Java to Kotlin OnClickListener

I am having trouble on the ways this code could be translated. 我在翻译此代码的方式上遇到麻烦。 So I have a few code in JAVA, as follows: 因此,我在JAVA中有一些代码,如下所示:

An OnClickListener set for a button: 为按钮设置的OnClickListener:

Button.OnClickListener mTakePicSOnClickListener = 
    new Button.OnClickListener() {
    @Override
    public void onClick(View v) {
        dispatchTakePictureIntent(ACTION_TAKE_PHOTO_S);
    }
};

Then I have the button itself, and a function to apply that OnClickListener to that button: 然后,我有了按钮本身,以及一个将该OnClickListener应用于该按钮的函数:

Button picSBtn = (Button) findViewById(R.id.btnIntendS);
setBtnListenerOrDisable( 
            picSBtn, 
            mTakePicSOnClickListener,
            MediaStore.ACTION_IMAGE_CAPTURE
    );

Then I have this function: 然后我有这个功能:

private void setBtnListenerOrDisable( 
        Button btn, 
        Button.OnClickListener onClickListener,
        String intentName
) {
    if (isIntentAvailable(this, intentName)) {
        btn.setOnClickListener(onClickListener);            
    } else {
        btn.setText( 
            getText(R.string.cannot).toString() + " " + btn.getText());
        btn.setClickable(false);
    }
}

So I asked to Android Studio to translate the code to kotlin. 因此,我要求Android Studio将代码翻译为kotlin。

It has just some minor changes to make, but in this case it does not work. 它只需要进行一些细微的更改,但是在这种情况下,它不起作用。 It seems that there is a big difference on the approach to that. 似乎在处理方法上有很大的不同。

So in Kotlin the same portions of code are as this: 因此在Kotlin中,相同的代码部分是这样的:

internal var mTakePicSOnClickListener: Button.OnClickListener = object : Button.OnClickListener {
    override fun onClick(v: View) {
        dispatchTakePictureIntent(ACTION_TAKE_PHOTO_S)
    }
}

After change a little bit of code I have: 更改一些代码后,我得到:

val picSBtn = btnIntendS as Button
    setBtnListenerOrDisable(
            picSBtn,
            mTakePicSOnClickListener,
            MediaStore.ACTION_IMAGE_CAPTURE
    )

Then the function: 然后函数:

private fun setBtnListenerOrDisable(
        btn: Button,
        onClickListener: View.OnClickListener,
        intentName: String
) {
    if (isIntentAvailable(this, intentName)) {
        btn.setOnClickListener(onClickListener)
    } else {
        btn.text = getText(R.string.cannot).toString() + " " + btn.text
        btn.isClickable = false
    }
}

The error AndroidStudio is pointing is at the creation of the "generic" OnClickListener: AndroidStudio指向的错误是“通用” OnClickListener的创建:

internal var mTakePicSOnClickListener: Button.OnClickListener

I don't know to create this in Kotlin and the translator gave me that. 我不知道如何在Kotlin中创建此代码,翻译员给了我。

Does anyone knows if a should just translate that, or take another approach? 有谁知道a应该只是翻译它,还是采取其他方法?

Thanks a lot! 非常感谢!

When I check in android studio, there seems to be no interface Button.OnClickListener, just change it to View.OnClickListener and you should be good to go. 当我在android studio中签入时,似乎没有接口Button.OnClickListener,只需将其更改为View.OnClickListener即可。

The Kotlin style would be to use a lambda. Kotlin风格是使用lambda。 For example : 例如 :

btn.setOnClickListener { v -> doSomething() }

To pass this as an argument, just store it as 要将其作为参数传递,只需将其存储为

val listener={v:View -> doSomething()}

or 要么

val listener:((View) -> Unit)={v -> doSomething()}

To pass it to a function, the parameters should look like this: 要将其传递给函数,参数应如下所示:

fun foo(listener:((View) -> Unit)){
    btn.setOnClickListener(listener)
}

val l={ v:View -> Log.d("View", "click")}

foo(l)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM