本地化进度对话框

发布于 2025-01-01 02:34:14 字数 762 浏览 1 评论 0原文

我显示了一个带有异步任务的进度对话框。

private class RetrieveTask extends AsyncTask<String, Void, String>
{
    RetrieveTask t = this;
    ProgressDialog d = null;

    @Override
    protected void onPreExecute()
    {
        // TODO
        //d = ProgressDialog.show(context, "", context.getString(R.string.querying_server), true, true, new DialogInterface.OnCancelListener() {
        d = ProgressDialog.show(context, "", "Loading...", true, true, new DialogInterface.OnCancelListener() {
                public void onCancel(DialogInterface dialog) {
                t.cancel(true);
        }
        });
    // continues....

到此阶段,context 已设置为根 Activity。

我想本地化该消息,如“TODO”后面的评论中所示。为此,我需要获取字符串资源;我如何最好地从这里做到这一点?

I show a progress dialog with an asynchronous task.

private class RetrieveTask extends AsyncTask<String, Void, String>
{
    RetrieveTask t = this;
    ProgressDialog d = null;

    @Override
    protected void onPreExecute()
    {
        // TODO
        //d = ProgressDialog.show(context, "", context.getString(R.string.querying_server), true, true, new DialogInterface.OnCancelListener() {
        d = ProgressDialog.show(context, "", "Loading...", true, true, new DialogInterface.OnCancelListener() {
                public void onCancel(DialogInterface dialog) {
                t.cancel(true);
        }
        });
    // continues....

By this stage context has been set to the root activity.

I'd like to localise the message as indicated in the comment following the "TODO". To do this I need to get at the strings resource; how do I best do this from here?

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

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

发布评论

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

评论(2

哀由 2025-01-08 02:34:14

我缺少一个 getResources() 调用,仅此而已。

context.getResources().getString(R.string.my_string_id);

I'm missing a getResources() call, that's all.

context.getResources().getString(R.string.my_string_id);

浮生面具三千个 2025-01-08 02:34:14

我只是使用对调用活动上下文的引用。

伪代码示例:

    public class MyMainActivity extends Activity {

    private void MethodA{
        new RetreiveTask().execute();
    }

   //then in the AsyncTask, you could get a context by using the: *MyMainActivity.this* syntax.

    private class RetrieveTask extends AsyncTask<String, Void, String>
    {
        RetrieveTask t = this;
        ProgressDialog d = null;

        @Override
        protected void onPreExecute()
        {
            // TODO
            //d = ProgressDialog.show(context, "", context.getString(MyMainActivity.this.R.string.querying_server), true, true, new DialogInterface.OnCancelListener() {
            d = ProgressDialog.show(context, "", "Loading...", true, true, new DialogInterface.OnCancelListener() {
                    public void onCancel(DialogInterface dialog) {
                    t.cancel(true);
            }
            });

    }



    }

I just use a reference to the calling activities context.

Example pseudo-code:

    public class MyMainActivity extends Activity {

    private void MethodA{
        new RetreiveTask().execute();
    }

   //then in the AsyncTask, you could get a context by using the: *MyMainActivity.this* syntax.

    private class RetrieveTask extends AsyncTask<String, Void, String>
    {
        RetrieveTask t = this;
        ProgressDialog d = null;

        @Override
        protected void onPreExecute()
        {
            // TODO
            //d = ProgressDialog.show(context, "", context.getString(MyMainActivity.this.R.string.querying_server), true, true, new DialogInterface.OnCancelListener() {
            d = ProgressDialog.show(context, "", "Loading...", true, true, new DialogInterface.OnCancelListener() {
                    public void onCancel(DialogInterface dialog) {
                    t.cancel(true);
            }
            });

    }



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