Android的碎片 - 添加进度条 [英] Android Fragments - Adding progress bar

查看:196
本文介绍了Android的碎片 - 添加进度条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的。让我知道如果我在问这个问题的错误的方式或者如果我需要澄清这更多。

I'm new here. Let me know if I'm asking the question the wrong way or if I need to clarify this more.

我创建一个Android应用程序,我用我的code片段(即4个选项卡,可以滑动)。我想添加一个栏显示多少用户已经使用的百分比。我认为最好的办法是一个进度条,但我似乎无法得到它的工作。

I'm creating an Android app and I'm using fragments in my code (ie 4 tabs that can slide). I'm trying to add a bar to show a percentage of how much the user has used. I thought the best option would be a progress bar but I cant seem to get it to work.

下面是我的code,因为我想添加到酒吧片段;

Here's my code for the fragment I want to add the bar to;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class UserFragment extends Fragment {

    private ProgressDialog progress;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_calls, container, false);

    progress = new ProgressDialog(this);
    return rootView;
}

public void open(View view){
      progress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
      progress.setIndeterminate(true);
      progress.show();

       final int totalProgressTime = 100;

       final Thread t = new Thread(){

       @Override
       public void run(){

          int jumpTime = 0;
          while(jumpTime < totalProgressTime){
             try {
                sleep(200);
                jumpTime += 5;
                progress.setProgress(jumpTime);
             } catch (InterruptedException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
             }

          }

       }
       };
       t.start();
}

}
我跟着一对夫妇教程在线,但我一直得到错误说构造进度对话框是不明确的。我明白我需要某种上下文对象,但我迷路了如何去这样做。如果有任何的你们可以给我帮助,在此,我会非常AP preciate它。谢谢

} I followed a couple of tutorials online but I kept getting errors saying "the constructor progress dialog is undefined". I understand that I need some kind of context object but I'm lost on how to go about doing this. If any of ye could give me help on this, I'd greatly appreciate it. Thanks

推荐答案

如果您需要得到内部片段的上下文对象,你可以使用的 getActivity()的方法,该方法返回活动有关你的分段。如果你有活动,你有上下文的,因为的活动扩展上下文

If you need to get a context object inside fragment, you can use getActivity() method, which returns the activity associated to your fragment. If you've got the Activity, you've got the context, since Activity extends Context.

这篇关于Android的碎片 - 添加进度条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆