我正在尝试创建一个小型计算器小部件但我无法使用StringBuilder
连接输入字符串,因为按钮上每次单击时的小部件仅显示与单击的按钮对应的字符串。
似乎StringBuilder
中的追加方法无效。你能给我一些帮助吗?
以下是代码的相关部分:
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);
}
由于
答案 0 :(得分:1)
private static StringBuilder sb = new StringBuilder();