2014-01-31 48 views
0

我試圖創建一個小型計算器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); 
} 

由於

+1

你試過調試過嗎? StringBuilder.append不工作的機會遠遠小於它在其他地方成爲問題的可能性......我懷疑你會發現你沒有發佈的代碼片段正在清除「StringBuilder」點擊。在「fnHandleCommand」的* start *處記錄(或者用調試器檢查)'sb'的值。 –

+0

和你有什麼問題? – hoaz

+1

似乎每次點擊都會初始化stringbuilder – user1480020

回答

1

私人靜態 StringBuilder的SB =新的StringBuilder();

+1

所以現在StringBuilder在任何Java類的所有實例之間共享。這聽起來不是一個特別好的主意。如果每次都真正重新創建StringBuilder(我認爲他的命令處理程序更有可能不被調用),他應該獲取當前的小部件輸入文本,將他想要的任何內容附加到該文本中,然後重新設置它。 –

+0

如果小部件被重新創建,那就是輸入文本。整個小部件與所有領域重新創建每一次。上面是最簡單的解決方案。另一種解決方案是將文本保存到首選項中,並且每次都從它們重新加載。 – Autocrab

+0

他的建議很好。非常感謝你 – user1480020