如何在Android中显示活动指标?是否有任何Android库给定的方法?如果不是,请告诉我用于在Android中显示活动指示器的技巧?
答案 0 :(得分:88)
Android中iOS活动指标的最直接等价物是ProgressBar,但设置为不确定。因此,您可以将视图放入布局中,它将为您提供旋转动画。
<ProgressBar
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/ctrlActivityIndicator"
android:indeterminateOnly="true"
android:keepScreenOn="true"
/>
您可以使用它来向用户指示一些背景活动。
答案 1 :(得分:55)
做这样的事情
ProgressDialog mDialog = new ProgressDialog(getApplicationContext());
mDialog.setMessage("Please wait...");
mDialog.setCancelable(false);
mDialog.show();
答案 2 :(得分:19)
在不使用模态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-drawable来描述一个背景图像,它将有一些旋转动画:
首先描述一个drawable(在/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" />
然后将其设置在某个视图的背景
答案 3 :(得分: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
}
答案 4 :(得分:0)
活动指示器什么也不是,但它称为进度对话框。您可以创建可编程性。有许多可用的教程,搜索如何创建进度对话框/ ProgressBar
ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setMax(100);
progressDialog.setMessage("Its loading....");
progressDialog.setTitle("ProgressDialog bar example");
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
or
progressDialog.setProgressStyle(ProgressDialog.STYLE_CIRCULAR);
或者您可以使用xml创建它,在任务完成后将可见性设置为 visible 设置可见性消失
<ProgressBar
android:id="@+id/loading_spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
android:indeterminateTintMode="src_atop"
android:indeterminateTint="@color/grey"
android:layout_gravity="center" />