gpt4 book ai didi

android - 如何实现加载屏幕以防止用户在进行后台处理时与界面交互

转载 作者:太空宇宙 更新时间:2023-11-03 13:16:38 24 4
gpt4 key购买 nike

在我的新应用中,我需要在进行某些处理(例如,将数据发送到服务器、从网络服务读取数据等)时显示动画。

像这样:

enter image description here

当我想实现类似的东西时,这是我曾经做的:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<RelativeLayout
android:id="@+id/sincronizarSpinnerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone" >

<ProgressBar
android:id="@+id/pbHeaderProgress"
style="@android:style/Widget.ProgressBar.Inverse"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" >
</ProgressBar>
</RelativeLayout>


<RelativeLayout>

<!--Main content here-->

</RelativeLayout>

</LinearLayout>

如您所见,我有两个嵌套 相对布局。默认情况下,第一个布局是不可见的 (android:visibility="gone"),我仅在启动服务、AsyncTask 或异步技术时才使其可见。

虽然我过去使用过这种方法,但现在我的主要布局(应该放在第二个相对布局中的布局)更加复杂,我不确定是否让事情变得更均匀是个好主意通过向我的 Activity 添加另一个嵌套级别变得更加复杂。

  1. 有什么方法可以避免为所有的布局添加另一个布局?我的 Activity 需要显示微调动画吗?也许有某种我不知道的模式或良好做法。

  2. 我真的需要担心在我的代码中添加另一个嵌套级别吗? Activity ,知道我已经有两个或三个级别?

谢谢。

最佳答案

您可以简单地使用 ProgressDialog 并使用 setCanceledOnTouchOutside(false) 这样当您的 AsyncTask 工作时用户无法触摸外部。

这是我在使用 AsyncTask 的项目中几乎使用的结构代码。您可以申请您的项目:

public class DownloadTask extends AsyncTask<Void, Void, Void> {
private ProgressDialog mProgressDialog;

private Context mContext;

public DownloadTask(Context context) {
this.mContext = context;
}

@Override
protected void onPreExecute() {
super.onPreExecute();

mProgressDialog = ProgressDialog.show(mContext, "Downloading", "Downloading Data ...");
mProgressDialog.setCanceledOnTouchOutside(false); // main method that force user cannot click outside
mProgressDialog.setCancelable(true);
mProgressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dlg) {
DownloadTask.this.cancel(true);
}
});
}

@Override
protected Void doInBackground(Void... params) {
// do some background work here

}

@Override
protected void onPostExecute(Void result) {
if (this.isCancelled()) {
result = null;
return;
}

if (mProgressDialog != null) {
mProgressDialog.dismiss();
}

}
}

希望这有帮助:)

关于android - 如何实现加载屏幕以防止用户在进行后台处理时与界面交互,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35044901/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com