简体   繁体   中英

Reusable button

I am trying to create a reusable button by extending the Button class. I am just trying something basic with setting the background color and the text of the button. I am a little confused on where/how to call a init method within the class that extends Button. I know I can set these fields with a style but I am hoping there is a way to do it within the class. I wish to make if conditions within the class to determine if the button will change from transparent, color, shape, and other attributes.

Here is the class,

    public class SVButton extends Button {

    public SVButton(Context context) {
        super(context);

    }

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

    public SVButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private void init(Context context) {
        Button SVColorButton = new Button(getContext());
        SVColorButton.setBackgroundColor(ContextCompat.getColor(context, R.color.colorPrimary));
        SVColorButton.setText("Push Me");
    }
}

Here is the XML I am calling the customWidget Button,

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context="com.example.agar098.atomicdesigndemos.MainActivity">

    <com.example.agar098.atomicdesigndemos.CustomWidgets.SVButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</RelativeLayout>

I think this is what you were going for.

In other words, you only made a new Button(); in your class, you didn't actually extend anything.

public class SVButton extends Button {
    private Context mContext;

    public SVButton(Context context) {
        super(context);
        this.mContext = context;
        init();
    }

    public SVButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        init();
    }

    public SVButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
        init();
    }

    private void init() {
        this.setBackgroundColor(ContextCompat.getColor(mContext, R.color.colorPrimary));
        this.setText("Push Me");
    }
}

1. When you are not defining in your xml and directly use button like below.

    final SVButton svButton = new SVButton(this);

Below constructor will call

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

2. When you define button in xml, like below

 <com.example.agar098.atomicdesigndemos.CustomWidgets.SVButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

That time below constructor will call

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

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