简体   繁体   English

Android 按钮点击覆盖

[英]Android button onclick override

I would like to create a CustomButton which has a predefined onClick .我想创建一个具有预定义onClickCustomButton In fact, my object would do the same job than事实上,我的对象会做同样的工作

CustomButton mButton = getViewById(..);
mButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
  show_something()
}

Is there a way to embed the Listener into the CustomButton object that inherits from Button ?有没有办法将Listener嵌入到继承自 Button 的 CustomButton 对象中? What I would like is to create a CustomButton in my layout XML file, and not having to mention this button in my activity, which would give:我想要的是在我的布局 XML 文件中创建一个CustomButton ,而不必在我的活动中提到这个按钮,它会给出:

main.xml:主文件:

<LinearLayout xmlns:"...">
     <com.mypackage.view.CustomButton
         (attributes)/>
</LinearLayout>

CustomButton.java:自定义按钮.java:

class CustomButton extends Button implements... {

@Override
OnClick (or something like that, that's the core of my question, what to put here)
}

myActivity.java我的活动.java

public class myActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    }
}

Thanks a lot.非常感谢。

You were really close:你真的很接近:

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class CustomButton extends Button implements OnClickListener{

    public CustomButton(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    public CustomButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public CustomButton(Context context) {
        super(context);
        init();
    }

    private void init(){
        setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        // Do something
    }

}

In your button class just implement:在您的按钮类中只需实现:

@Override
public void onClick(View v) {
    showSomething();
}

Or if you want more granular control:或者,如果您想要更精细的控制:

@Override
public boolean onTouchEvent(MotionEvent event) {
    showSomething();
}

You can either implement your click logic in the method by checking the event.getAction(), or send it to a GestureDetector to figure out when a click has been performed.您可以通过检查 event.getAction() 在方法中实现您的点击逻辑,或者将其发送到 GestureDetector 以判断何时执行了点击。

Use that code:使用该代码:

class CustomButton extends Button implements View.OnClickListener{
     CustomButton(Context c, AttributeSet attrs) {
         ...
         setOnClickListener(this);
         ...
     }

    @override
    public void onClick(View v){
        //add your code here
    }
}

When you are using Kotlin:当您使用 Kotlin 时:

class TemporyActionButton constructor(
  context: Context,
  attrs: AttributeSet
) : RelativeLayout(context, attrs), View.OnClickListener {

  private val button: MaterialButton
  private var event: CustomOnClickListener? = null

  init {
    inflate(context, attrs)

    button = findViewById(R.id.button_action)
    button.setOnClickListener(this)

    context.theme.obtainStyledAttributes(
      attrs, R.styleable.TemporyActionButton, 0, 0
    ).use {
      // styling
    }
  }

  override fun onClick(v: View) {
    event?.customOnClick(v)
  }

  fun setCustomClickListener(event: CustomOnClickListener) {
    this.event = event
  }

  private fun inflate(context: Context, attrs: AttributeSet) {
    LayoutInflater.from(context).inflate(R.layout.layout_button_action, this, true)
    layoutParams = LinearLayout.LayoutParams(context, attrs)
  }
}

fun interface CustomOnClickListener {
  fun customOnClick(view: View)
}

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

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