我是Android开发的新手。我想制作一个自定义的ImageButton,当您为我的项目点击它时可以切换颜色。我想正确地做到这一点,并使按钮本身能够跟踪其自身的状态,因此我扩展了类并在下面制作了ToggleImageButton。
package com.ktpackages.elements.buttons
import android.annotation.TargetApi
import android.content.Context
import android.graphics.Color
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.ImageButton
import com.google.android.libraries.places.internal.it
import com.ktpackages.tacosolo.R
class ToggleImageButton : ImageButton {
private var toggle_state = true
@JvmOverloads
constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) :
super(context, null, defStyleAttr)
{
// Add onColor and offColor to attrs.
setAttrs(attrs)
}
@TargetApi(21)
constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int, defStyleRes: Int) :
super(context, attrs, defStyleAttr, defStyleRes) {
setAttrs(attrs)
}
private fun setAttrs(attrs: AttributeSet?) {
attrs?.let {
val a = context.obtainStyledAttributes(
it,
R.styleable.custom_component_attributes, 0, 0
)
var offColor = resources.getText(
a.getResourceId(
R.styleable
.custom_component_attributes_toggle_color_off,
R.color.design_default_color_primary
)
)
var onColor = resources.getText(
a.getResourceId(
R.styleable
.custom_component_attributes_toggle_color_off,
R.color.design_default_color_primary_dark
)
)
a.recycle()
}
}
var toggled: Boolean
get() {
return toggle_state
}
set(value) {
if (toggle_state != value as Boolean) {
toggle()
}
}
fun toggle() {
toggle_state = !toggle_state
if (toggle_state) {
setBackgroundColor(R.attr.toggle_color_on)
} else {
setBackgroundColor(R.attr.toggle_color_off)
}
}
init {
LayoutInflater.from(context)
.inflate(R.layout.toggle_image_button, null, true)
}
}
我遵循了一些教程来达到这一目的,但是我有点迷路了。据我了解,按钮必须膨胀才能在Android Studio编辑器中正确呈现。因此,我在init中为ViewGroup使用null添加了该代码。但是,尝试访问按钮时出现NullPointerException,并且应用程序在加载视图之前崩溃。当我只有一个ImageButton时,不会发生这种情况。
values / attrs.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="custom_component_attributes">
<attr name="toggle_color_on" format="color" />
<attr name="toggle_color_off" format="color" />
</declare-styleable>
</resources>
layout / toggle_image_button.xml
<?xml version="1.0" encoding="utf-8"?>
<ImageButton
android:layout_height="match_parent"
android:layout_width="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"/>
崩溃的代码:
driveThruToggle.setOnClickListener {
togglePins(driveThruToggle)
}
foodTruckToggle.setOnClickListener {
togglePins(foodTruckToggle)
}
sitDownToggle.setOnClickListener {
togglePins(sitDownToggle)
}
layout.xml
<TableRow
android:layout_width="match_parent"
android:layout_height="67dp" android:layout_marginBottom="0dp"
android:layout_alignBottom="@+id/map" android:orientation="horizontal"
android:gravity="center|center_vertical"
android:background="@android:color/transparent"
android:weightSum="3">
<com.ktpackages.elements.buttons.ToggleImageButton
android:layout_width="115dp"
android:layout_height="wrap_content" app:srcCompat="@drawable/ic_car_128"
app:toggle_color_off="@color/colorLightText"
app:toggle_color_on="@color/colorBackground"
android:id="@+id/driveThruToggle"
android:background="@color/colorLightText" android:scaleType="fitCenter" android:layout_weight="1"/>
<com.ktpackages.elements.buttons.ToggleImageButton
android:layout_width="115dp"
android:layout_height="wrap_content" app:srcCompat="@drawable/ic_chairs_128"
app:toggle_color_off="@color/colorLightText"
app:toggle_color_on="@color/colorBackground"
android:id="@+id/sitDownToggle"
android:background="@color/colorLightText" android:scaleType="fitCenter" android:layout_weight="1"/>
<com.ktpackages.elements.buttons.ToggleImageButton
android:layout_width="115dp"
android:layout_height="wrap_content"
app:srcCompat="@drawable/ic_food_truck_128"
app:toggle_color_off="@color/colorLightText"
app:toggle_color_on="@color/colorBackground"
android:id="@+id/foodTruckToggle"
android:background="@color/colorLightText"
android:scaleType="fitCenter"
android:layout_weight="1"/>
</TableRow>
我知道切换按钮已经存在,但是我想学习如何操作。