咱们来就用OnClickListener 接口, 作为匿名内部类时 看看怎么写
/**
* Interface definition for a callback to be invoked when a view is clicked.
*/
public interface OnClickListener {
/**
* Called when a view has been clicked.
*
* @param v The view that was clicked.
*/
void onClick(View v);
}
首先看 java怎么写
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
那么kotlin 怎么写呢
方式一: 这个是最接近Java 写法的方式
tv_go.setOnClickListener(object : View.OnClickListener {
override fun onClick(v: View?) {
finish()
}
})