[英]Append method doesn't work in StringBuilder
I'm trying to create a small calculator widget but I can not concatenate the input string using StringBuilder
, because the widget at every click on the button only displays the string corresponding to the button clicked. 我正在尝试创建一个小的计算器窗口小部件,但无法使用
StringBuilder
连接输入字符串,因为每次单击按钮时该窗口小部件仅显示与单击的按钮相对应的字符串。
It seems that the append method in StringBuilder
is not working. 看来
StringBuilder
中的append方法不起作用。 Could you give me some help? 你能给我些帮助吗?
Here are the relevant parts of the code: 以下是代码的相关部分:
StringBuilder sb = new StringBuilder();
@Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
Log.i(LOG_TAG, "onReceive(): " + action);
if (ACTION_WIDGET_CONTROL.equals(action)) {
final int appWidgetId = intent.getIntExtra(
AppWidgetManager.EXTRA_APPWIDGET_ID,
AppWidgetManager.INVALID_APPWIDGET_ID);
if (appWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID) {
fnHandleCommand(context, intent, appWidgetId);
}
} else {
super.onReceive(context, intent);
}
}
private void fnHandleCommand(Context context, Intent intent, int appWidgetId) {
int control_id = intent.getIntExtra(COMMAND, -1);
switch (control_id) {
case R.id.Button00:
String zero = "0";
sb.append(zero);
setInputText(context, appWidgetId,sb.toString());
break;
case R.id.Button01:
String uno = "1";
sb.append(uno);
setInputText(context, appWidgetId,sb.toString());
break;
case R.id.Button02:
String due = "2";
sb.append(due);
setInputText(context, appWidgetId,sb.toString());
break;
case R.id.Button03:
String tre = "3";
sb.append(tre);
setInputText(context, appWidgetId,sb.toString());
break;
case R.id.Button04:
String quattro = "4";
sb.append(quattro);
setInputText(context, appWidgetId,sb.toString());
break;
case R.id.Button05:
String cinque = "5";
sb.append(cinque);
setInputText(context, appWidgetId,sb.toString());
break;
case R.id.Button06:
String sei = "6";
sb.append(sei);
setInputText(context, appWidgetId,sb.toString());
break;
case R.id.Button07:
String sette = "7";
sb.append(sette);
setInputText(context, appWidgetId,sb.toString());
break;
case R.id.Button08:
String otto = "8";
sb.append(otto);
setInputText(context, appWidgetId,sb.toString());
break;
case R.id.Button09:
String nove = "9";
sb.append(nove);
setInputText(context, appWidgetId,sb.toString());
break;
case R.id.ButtonPlus:
String piu = "+";
sb.append(piu);
setInputText(context, appWidgetId,sb.toString());
break;
case R.id.ButtonMinus:
String meno = "-";
sb.append(meno);
setInputText(context, appWidgetId,sb.toString());
break;
case R.id.ButtonMutiple:
String per = "*";
sb.append(per);
setInputText(context, appWidgetId,sb.toString());
break;
case R.id.ButtonDivide:
String diviso = "/";
sb.append(diviso);
setInputText(context, appWidgetId,sb.toString());
break;
case R.id.ButtonNegative:
String rad = "\u221a";
sb.append(rad);
setInputText(context, appWidgetId,sb.toString());
break;
case R.id.ButtonAC:
String vuota = "";
sb.setLength(0);
cancel(context, appWidgetId,sb.toString(),vuota);
break;
default:
break;
}
fnVibrate(context);
}
private void setInputText(Context context, int appWidgetId, String value) {
RemoteViews remoteView = new RemoteViews(context.getPackageName(),
R.layout.simple_calculator);
remoteView.setTextViewText(R.id.TextValue, value);
AppWidgetManager.getInstance(context).updateAppWidget(appWidgetId,
remoteView);
}
Thanks 谢谢
私有静态 StringBuilder sb = new StringBuilder();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.