2011-02-17 73 views
44

我如何在Android中顯示活動指標?有沒有Android庫給定的方法?如果不是,請讓我知道用於在Android中顯示活動指示器的技巧。Android活動指標?

+0

D'你的意思是這樣獨立的無限進度指示器就像一個在ProgressDialog使用(http://developer.android.com/guide/topics/ui/dialogs.html#ProgressDialog) – Olegas 2011-02-17 06:21:40

+0

「活動指示燈」是非常容易混淆Android中的「Activity」類和概念...並沒有意識到這可能是進度對話框/欄:-)但是,在Android中有一些東西,我不知道正確的名稱,因此不能谷歌 – 2011-02-17 06:34:27

回答

52

做一些這樣的事

ProgressDialog mDialog = new ProgressDialog(getApplicationContext()); 
      mDialog.setMessage("Please wait..."); 
      mDialog.setCancelable(false); 
      mDialog.show(); 
+0

我想顯示一個活動指標,如在iphone中。是否有可能顯示繁忙的圖像? – 2011-02-17 06:34:05

+0

耶是使用動畫和自定義圖像,但不會讓用戶停止選擇,即用戶可能會選擇一些按鈕,並直接到其他活動,而您正在等待迴應 – ingsaurabh 2011-02-17 06:37:03

+2

如果最終得到`WindowManager $ BadTokenException`請參閱http:///stackoverflow.com/q/1561803/650233 – 2011-05-26 18:56:53

18

有顯示,而無需使用模式ProgressDialog活動指示燈的另外兩種方式。

您可以在佈局中使用ImageView並將動畫應用到它。 Refer developer's site

public void startAnimation() { 
    // Create an animation 
    RotateAnimation rotation = new RotateAnimation(
     0f, 
     360f, 
     Animation.RELATIVE_TO_SELF, 
     0.5f, 
     Animation.RELATIVE_TO_SELF, 
     0.5f); 
    rotation.setDuration(1200); 
    rotation.setInterpolator(new LinearInterpolator()); 
    rotation.setRepeatMode(Animation.RESTART); 
    rotation.setRepeatCount(Animation.INFINITE); 

    // and apply it to your imageview 
    findViewById(R.id.myActivityIndicator).startAnimation(rotation); 
} 

或者你可以使用XML抽拉描述的背景圖像,這將有一些旋轉的動畫:

首先描述繪製(在IE /res/drawable/my-indicator.xml)

<animated-rotate xmlns:android="http://schemas.android.com/apk/res/android" 
    android:drawable="@drawable/spinner_black_76" 
    android:pivotX="50%" 
    android:pivotY="50%" 
    android:framesCount="12" 
    android:frameDuration="100" /> 

然後將其設置在某個視圖的背景

79

iOS的活動指示燈在Android上最直接對應的是進度條,但設置爲不定。因此,您可以將視圖拖放到佈局中,它將爲您提供旋轉動畫。

<ProgressBar 
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content" 
    android:id="@+id/ctrlActivityIndicator" 
    android:indeterminateOnly="true" 
    android:keepScreenOn="true" 
    /> 

您可以使用它來指示給用戶一些背景活動。

4
public class Mp3cutterActivity extends Activity { 

    MP3Class mp3obj = null; 
    TextView tv; 
    MP3Class mp3classobj = null; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     Context thiscontext = this.getApplicationContext(); 
     mp3classobj = new MP3Class(thiscontext); 
     setContentView(R.layout.main); 

     Button btn = (Button)findViewById(R.id.startbutton); 
     tv = (TextView)findViewById(R.id.textview1); 
     btn.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       //show a wait indicator as well as the function that calls walkSdcard 

       try { 
        new SearchSdCard(Mp3cutterActivity.this).execute(); 
        // copyProtector.doCopyProtection(); 
       } catch (Exception e) { 
        System.out.println("in search SD card " + e.getMessage()); 
       } 
      } 

      private void domp3stuff() { 
       // TODO Auto-generated method stub 
      } 
     }); 
    } 
} 

class SearchSdCard extends AsyncTask<String, Void, Boolean> { 

    Context context;  

    public ProgressDialog dialog; 

    public SearchSdCard(Activity activity) { 
     this.context = activity; 
    } 

    protected void onPreExecute() { 
     dialog = new ProgressDialog(context); 
     dialog.setMessage("wait for a moment..."); 
     dialog.show(); 
    } 

    @Override 
    protected Boolean doInBackground(String... params) { 
     // TODO Auto-generated method stub 
     boolean retval = true; 
     mp3classobj.searchSdCard(); 
     return retval; 
    } 

    @Override 
    protected void onPostExecute(Boolean result) { 
     // TODO Auto-generated method stub 
     if (dialog.isShowing()) { 
      dialog.dismiss(); 
      tv.setText("mp3 cutter is an app which cuts down a chunk of memory \nfrom your sdcard by \ndeleting the .mp3 files and more \nyou were made a BAKRA :-)"); 
      if(!result){  
       tv.setText("error occured !!"); 
      } 
     } 
    } 
    //refer below comment for the MP3class.java file details 
}