简体   繁体   中英

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:

An OnClickListener set for a button:

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:

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.

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:

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:

internal var mTakePicSOnClickListener: Button.OnClickListener

I don't know to create this in Kotlin and the translator gave me that.

Does anyone knows if a should just translate that, or take another approach?

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.

The Kotlin style would be to use a 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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