2012-12-14 224 views
0

我想創建一個不需要顯示百分比的進度條。ProgressBar無百分比

public boolean onOptionsItemSelected(MenuItem item) { 
    switch(item.getItemId()) { 
     case R.id.back: 
      this.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK)); 
      finish(); 
      return true; 
     case R.id.read: 
      // This is where I would start my spinner.... 
      if(myService.getState() != 3) { 
       myService.connect(MainMenu.previousDevice, true); 
      } 
      readWaves(); 
      return true; 
     default: 
      return super.onContextItemSelected(item); 
    } 
} 

的微調將停止自己在readWaves()函數...

private void readWaves() { 
     waveResults = RelayAPIModel.NativeCalls.GetWavesJava(RelayAPIModel.WAVES_V); 
     // Turn the Spinner off Here. 
     doOtherThings(); 
    } 

我發現噸的代碼遍佈告訴我如何使用線程使用一個進度條,加入進步在xml文件中的bar和許多其他方法。

我認爲做這件事的最好方法是創建一個CustomDialog類來彈出並讓旋轉器旋轉,直到我調用它來結束它。

顯示的第一個答案here是迄今爲止最接近我找到我想要的,但不幸的是他的解決方案不起作用。它不工作的原因是因爲代碼的最後一節:

public MyProgressDialog(Context context) { 
    super(context, R.style.NewDialog); 
} 

我使用的是Android 2.3,並且沒有styleR屬性。我嘗試自己添加它,並得到一個錯誤。有誰知道如何解決這一問題?

編輯:

這是我使用ProgressDialog

case R.id.read: 
      pDialog = new ProgressDialog(Waves.this); 
      pDialog.setTitle("Waves"); 
      pDialog.setMessage("Reading..."); 
      pDialog.show(); 
      if(myService.getState() != 3) { 
       myService.connect(MainMenu.previousDevice, true); 
      } 
      readWaves(); 
      return true; 
     default: 
      return super.onContextItemSelected(item); 
    } 
} 

private void readWaves() { 
    if(spinnerChoice == 0) { 
     Log.i("IN IF", "IN IF"); 
     // Diff Voltage 
     waveResults = RelayAPIModel.NativeCalls.GetWavesJava(RelayAPIModel.WAVES_V); 
    } else { 
     // Current 
     waveResults = RelayAPIModel.NativeCalls.GetWavesJava(RelayAPIModel.WAVES_I); 
    } 
    Log.i("READ WAVES", "After If/Else"); 
    pDialog.dismiss(); 
    /* 
+0

你在styles.xml文件中有一個樣式名稱NewDialog ?如果是這樣,你確定你導入了正確的R類嗎? – njzk2

+0

我不知道在哪裏可以找到'styles.xml'文件。我對Android比較陌生,我認爲'R.style.NewDialog'類似於調用'setContentView(R.layout.main)',所以'style'將會是我的項目中的一個目錄。有什麼'styles.xml'文件? – JuiCe

+1

style.xml轉到res/values( - *) – njzk2

回答

1

已經有正在爲這一特定目的創建了一個對話框試過的代碼。檢出ProgressDialog。您可以使用它像這樣:

//show the dialog 
ProgressDialog pd = new ProgressDialog(YourActivity.this); 
pd.setTitle("Your title"); 
pd.setMessage("Helpful message to the user"); 
pd.show(); 

/************** 
* Do some stuff 
**************/ 

//Hide the dialog 
pd.dismiss(); 

編輯:這裏是它會怎樣看在選項菜單中選擇一個小樣本:

public boolean onCreateOptionsMenu(Menu menu) { 
    menu.add(0, 42, 0, "Option"); 
    return true; 
} 
public boolean onOptionsItemSelected(MenuItem item) { 
    switch (item.getItemId()) { 
    case 42: 
      ProgressDialog pd = new ProgressDialog(this); 
      pd.show(); 
     break; 
    default: 
     //Do nothing  
    } 
    return false; 
} 
+0

試圖給出一個鏡頭,沒有出現。 – JuiCe

+0

@JuiCe你叫'.show()'?如果可能的話,用你試過的代碼編輯你的問題 – FoamyGuy

+0

是的,我做過。我編輯了我的原始帖子以顯示我的代碼。我嘗試在我的'onCreate()'方法中添加初始化和'.show()'調用,並且它工作正常。我假設代碼以某種方式跳過對話框,因爲它跳轉到'readWaves()'中的'waveResults = ...'行,但我不明白爲什麼。另外,當對話框在我的'onCreate()'方法中時,只需按下硬件後退按鈕即可取消,這似乎不正確。任何想法呢? – JuiCe