是否可以为 Android 中的 TextView 添加 setOnColorChangeListener?
Is it possible to add a setOnColorChangeListener for a TextView in Android?
例如,我想在每次以编程方式更改 TextView
的 textColor
时打印 Toast
-->COLOR CHANGED
<--。这可能吗?
可能您添加回调的方法与为文本视图设置新颜色的方法相同。
例子
public void setNewTextColor(int color) {
yourTextView.setTextColor(color);
yourCallbackMethod();
}
public void yourCallbackMethod() {
//you can do whatever in this method
}
更新 -
您可以添加自定义文本视图并通过覆盖自定义文本视图中的 setTextColor 方法来定义回调 class.
在 android 中创建自定义视图的官方指南 - https://developer.android.com/training/custom-views/create-view
AFAIK,没有记录的官方方法来实现您想要的。
但我可能会为此提出一个解决方法,尽管它有点矫枉过正。我保证这个答案的灵活性,因为它几乎适用于涉及 TextView 使用的所有用例。
您可以通过扩展 TextView class 创建一个自定义 TextView,并在您的自定义 TextView class 中添加一个可用于应用颜色变化侦听器的自定义界面。像这样:
public class MyCustomTextView extends TextView {
// other code, but you not need it since it's already inherited from the parent; unless you want to customize them too.
public interface OnColorChangeListener {
void onColorChanged()
}
private OnColorChangeListener onColorChangeListener;
// a public method to apply a color change listener interface to your TextView
public void setOnColorChangeListener(OnColorChangeListener onColorChangeListener) {
this.onColorChangeListener = onColorChangeListener;
}
public void setTextColor() {
// REMEMBER & BE AWARE: this is the original TextView method for setting a color.
// here you can call the listener onColorChanged() method.
onColorChangeListener.onColorChanged()
}
}
您的下一步是将 XML 布局文件中的 TextView 更改为此自定义 TextView class。然后,在你想要监听颜色变化的 activity/fragment 中,你可以像这样简单地做:
yourCustomTextView.setOnColorChangeListener(new MyCustomTextView.OnColorChangeListener() {
void onColorChanged() {
Toast.makeText(context, "Your text", Toast.LENGTH_SHORT).show();
}
});
使用这种方法,每次更改自定义 TextView 的颜色时都会显示 toast。
希望这对您有所帮助。如果您不 understand/want 建议,请随时发表评论。
编码愉快!
例如,我想在每次以编程方式更改 TextView
的 textColor
时打印 Toast
-->COLOR CHANGED
<--。这可能吗?
可能您添加回调的方法与为文本视图设置新颜色的方法相同。
例子
public void setNewTextColor(int color) {
yourTextView.setTextColor(color);
yourCallbackMethod();
}
public void yourCallbackMethod() {
//you can do whatever in this method
}
更新 - 您可以添加自定义文本视图并通过覆盖自定义文本视图中的 setTextColor 方法来定义回调 class.
在 android 中创建自定义视图的官方指南 - https://developer.android.com/training/custom-views/create-view
AFAIK,没有记录的官方方法来实现您想要的。 但我可能会为此提出一个解决方法,尽管它有点矫枉过正。我保证这个答案的灵活性,因为它几乎适用于涉及 TextView 使用的所有用例。
您可以通过扩展 TextView class 创建一个自定义 TextView,并在您的自定义 TextView class 中添加一个可用于应用颜色变化侦听器的自定义界面。像这样:
public class MyCustomTextView extends TextView {
// other code, but you not need it since it's already inherited from the parent; unless you want to customize them too.
public interface OnColorChangeListener {
void onColorChanged()
}
private OnColorChangeListener onColorChangeListener;
// a public method to apply a color change listener interface to your TextView
public void setOnColorChangeListener(OnColorChangeListener onColorChangeListener) {
this.onColorChangeListener = onColorChangeListener;
}
public void setTextColor() {
// REMEMBER & BE AWARE: this is the original TextView method for setting a color.
// here you can call the listener onColorChanged() method.
onColorChangeListener.onColorChanged()
}
}
您的下一步是将 XML 布局文件中的 TextView 更改为此自定义 TextView class。然后,在你想要监听颜色变化的 activity/fragment 中,你可以像这样简单地做:
yourCustomTextView.setOnColorChangeListener(new MyCustomTextView.OnColorChangeListener() {
void onColorChanged() {
Toast.makeText(context, "Your text", Toast.LENGTH_SHORT).show();
}
});
使用这种方法,每次更改自定义 TextView 的颜色时都会显示 toast。
希望这对您有所帮助。如果您不 understand/want 建议,请随时发表评论。 编码愉快!