简体   繁体   English

如何将加载屏幕添加到android中的选项卡?

[英]how to add the loading screen in to tab in android?

In my application i'm using two tabs one tab app contacts and other one is all contacts ,in application opening a one tab to other one it tack 10sce ,in that time how to add the same type of message like loading screen etc , 在我的应用程序中,我使用两个选项卡,一个选项卡应用程序联系人,另一个是所有联系人,在应用程序打开一个选项卡到另一个,它粘贴10sce,在那个时间如何添加相同类型的消息,如加载屏幕等,

plz tell me how to do that in my android app? 请告诉我如何在我的Android应用程序中这样做?

      public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tabview);



    final TabHost tabHost = getTabHost(); 



    TextView txtTab = new TextView(this);
    txtTab.setText("Mobell Contacts");
    txtTab.setPadding(8, 9, 8, 9);
    txtTab.setTextColor(Color.WHITE);
    txtTab.setTextSize(14);
    //txtTab.setTypeface(localTypeface1);
    txtTab.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);


    TabHost.TabSpec spec;
    // Initialize a TabSpec for each tab and add it to the TabHost
    spec = tabHost.newTabSpec("tab1").setIndicator(txtTab).setContent(new Intent(this, ContactList.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
    tabHost.addTab(spec);






    TextView txtTab1 = new TextView(this);
    txtTab1.setText("All Contacts");
    txtTab1.setPadding(8, 9, 8, 9);
    txtTab1.setTextColor(Color.WHITE);
    txtTab1.setTextSize(14);
    //txtTab.setTypeface(localTypeface1);
    txtTab1.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);


    TabHost.TabSpec spec1;
    // Initialize a TabSpec for each tab and add it to the TabHost
    spec1 = tabHost.newTabSpec("Tab2").setIndicator(txtTab1).setContent(new Intent(this, TabAct2.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
    tabHost.addTab(spec1);



    tabHost.getTabWidget().getChildAt(0).setBackgroundColor(Color.parseColor("#101010"));
    tabHost.getTabWidget().getChildAt(1).setBackgroundColor(Color.parseColor("#848284"));





    tabHost.setOnTabChangedListener(new OnTabChangeListener() {

        @Override
        public void onTabChanged(String tabId) {
            for(int i=0;i<tabHost.getTabWidget().getChildCount();i++)
            {
                tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#848284"));

            }
            tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundColor(Color.parseColor("#181418"));   


        }
    });





}




   }

see this code plz tell how to add the progressDialog when tab change one to other... 看到这段代码plz告诉如何在tab切换到其他时添加progressDialog ...

You can use AsyncTask best way to display Loading screen... 您可以使用AsyncTask最佳方式显示加载屏幕...

onCreate()
{
    dialog.show();
    new LoadData().execute(null);
}
--------------------------------------------------------------------------     
    private class LoadData extends AsyncTask<URL, Integer, Boolean> {
         protected Long doInBackground(URL... urls) {
             // your code to load data here <------
             return true;
         }

         protected void onPostExecute(Boolean result) {
             dialog.dismiss();
         }
     }

You can do something like this for showing progress - 你可以做这样的事情来展示进步 -

    public class MainActivity extends Activity {
        Button b;
        ProgressBar pb1;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            b = (Button)findViewById(R.id.button1);
            pb1 = (ProgressBar)findViewById(R.id.progressBar1);
            }

        public void click(View v){
            new Thread("thread1"){
                public void run(){
                    try{
                        for(int i=0; i<10; i++)
                        {
                            Thread.sleep(4000);
                            pb1.setProgress(i*10);
                        }
                    }
                    catch(InterruptedException e){
                        e.printStackTrace();
                    }
                };
            }.start();  
        }
    }

In case you want to show a message, use Spinner. 如果您想显示消息,请使用Spinner。

使用可见性开/关在屏幕中心显示进度条

Since you are using TabActivity (and you haven't posted your work around yet), so, I am guessing you have used 既然你正在使用TabActivity (并且还没有发布你的工作),那么,我猜你已经使用过了

  1. TabActivity
  2. Activity (for both tabs) Activity (适用于两个标签)

Now, when you switch from one tab to another you are actually calling Activity . 现在,当您从一个选项卡切换到另一个选项卡时,您实际上正在调用Activity So, just create an AsyncTask in the Activity which takes time to load. 因此,只需在Activity创建一个AsyncTask ,它需要一些时间来加载。 For instance, 例如,

class ContactLoadAsync extends AsyncTask<Void, Void, Void> {
    ProgressDialog pd;

    @Override
    protected void onPreExecute()
        pd = ProgressDialog.show(YourActivity.this, "title", "loading", true, false);
    }

    @Override
    protected Void doInBackground(Void... params) {
        // call your contact loading functions here

        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        pd.dismiss();
    }

}

And, call this AsyncTask in onCreate() of your Activity . 并且,在您的Activity onCreate()中调用此AsyncTask

new ContactLoadAsync().execute();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM