简体   繁体   English

在 Android 自定义组件上添加 setOnClickListener

[英]Add setOnClickListener on Android custom component

When making a custom component for Android, as far as I understood, I need:在为 Android 制作自定义组件时,据我所知,我需要:

  • one xml file with the layout for the component;一个包含组件布局的 xml 文件;
  • one java class.一个java类。

Tried to make one, just for practice.试着做一个,只是为了练习。 Use two progressbars and one button in the XML, and create the class, than tried out in my main activity, worked fine.在 XML 中使用两个进度条和一个按钮,并创建该类,比在我的主要活动中尝试过,效果很好。

But now I'm loking how to set OnClickListener on my button through the activity, and in this part I'm lost.但是现在我正在寻找如何通过活动在我的按钮上设置 OnClickListener ,在这部分我迷路了。

My loading_button.xml:我的 loading_button.xml:

<merge
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<RelativeLayout
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <ProgressBar
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="4dp"
        android:progress="0"
        android:id="@+id/pg_top" />

    <Button
        android:text="Button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/btn_ok"
        android:background="@color/colorPrimary"
        android:textColor="#ffffff"
        android:layout_below="@+id/pg_top"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <ProgressBar
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="5dp"
        android:progress="0"
        android:id="@+id/pg_bottom"
        android:layout_below="@+id/btn_ok"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

</RelativeLayout>

</merge>

Here the LoadingButton.java:这里是 LoadingButton.java:

public class LoadingButton extends RelativeLayout{

private ProgressBar pbTop;
private ProgressBar pbBottom;
private Button btnOk;

private int pbTopProgress = 0;
private int pbBottomProgress = 0;

public LoadingButton(Context context, AttributeSet attrs) {
    super(context, attrs);
    View view = View.inflate(context, R.layout.loading_button, this);

    btnOk = (Button) view.findViewById(R.id.btn_ok);
    pbTop = (ProgressBar) view.findViewById(R.id.pg_top);
    pbBottom = (ProgressBar) view.findViewById(R.id.pg_bottom);

    btnOk.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            // Doing stuff ok
        }
    });

}// LoadingButton
}

The setOnClickListener in the class works, but how can I set it from my main activity?类中的 setOnClickListener 有效,但如何从我的主要活动中设置它?

MainActivity:主要活动:

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // My new component (it's id in activity_main.xml is 'lb') 
    LoadingButton lb = (LoadingButton) findViewById(R.id.lb);

    // QUESTION - how to make something like this with btnOk:
    lb.btnOkSetOnClickListener(new OnClickListener() {
       @Override
       public void onClick(View v) {
          // Do stuff
       }
    });
  }
}

If this is possible, how?如果这是可能的,如何?

If not, what is the right approach to make a custom component and set clickListeners on specific element of this component?如果没有,制作自定义组件并在该组件的特定元素上设置 clickListeners 的正确方法是什么?

Best approach to handle all view click in LoadingButton (For custom view).处理 LoadingButton 中所有视图单击的最佳方法(用于自定义视图)。 And create interface.并创建界面。

public class LoadingButton extends RelativeLayout {


    public interface OnLoadingButtonClickListener{
        void onLoadingButtonClickListener();
    }

    private ProgressBar pbTop;
    private ProgressBar pbBottom;
    private Button btnOk;

    private int pbTopProgress = 0;
    private int pbBottomProgress = 0;
    private OnLoadingButtonClickListener mONOnLoadingButtonClickListener;

    public LoadingButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        View view = View.inflate(context, R.layout.loading_button, this);

        btnOk = (Button) view.findViewById(R.id.btn_ok);
        pbTop = (ProgressBar) view.findViewById(R.id.pg_top);
        pbBottom = (ProgressBar) view.findViewById(R.id.pg_bottom);

        btnOk.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mONOnLoadingButtonClickListener != null){
                    mONOnLoadingButtonClickListener.onLoadingButtonClickListener();
                }
            }
        });

    }// LoadingButton

    public void setOnLoadingClickListener(OnLoadingButtonClickListener onLoadingClickListener){
        mONOnLoadingButtonClickListener = onLoadingClickListener;
    }


}

And implement OnLoadingButtonClickListener in your activity.并在您的活动中实现 OnLoadingButtonClickListener。

public class MainActivity extends AppCompatActivity implements LoadingButton.OnLoadingButtonClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // My new component (it's id in activity_main.xml is 'lb') 
        LoadingButton lb = (LoadingButton) findViewById(R.id.lb);
        lb.setOnLoadingClickListener(this);

    }

    @Override
    public void onLoadingButtonClickListener() {
        //do your stuff on ok button click
    }
}

Override setOnClickListener(OnClickListener listener) in your custom Java class.在您的自定义 Java 类中覆盖setOnClickListener(OnClickListener listener) Inside it, write:在里面写:

btnOk.setOnClickListener(listener); btnOk.setOnClickListener(listener);

where listener is an argument of your custom view's setOnClickListener() function.其中 listener 是自定义视图的setOnClickListener()函数的参数。

That will make your whole view clickable and click on your button will be performed.这将使您的整个视图可点击并点击您的按钮。 Of couse add android:clickable="true" to your custom view's root layout.当然,将android:clickable="true"到自定义视图的根布局中。

In the component在组件中

class Example @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyle: Int = 0,
defStyleRes: Int = 0) : ConstraintLayout(context, attrs, defStyle) {

var clickListener: () -> Unit = { }

init { }

private fun initListeners() {
    binding.button.onClick {
        clickListener()
    }
}

In the activity / fragment在活动/片段中

component.clickListener = { }

And the best part, you can send data on the component and receive it in the landa最好的部分是,您可以在组件上发送数据并在landa 中接收它

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

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