我有一个文本视图,应该在单击某些复选框时更改。用户具有一定数量的免费积分,用于购买属性。单击复选框时,点数会减少。当取消选中一个复选框时,检查它的成本应该被退还到总的空闲点,但在我当前的代码中并非如此。复选框的代码很简单:
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/class_container"
android:layout_below="@id/stats_container"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/class_select"
android:textSize="24sp"
android:id="@+id/class_description"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"/>
<CheckBox
android:layout_below="@id/class_description"
android:id="@+id/checkbox_noble"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/class_noble"
android:onClick="creationClassClick"/>
<CheckBox
android:layout_below="@id/class_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/checkbox_warrior"
android:text="@string/class_warrior"
android:layout_toEndOf="@+id/checkbox_noble"
android:layout_toRightOf="@+id/checkbox_noble"
android:onClick="creationClassClick"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/checkbox_wizard"
android:text="@string/class_wizard"
android:layout_below="@+id/checkbox_noble"
android:onClick="creationClassClick"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/checkbox_trader"
android:text="@string/class_trader"
android:layout_below="@id/checkbox_warrior"
android:layout_toEndOf="@+id/checkbox_wizard"
android:layout_toRightOf="@+id/checkbox_wizard"
android:onClick="creationClassClick"/>
</RelativeLayout>
此处的相关方法如下:
public void pointChange(final int amt){
Runnable updateTextView = new Runnable() {
@Override
public void run() {
TextView tv = findViewById(R.id.free_points);
tv.setText(String.valueOf(amt));
}
};
runOnUiThread(updateTextView);
}
public void creationClassClick(View v){
int freePoints = Integer.parseInt(freePts.getText().toString());
if(lastPointsUsed > 0){
int addedBack = freePoints + lastPointsUsed;
pointChange(addedBack);
freePoints = Integer.parseInt(freePts.getText().toString());
lastPointsUsed = 0;
}
String targetFull = A.getResources().getResourceEntryName(v.getId());
String[] target = targetFull.split("_");
boolean query;
int cost = 1;
int error;
int cause;
switch(target[1]){
case "noble":
warriorCheck.setChecked(false);
wizardCheck.setChecked(false);
traderCheck.setChecked(false);
cost = 12;
break;
case "warrior":
wizardCheck.setChecked(false);
nobleCheck.setChecked(false);
traderCheck.setChecked(false);
cost = 8;
break;
case "wizard":
warriorCheck.setChecked(false);
nobleCheck.setChecked(false);
traderCheck.setChecked(false);
cost = 9;
break;
case "trader":
warriorCheck.setChecked(false);
wizardCheck.setChecked(false);
nobleCheck.setChecked(false);
cost = 10;
break;
}
lastPointsUsed = cost;
query = (freePoints - cost < 0);
error = R.string.creation_no_points;
cause = freePoints - cost;
if(query){
new Modal(app, true, R.string.oops, error, 0, 0);
}else{
this.classType = target[1];
pointChange(cause);
}
}
当我在此活动中使用任何其他方法的pointChange()
方法时,它可以正常工作。它只有在取消选中它失败的复选框时才会这样。
我搜索谷歌和SO的答案,我能想到的最好的是runnable设置为runonuithread,但它也不起作用。任何帮助将不胜感激。
- EDIT-- 通过Butterknife检索适当的视图,但这实际上是除了处理它们的xml之外的唯一代码:
@BindView(R.id.free_points) TextView freePts;
@BindView(R.id.checkbox_noble) CheckBox nobleCheck;
@BindView(R.id.checkbox_warrior) CheckBox warriorCheck;
@BindView(R.id.checkbox_wizard) CheckBox wizardCheck;
@BindView(R.id.checkbox_trader) CheckBox traderCheck;