Before that, I used the standard android buttons, but I needed to make a custom button and I wrote such a class.
public class MyButton extends androidx.appcompat.widget.AppCompatButton {
private boolean showIfNotAccessibleByRole;
private final AccessUrlUtils accessUrlUtils = new AccessUrlUtils();
ToastCreator toastCreator = new ToastCreator();
public MyButton(Context context) {
super(context);
init(null, 0);
}
public MyButton(Context context, AttributeSet attrs) {
super(context, attrs);
init(attrs, 0);
}
public MyButton(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(attrs, defStyleAttr);
}
private void init(AttributeSet attrs, int defStyle) {
// Load attributes
final TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.MyButton, defStyle, 0);
showIfNotAccessibleByRole = a.getBoolean(R.styleable.MyButton_showIfNotAccessibleByRole, true);
a.recycle();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (!showIfNotAccessibleByRole) {
if (!accessUrlUtils.checkRoleBasedAccessByView(this)) {
setVisibility(GONE);
}
}
}
@Override
public void setOnClickListener(@Nullable OnClickListener l) {
OnClickListener onClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
if (accessUrlUtils.checkRoleBasedAccessByView(v)) {
if (accessUrlUtils.checkSubscriptionBasedAccessByView(v)) {
if (l != null) {
l.onClick(v);
}
}
else {
SubscriptionDialog subscriptionDialog = new SubscriptionDialog(getContext());
subscriptionDialog.show();
}
}
else {
toastCreator.createToast(getContext(), (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE), getResources().getStringArray(R.array.errors)[17], getActivity());
}
}
};
super.setOnClickListener(onClickListener);
}
@Override
public void setOnLongClickListener(@Nullable OnLongClickListener l) {
OnLongClickListener onLongClickListener = new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
if (accessUrlUtils.checkRoleBasedAccessByView(v, ClickType.LONG)) {
if (accessUrlUtils.checkSubscriptionBasedAccessByView(v, ClickType.LONG)) {
if (l != null) {
return l.onLongClick(v);
}
}
else {
SubscriptionDialog subscriptionDialog = new SubscriptionDialog(getContext());
subscriptionDialog.show();
}
}
else {
toastCreator.createToast(getContext(), (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE), getResources().getStringArray(R.array.errors)[17], getActivity());
}
return false;
}
};
super.setOnLongClickListener(onLongClickListener);
}
private Activity getActivity() {
Context context = getContext();
while (context instanceof ContextWrapper) {
if (context instanceof Activity) {
return (Activity)context;
}
context = ((ContextWrapper)context).getBaseContext();
}
return null;
}
}
However, when using this class, the styles that I wrote for the buttons are not applied, and the buttons look like standard buttons in android. How can I fix this without destroing my class?
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743750448a4500798.html
评论列表(0条)