我試圖創建一個小型計算器Widget,但我無法使用StringBuilder
連接輸入字符串,因爲每次點擊按鈕時Widget只顯示與點擊按鈕對應的字符串。追加方法在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);
}
由於
你試過調試過嗎? StringBuilder.append不工作的機會遠遠小於它在其他地方成爲問題的可能性......我懷疑你會發現你沒有發佈的代碼片段正在清除「StringBuilder」點擊。在「fnHandleCommand」的* start *處記錄(或者用調試器檢查)'sb'的值。 –
和你有什麼問題? – hoaz
似乎每次點擊都會初始化stringbuilder – user1480020