单击选项卡上的进度条。

时间:2011-08-24 17:06:49

标签: android

我的应用程序中有5个选项卡,登录后默认加载第一个选项卡,但是当我点击其他选项卡时,我基本上从服务器获取数据,我需要显示进度条,我无法抓住保持新的单击选项卡,我可以使用getcurrentTab()获取当前选项卡单击。请帮帮我这个。以下是我正在使用的代码。

Thanks
Max


package com.sof.android;


import android.app.AlertDialog;
import android.app.TabActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.Toast;

public class Home extends TabActivity {

    TabHost tabHost;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(R.layout.home);
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.home_title);

       // Ask New Question 
        TextView username  = (TextView) findViewById(R.id.usernameBar);
        username.setText(Global.username);



        //Logout 

        Button logOut = (Button) this.findViewById(R.id.logoutBar);
        logOut.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                AlertDialog.Builder builder = new AlertDialog.Builder(Home.this);
                builder.setMessage("Are you sure you want to Log Out?")
                       .setCancelable(false)
                       .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                           public void onClick(DialogInterface dialog, int id) {
                                finish();
                           }
                       })
                       .setNegativeButton("No", new DialogInterface.OnClickListener() {
                           public void onClick(DialogInterface dialog, int id) {
                                dialog.cancel();
                           }
                       });
                AlertDialog alert = builder.create();
                alert.show();



            }
        });

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

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

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

        // Do the same for the other tabs
        intent = new Intent().setClass(this, b.class);
        spec = tabHost.newTabSpec("albums").setIndicator("Ask",
                          res.getDrawable(R.drawable.b))
                      .setContent(intent);
        tabHost.addTab(spec);

        intent = new Intent().setClass(this, c.class);
        spec = tabHost.newTabSpec("songs").setIndicator("Questions",
                          res.getDrawable(R.drawable.c))
                      .setContent(intent);
        tabHost.addTab(spec);

        intent = new Intent().setClass(this, d.class);
        spec = tabHost.newTabSpec("tutorial").setIndicator("Tutorials",
                          res.getDrawable(R.drawable.d))
                      .setContent(intent);
        tabHost.addTab(spec);

        intent = new Intent().setClass(this, e.class);
        spec = tabHost.newTabSpec("account").setIndicator("Account",
                          res.getDrawable(R.drawable.e))
                      .setContent(intent);
        tabHost.addTab(spec);
        tabHost.setCurrentTab(0);



        getTabWidget().getChildAt(getTabHost().getCurrentTab()).setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                if(getTabHost().getCurrentTab() == 1){

                    System.out.println("home home home");
                    tabHost.setCurrentTab(0);
                }

                if(getTabHost().getCurrentTab() == 2){

                    System.out.println("i am clicking tabs now overrided method");
                }
                //do whatever you need

            }
        });


      /* 
            tabHost.setOnTabChangedListener(new OnTabChangeListener(){
            @Override
            public void onTabChanged(String tabId) {
                if(TAB_1_TAG.equals(tabId)) {
                    //destroy earth
                }
                if(TAB_2_TAG.equals(tabId)) {
                    //destroy mars
                }
            }}); 
      */ 

    }



    public boolean onKeyDown(int keyCode, KeyEvent event) {
         if (keyCode == KeyEvent.KEYCODE_BACK) {

         //preventing default implementation previous to android.os.Build.VERSION_CODES.ECLAIR
             Toast.makeText(getApplicationContext(), "Please Use Log Out.", Toast.LENGTH_LONG).show();   
         return true;

         }

         return super.onKeyDown(keyCode, event);    
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.icon:     
                                /*try{
                                Intent previewMessage = new Intent(Home.this, a.class);
                                System.out.println("preview message "+previewMessage);

                                TabGroupActivity parentActivity = new TabGroupActivity();
                                System.out.println("parentActivity message "+parentActivity);

                                parentActivity.startChildActivity("home", Global.aIns);

                                }catch(Exception ex){
                                    System.out.println("Caought exception :- "+ex.getMessage());
                                }*/
                                tabHost.setCurrentTab(0);
                                break;
            case R.id.text:     AlertDialog.Builder builder = new AlertDialog.Builder(Home.this);
                                builder.setMessage("Are you sure you want to Log Out?")
                                       .setCancelable(false)
                                       .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                                           public void onClick(DialogInterface dialog, int id) {
                                                finish();
                                           }
                                       })
                                       .setNegativeButton("No", new DialogInterface.OnClickListener() {
                                           public void onClick(DialogInterface dialog, int id) {
                                                dialog.cancel();
                                           }
                                       });
                                AlertDialog alert = builder.create();
                                alert.show();
                                break;

            case R.id.icontext: //Intent Account = new Intent(Home.this, e.class);
                                //a aIns = new a();
                                /*System.out.println("Global.eInsObject is" +Global.eInsObject);
                                System.out.println("Global.eIns is" +Global.eIns);

                                TabGroupActivity parentActivityE = (TabGroupActivity) Global.eInsObject;
                                //System.out.println("parentActivityE is "+parentActivityE);
                                parentActivityE.startChildActivity("myAccount", Global.eIns);

                                //parentActivityE.switchTabs("myaccount",);

                                //Window window = getLocalActivityManager().startActivity("myaccount",Account.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
                                 // if (window != null) {
                                  //    Global.mIdList.add("myaccount");
                                   //   try{
                                    //  setContentView(window.getDecorView());
                                     // }catch(Exception ex){
                                    //    System.out.println("exception is "+ex.getMessage());
                                     // }
                                  //}
                                  */
                                tabHost.setCurrentTab(4);
                                break;
        }
        return true;
    }
}

0 个答案:

没有答案