无法更改 TextView 颜色
Can't change TextView color
我需要你的帮助。
我想在代码中更改 TextViews 的颜色,但是遇到了一些困难,因为没有任何反应。
我已经在 MainActivity 中声明了一个 TextView 字段,并将我的文本对象从布局分配给它。然后我调用一些方法,进行一些计算并将结果写入该 textView 对象。当我在此方法中捕获异常时,我想更改 textView 的颜色,但我不明白。我应该寻找什么来修复它?
顺便说一句,当我尝试在 onCreate 方法中更改颜色时,效果很好。异常的 catch 块也实现得很好,因为我通过该块的其他命令看到了这一点。如果没有任何异常从中引发任何异常,则 Scale 方法可以正常工作。
试图找到类似的问题,但我猜我离正确的方法太远了。
private TextView outputText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
outputText = findViewById(R.id.outputText);
//this code works fine
outputText.setTextColor(getResources().getColor(R.color.errorColor, getTheme()));
scale();
}
public void scale() {
try {
//some command that can throw an exception
} catch (Exception e) {
//this command doesn't work
outputText.setTextColor(getResources().getColor(R.color.errorColor, getTheme()));
}
}
UPD.1
这里是 Scale 方法的代码:
public void scale() {
try {
outMin = Double.parseDouble(String.valueOf(outMinField.getText()));
outMinErr = false;
} catch (Exception e) {
outMaxErr = true;
outMinField.requestFocus();
outputText.setTextColor(getResources().getColor(R.color.errorColor, getTheme()));
}
try {
outMax = Double.parseDouble(String.valueOf(outMaxField.getText()));
outMaxErr = false;
} catch (Exception e) {
outMaxErr = true;
outMaxField.requestFocus();
outputText.setTextColor(getResources().getColor(R.color.errorColor, getTheme()));
}
//inMin & inMax gives another method
double out = (outMax - outMin) / (inMax - inMin) * (in - inMin) + outMin;
outputText.setText(String.format("%.2f", out));
outputText.setTextColor(getResources().getColor(R.color.outputColor, getTheme()));
}
我知道我得到了一个异常,因为我看到一个焦点文本输入字段我遇到了问题。
我没有发现您的代码有任何严重问题,但我会提出以下建议
public void scale() {
boolean error = false;
try {
callThatMayThrowAnException();
} catch (Exception e) {
error = true;
}
Log.d("ViewColor", "Exception thrown: " + error);
if(error)
outputText.setTextColor(getResources().getColor(R.color.errorColor, getTheme()));
}
现在您可以在 LogCat
中查看您是捕获到异常还是 callThatMayThrowAnException()
成功。如果这没有帮助(错误被捕获但视图颜色没有改变)尝试在 Build
菜单中清理你的项目并重建它。
我需要你的帮助。
我想在代码中更改 TextViews 的颜色,但是遇到了一些困难,因为没有任何反应。
我已经在 MainActivity 中声明了一个 TextView 字段,并将我的文本对象从布局分配给它。然后我调用一些方法,进行一些计算并将结果写入该 textView 对象。当我在此方法中捕获异常时,我想更改 textView 的颜色,但我不明白。我应该寻找什么来修复它?
顺便说一句,当我尝试在 onCreate 方法中更改颜色时,效果很好。异常的 catch 块也实现得很好,因为我通过该块的其他命令看到了这一点。如果没有任何异常从中引发任何异常,则 Scale 方法可以正常工作。 试图找到类似的问题,但我猜我离正确的方法太远了。
private TextView outputText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
outputText = findViewById(R.id.outputText);
//this code works fine
outputText.setTextColor(getResources().getColor(R.color.errorColor, getTheme()));
scale();
}
public void scale() {
try {
//some command that can throw an exception
} catch (Exception e) {
//this command doesn't work
outputText.setTextColor(getResources().getColor(R.color.errorColor, getTheme()));
}
}
UPD.1
这里是 Scale 方法的代码:
public void scale() {
try {
outMin = Double.parseDouble(String.valueOf(outMinField.getText()));
outMinErr = false;
} catch (Exception e) {
outMaxErr = true;
outMinField.requestFocus();
outputText.setTextColor(getResources().getColor(R.color.errorColor, getTheme()));
}
try {
outMax = Double.parseDouble(String.valueOf(outMaxField.getText()));
outMaxErr = false;
} catch (Exception e) {
outMaxErr = true;
outMaxField.requestFocus();
outputText.setTextColor(getResources().getColor(R.color.errorColor, getTheme()));
}
//inMin & inMax gives another method
double out = (outMax - outMin) / (inMax - inMin) * (in - inMin) + outMin;
outputText.setText(String.format("%.2f", out));
outputText.setTextColor(getResources().getColor(R.color.outputColor, getTheme()));
}
我知道我得到了一个异常,因为我看到一个焦点文本输入字段我遇到了问题。
我没有发现您的代码有任何严重问题,但我会提出以下建议
public void scale() {
boolean error = false;
try {
callThatMayThrowAnException();
} catch (Exception e) {
error = true;
}
Log.d("ViewColor", "Exception thrown: " + error);
if(error)
outputText.setTextColor(getResources().getColor(R.color.errorColor, getTheme()));
}
现在您可以在 LogCat
中查看您是捕获到异常还是 callThatMayThrowAnException()
成功。如果这没有帮助(错误被捕获但视图颜色没有改变)尝试在 Build
菜单中清理你的项目并重建它。