如何在 Android 中生成加载屏幕?

发布于 2024-12-13 22:28:35 字数 198 浏览 1 评论 0原文

问题是我来自另一个活动,当我尝试转到新活动时,它只是坐在旧活动上,直到显示新活动,所以我试图让它立即转到新活动,然后在获取内容时调出加载屏幕。 (内容来自网站或内部数据库)。

我已经尝试过 Android 开发网站上的 ProgressDialog,但它没有执行任何操作,因为 Activity 在显示任何内容之前就完成了加载,因此当它显示时,没有任何内容可以加载。

The problem is I am coming from another Activity, and when I try to go to the new Activity, it just sits on the old one until the new one is displayed, so I am trying to get it to go to the new Activity right away, and then bring up a loading screen while it gets the content. (The content is either coming from a website or an internal database).

I've tried the progressDialog from the Android development site but it doesn't do anything as the Activity finishes loading before showing anything, so by time it shows up, theres nothing to load.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时光与爱终年不遇 2024-12-20 22:28:35

首先启动新活动,然后调用异步任务文件。当您关闭旧活动时,这将启动新活动。在新活动的 Oncreate 中调用异步任务类,如下所示

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
            setContentView(your layout here);
            new GetTask(this).execute();

   }
}

class GetTask extends AsyncTask<Object, Void, String> {
    Context context;

    GetTask(Context context, String userid) {
        this.context = context;
    }

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

        mDialog = new ProgressDialog(mContext);
        mDialog.setMessage("Please wait...");
        mDialog.show();
    }

    @Override
    protected String doInBackground(Object... params) {
        // here you can get the details from db or web and fetch it..
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);

        mDialog.dismiss();
    }
}

First start new activity first and then call the async task file.. this will start new activity when u close old one. in Oncreate of new activity call the asyn task class like below

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
            setContentView(your layout here);
            new GetTask(this).execute();

   }
}

class GetTask extends AsyncTask<Object, Void, String> {
    Context context;

    GetTask(Context context, String userid) {
        this.context = context;
    }

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

        mDialog = new ProgressDialog(mContext);
        mDialog.setMessage("Please wait...");
        mDialog.show();
    }

    @Override
    protected String doInBackground(Object... params) {
        // here you can get the details from db or web and fetch it..
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);

        mDialog.dismiss();
    }
}
缱绻入梦 2024-12-20 22:28:35

作为建议,尝试减少第二个活动的 onCreate 方法中的代码。让它像调用 super 和设置内容视图一样简单。这将显示用 XML 设计的 UI。

将其余代码移至 onResume 方法。
下一步,如果您需要从数据库或外部获取一些数据,请尝试使用 线程如何做到 & 这是什么)或 异步任务

As a suggestion, try reducing the code in onCreate method of the second activity. Let it be as simple as calling super and setting the content views. This will bring up the UI as designed in XML.

Move rest of the code to your onResume method.
Next step, if you have some data to be fetched from DB or outside, try using the thread (how to do it & what is it) or async task.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文