在 Android 选项卡布局中显示进度对话框

发布于 2024-11-27 14:55:04 字数 3367 浏览 2 评论 0原文

我需要在单击选项卡时显示进度对话框,

我的代码 ID

public class SIPTabWidget extends TabActivity{
 Intent intent;
  @Override
    protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);


    setContentView(R.layout.main);



    final Resources res = getResources(); // Resource object to get Drawables
    final TabHost tabHost = getTabHost(); // The activity TabHost
    TabHost.TabSpec spec; // Reusable TabSpec for each tab



    // Create an Intent to launch an Activity for the tab (to be reused)
    intent = new Intent().setClass(this, ListContacts.class);

    // Initialize a TabSpec for each tab and add it to the TabHost
    spec = tabHost
    .newTabSpec("contacts")
    .setIndicator("",
        res.getDrawable(R.drawable.tab_contacts))
        .setContent(intent);



    tabHost.addTab(spec);

    intent = new Intent().setClass(this, Dialer.class);
    spec = tabHost
    .newTabSpec("dialer")
    .setIndicator("", res.getDrawable(R.drawable.tab_dial))
    .setContent(intent);
    tabHost.addTab(spec);


    intent = new Intent().setClass(this, Favorites.class);
    spec = tabHost
    .newTabSpec("favorites")
    .setIndicator("", res.getDrawable(R.drawable.tab_favorites))
    .setContent(intent);
    tabHost.addTab(spec);

    intent = new Intent().setClass(this, org.sipdroid.sipua.ui.Settings.class);
    spec = tabHost
    .newTabSpec("settings")
    .setIndicator("", res.getDrawable(R.drawable.tab_settings))
    .setContent(intent);

    tabHost.addTab(spec);

}

当我单击设置选项卡时,

final ProgressDialog dialog = ProgressDialog.show(getParent(), "Please wait..", "Doing stuff..", true);
        dialog.setCancelable(false);
        dialog.show();

,调用设置活动,在其 oncreate 中我给出了在此处输入图像描述

对我来说,在显示“设置”布局之前,它需要一些时间来加载布局,因此在此之前我需要显示“进度对话框”,这意味着当我单击“设置”选项卡时< /strong> 我需要在以下情况下关闭对话框该选项卡更改为 Settings

tabHost.setOnTabChangedListener(new OnTabChangeListener() {

        @Override
        public void onTabChanged(String tabId) {
        System.out.println(" Tab ID:  "+tabId);
if(tabId.equals("dial")){
dialog .dismiss();
}

    }
});

Settings.class

public class Settings extends PreferenceActivity implements OnSharedPreferenceChangeListener, OnClickListener {

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        System.out.println("Settings oncreate");
        final ProgressDialog dialog = ProgressDialog.show(getParent(), "Please  wait..", "Doing stuff..", true);
        handler = new Handler() {

              @Override
              public void handleMessage(Message msg) {
              dialog.dismiss();

              }
           };


           Thread t = new Thread() {
                public void run() {
                    //send message
                handler.sendEmptyMessage(0);      
                }
            };  





       if (Receiver.mContext == null) Receiver.mContext = this;
        addPreferencesFromResource(R.xml.preferences);
        setDefaultValues();
        Codecs.check();
        t.start





    }

I need to show a progress dialog, while clicking a tab,

My code id

public class SIPTabWidget extends TabActivity{
 Intent intent;
  @Override
    protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);


    setContentView(R.layout.main);



    final Resources res = getResources(); // Resource object to get Drawables
    final TabHost tabHost = getTabHost(); // The activity TabHost
    TabHost.TabSpec spec; // Reusable TabSpec for each tab



    // Create an Intent to launch an Activity for the tab (to be reused)
    intent = new Intent().setClass(this, ListContacts.class);

    // Initialize a TabSpec for each tab and add it to the TabHost
    spec = tabHost
    .newTabSpec("contacts")
    .setIndicator("",
        res.getDrawable(R.drawable.tab_contacts))
        .setContent(intent);



    tabHost.addTab(spec);

    intent = new Intent().setClass(this, Dialer.class);
    spec = tabHost
    .newTabSpec("dialer")
    .setIndicator("", res.getDrawable(R.drawable.tab_dial))
    .setContent(intent);
    tabHost.addTab(spec);


    intent = new Intent().setClass(this, Favorites.class);
    spec = tabHost
    .newTabSpec("favorites")
    .setIndicator("", res.getDrawable(R.drawable.tab_favorites))
    .setContent(intent);
    tabHost.addTab(spec);

    intent = new Intent().setClass(this, org.sipdroid.sipua.ui.Settings.class);
    spec = tabHost
    .newTabSpec("settings")
    .setIndicator("", res.getDrawable(R.drawable.tab_settings))
    .setContent(intent);

    tabHost.addTab(spec);

}

when i click the settings tab, Setting activity is called, in its oncreate i gave

final ProgressDialog dialog = ProgressDialog.show(getParent(), "Please wait..", "Doing stuff..", true);
        dialog.setCancelable(false);
        dialog.show();

enter image description here

for me before showing the Settings layout its taking some time to load the layout, so before that i need to show the ProgressDialog, means the time when i click the Settings tab and i need to dismiss the Dialog when the tab changes to Settings

tabHost.setOnTabChangedListener(new OnTabChangeListener() {

        @Override
        public void onTabChanged(String tabId) {
        System.out.println(" Tab ID:  "+tabId);
if(tabId.equals("dial")){
dialog .dismiss();
}

    }
});

Settings.class

public class Settings extends PreferenceActivity implements OnSharedPreferenceChangeListener, OnClickListener {

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        System.out.println("Settings oncreate");
        final ProgressDialog dialog = ProgressDialog.show(getParent(), "Please  wait..", "Doing stuff..", true);
        handler = new Handler() {

              @Override
              public void handleMessage(Message msg) {
              dialog.dismiss();

              }
           };


           Thread t = new Thread() {
                public void run() {
                    //send message
                handler.sendEmptyMessage(0);      
                }
            };  





       if (Receiver.mContext == null) Receiver.mContext = this;
        addPreferencesFromResource(R.xml.preferences);
        setDefaultValues();
        Codecs.check();
        t.start





    }

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

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

发布评论

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

评论(4

无悔心 2024-12-04 14:55:05

好吧,每个选项卡都是一个活动...我想花费时间在设置活动中...

将其放入设置活动中:

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final ProgressDialog dialog = ProgressDialog.show(getParent(), "Please wait..", "Doing stuff..", true);
dialog.setCancelable(false);
dialog.show(); 
Runnable myRun = new Runnable(){
public void run(){
    Looper.myLooper().prepare();
    //DO EVERYTHING YOU WANT!

    //Finally
    runOnUiThread(new Runnable() {
    @Override
    public void run() {
    dialog.dismis();
    }
    });
}
};
Thread T = new Thread(myRun);
T.start();

记住:在您< code>run() 在此块中

//DO EVERYTHING YOU WANT!

如果需要更改 UI,则必须使用

    runOnUiThread(new Runnable() {
    @Override
    public void run() {
        //DO IT
    }
    });

Well each tab is an activity... I suppose that what is taking time is in the settings activity...

Put this in the setting activity:

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final ProgressDialog dialog = ProgressDialog.show(getParent(), "Please wait..", "Doing stuff..", true);
dialog.setCancelable(false);
dialog.show(); 
Runnable myRun = new Runnable(){
public void run(){
    Looper.myLooper().prepare();
    //DO EVERYTHING YOU WANT!

    //Finally
    runOnUiThread(new Runnable() {
    @Override
    public void run() {
    dialog.dismis();
    }
    });
}
};
Thread T = new Thread(myRun);
T.start();

Remember: In you run() in this block

//DO EVERYTHING YOU WANT!

If anything needs to change the UI, you must do it using

    runOnUiThread(new Runnable() {
    @Override
    public void run() {
        //DO IT
    }
    });
空气里的味道 2024-12-04 14:55:05

你可以使用android的asyntask功能来显示进度对话框参考这个http://developer .android.com/reference/android/os/AsyncTask.html

you can use the asyntask feature of android to show Progress dialog refer this http://developer.android.com/reference/android/os/AsyncTask.html

鯉魚旗 2024-12-04 14:55:05

请参阅我的答案,

“旋转从 SD 卡删除文件夹时的“滚轮”进度对话框

,或者您必须执行异步任务才能执行此操作。

Refer to my answer here,

"Rotating wheel" progress dialog while deleting folder from SD card

or you must go for async task to do this.

勿忘初心 2024-12-04 14:55:05

如果 getParent() 不适合您,请尝试仅使用 TabsActivity.context (或替换父选项卡活动类的名称)。我正在使用嵌套活动,因此使用 getParent() 仍然没有返回对话框的正确上下文。

在尝试了上述建议的 20 种不同变体之后,我将这一行替换为:

AlertDialog.Builder builder = new AlertDialog.Builder(this); 

With:

AlertDialog.Builder builder = new AlertDialog.Builder(TabsActivity.context); 

,它的效果就像一个魅力。您还需要在 TabsActivity 类中创建上下文变量。类似于 onCreate 方法中的 public static TabsActivity context;context=this

If getParent() doesn't work for you, try using just TabsActivity.context (or substitute the name of your parent tab activity class). I am using nested activities and as a result using getParent() is still not returning the right context for the dialog.

After trying 20 different variations of the suggestions above I replaced this line:

AlertDialog.Builder builder = new AlertDialog.Builder(this); 

With:

AlertDialog.Builder builder = new AlertDialog.Builder(TabsActivity.context); 

and it worked like a charm. You'll also need to create the context variable in the TabsActivity class. Something like public static TabsActivity context; and context=this in the onCreate method.

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