当应用等待来自服务器的响应时,显示加载微调器的最佳方法是什么?
可以通过编程方式完成吗?这样我就不必在xml文件中添加加载微调器了吗?
答案 0 :(得分:283)
从Android Oreo弃用ProgressDialog。请改用ProgressBar
ProgressDialog progress = new ProgressDialog(this);
progress.setTitle("Loading");
progress.setMessage("Wait while loading...");
progress.setCancelable(false); // disable dismiss by tapping outside of the dialog
progress.show();
// To dismiss the dialog
progress.dismiss();
或强>
ProgressDialog.show(this, "Loading", "Wait while loading...");
顺便说一句, Spinner 在Android中有不同的含义。 (这就像HTML中的选择下拉列表)
答案 1 :(得分:16)
ProgressDialog.show(Context context, CharSequence title, CharSequence message);
然而,今天(2013年)这被视为反模式:http://www.youtube.com/watch?v=pEGWcMTxs3I
答案 2 :(得分:9)
实际上,如果您正在等待来自服务器的响应,应该以编程方式完成。您可以创建一个进度对话框并将其关闭,但是这又不是“android方式”。
目前推荐的方法是使用 DialogFragment :
public class MySpinnerDialog extends DialogFragment {
public MySpinnerDialog() {
// use empty constructors. If something is needed use onCreate's
}
@Override
public Dialog onCreateDialog(final Bundle savedInstanceState) {
_dialog = new ProgressDialog(getActivity());
this.setStyle(STYLE_NO_TITLE, getTheme()); // You can use styles or inflate a view
_dialog.setMessage("Spinning.."); // set your messages if not inflated from XML
_dialog.setCancelable(false);
return _dialog;
}
}
然后在您的活动中设置片段管理器,并在等待服务器启动后显示对话框:
FragmentManager fm = getSupportFragmentManager();
MySpinnerDialog myInstance = new MySpinnerDialog();
}
myInstance.show(fm, "some_tag");
一旦您的服务器响应完成,您将解雇它:
myInstance.dismiss()
请记住,progressdialog是一个微调器或一个进度条,具体取决于属性,请参阅api guide
答案 3 :(得分:9)
自API级别26以来,ProgressDialog已被弃用 https://developer.android.com/reference/android/app/ProgressDialog.html
我在布局中包含了一个ProgressBar
<ProgressBar
android:layout_weight="1"
android:id="@+id/progressBar_cyclic"
android:visibility="gone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minHeight="40dp"
android:minWidth="40dp" />
并将其可见性更改为.GONE | .VISIBLE取决于用例。
progressBar_cyclic.visibility = View.VISIBLE
答案 4 :(得分:4)
这就是我这样做的方式,因此一次只能打开一个进度对话框。基于Suraj Bajaj的答案
private ProgressDialog progress;
public void showLoadingDialog() {
if (progress == null) {
progress = new ProgressDialog(this);
progress.setTitle(getString(R.string.loading_title));
progress.setMessage(getString(R.string.loading_message));
}
progress.show();
}
public void dismissLoadingDialog() {
if (progress != null && progress.isShowing()) {
progress.dismiss();
}
}
我还必须使用
protected void onResume() {
dismissLoadingDialog();
super.onResume();
}