单击选定选项卡后,Android选项卡活动将返回

时间:2014-08-20 10:11:20

标签: android android-tabhost android-tabs android-tabactivity

我正在处理我正在制作tabactivity的应用程序,在第一个屏幕上我有一些按钮,当我点击按钮主页选项卡活动启动,并且选择了主页选项卡活动,现在我想要当主页选项卡单击选定的主页选项卡活动时,选择活动,它可以返回,是否可以?

这是选项卡活动,当它启动,主页选项卡被选中,我希望它再次点击选定的主页选项卡它回到以前的活动,任何解决方案将非常感谢谢谢。 enter image description here

这是我点击按钮时的第一个活动代码:

new_car.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        Intent newCar_intent = new Intent(Car_shop_home.this, Used_vehicle_tabbarnew.class);
        newCar_intent.putExtra("csHeader_logo", getIntent().getExtras().getString("csHeader_logo"));
        newCar_intent.putExtra("csMakeName", getIntent().getExtras().getString("csMakeName"));
        newCar_intent.putExtra("csMakeID", getIntent().getExtras().getString("csMakeID"));
        newCar_intent.putExtra("bCarShop", true);
        newCar_intent.putExtra("vehicle_type", "1");
        newCar_intent.putExtra("DealerEmergencyNumbers", getIntent().getExtras().getString("DealerEmergencyNumbers"));

        startActivity(newCar_intent); 


        //startActivity(k);
    }
   });

这是我的tabbar类:

public class Used_vehicle_tabbarnew extends TabActivity{
String csMakeName;
String csMakeID;
String csDealerID;
Bundle CarData;
String csAdvert;
String csImageUrl;
String csHeader_logo;
ProgressDialog myPd_ring;

JSONArray jsonNumberArray;
List<String> emergencyNumberList=new ArrayList<String>();
String csEmergencyNumber = "";
String csSavedDealerID;
String DealerEmergencyNumbers = "";
private ArrayAdapter<String> myEmergencyAdapter;

boolean emergency = false;

TextView title;
TextView title1;
TextView title2;
TextView title3;

int tabClick = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Log.e("CMH", "In Tabbar");

    final TabHost mTabHost = getTabHost();
    createUsedVehicleTab(mTabHost);
    createFinanceTab(mTabHost);
//  createEmergencyTab(mTabHost);
    createFavouritesTab(mTabHost);
//  createMoreTab(mTabHost);
    createProfileTab(mTabHost);

    DealerEmergencyNumbers =  getIntent().getExtras().getString("DealerEmergencyNumbers");


     csHeader_logo=getIntent().getExtras().getString("csHeader_logo");
     getTabHost().setOnTabChangedListener(new OnTabChangeListener() {


        @Override
           public void onTabChanged(String tabId)
           {

               if (getTabHost().getCurrentTabTag().equals("tab_emergency"))
               { 
                   UpdateList();
                   if(csSavedDealerID == null)
                       showMessage("To benefit from the full features of this app please fill in your profile.");
                   else
                       showCutomMessage("Would you like to call for emergency assistance");  

                   emergency = true;
                   mTabHost.setCurrentTab(tabClick);


               }
               else if (emergency == true)
               {
                   emergency = false;
               }
               else if (getTabHost().getCurrentTabTag().equals("tab_home"))
               { 
                   finish();
               }
               tabClick = getTabHost().getCurrentTab();

               if(tabClick == 0) {
                    //   title.setTextColor(Color.RED);
                       title.setTextColor(Color.parseColor("#a30000"));
                   }else{
                       title.setTextColor(Color.WHITE);
                   }
                   if(tabClick == 1) {
                       title1.setTextColor(Color.parseColor("#a30000"));
                   }else{
                       title1.setTextColor(Color.WHITE);
                   }
                   if(tabClick == 2) {
                       title2.setTextColor(Color.parseColor("#a30000"));
                   }else{
                       title2.setTextColor(Color.WHITE);
                   }
                   if(tabClick == 3) {
                       title3.setTextColor(Color.parseColor("#a30000"));
                   }else{
                       title3.setTextColor(Color.WHITE);
                   }


             }
           });


}

public void createUsedVehicleTab(TabHost mTabHost)
{
    // Create Intent for My Info

      csMakeID=getIntent().getExtras().getString("csMakeID");
      csHeader_logo=getIntent().getExtras().getString("csHeader_logo");
      String csMakeName=getIntent().getExtras().getString("csMakeName");

    Intent intent_settings = new  Intent(Used_vehicle_tabbarnew.this,Used_vehicle_group_actvitynew.class);
    //intent_settings.putExtras(CarData);

    intent_settings.putExtra("csMakeID",csMakeID);
    intent_settings.putExtra("csMakeName",csMakeName);
    intent_settings.putExtra("csHeader_logo", csHeader_logo);
    intent_settings.putExtra("bCarShop",  getIntent().getExtras().getBoolean("bCarShop", false));
    intent_settings.putExtra("vehicle_type",  getIntent().getExtras().getString("vehicle_type"));

     TabHost.TabSpec spec = mTabHost.newTabSpec("tab_used_home");       

        View tabIndicator = LayoutInflater.from(this).inflate(R.layout.tab_layout, getTabWidget(), false);

        title = (TextView) tabIndicator.findViewById(R.id.tab_title);
        title.setText("Home");
        title.setTextColor(Color.parseColor("#a30000"));
        ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon);
        icon.setImageResource(R.drawable.tab_home);

        spec.setIndicator(tabIndicator);
        spec.setContent(intent_settings);
        mTabHost.addTab(spec);


        int height = mTabHost.getTabWidget().getChildAt(0).getLayoutParams().height;

        //Log.e("CMH", "tab Hight " + height);
}


public void createFinanceTab(TabHost mTabHost)
{
    // Create Intent for My Info

    Intent intent_settings = new Intent(Used_vehicle_tabbarnew.this,
            Finance.class);

    intent_settings.putExtra("csHeader_logo", csHeader_logo);
    intent_settings.putExtra("bCarShop",  getIntent().getExtras().getBoolean("bCarShop", false));
    // mTabHost.addTab(mTabHost.newTabSpec("tab_my_info").setIndicator(language.txt_myinfo[language.language], getResources().getDrawable(R.drawable.tab_my_info)).setContent(intent_my_info));

     TabHost.TabSpec spec = mTabHost.newTabSpec("tab_finance");     

        View tabIndicator = LayoutInflater.from(this).inflate(R.layout.tab_layout, getTabWidget(), false);

         title1 = (TextView) tabIndicator.findViewById(R.id.tab_title);
        title1.setText("Finance");
        title1.setTextColor(Color.parseColor("#ffffff"));
        ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon);
        icon.setImageResource(R.drawable.tab_finance);

        spec.setIndicator(tabIndicator);
        spec.setContent(intent_settings);
        mTabHost.addTab(spec);
}


public void createFavouritesTab(TabHost mTabHost)
{
    // Create Intent for My Info

    Intent intent_settings = new Intent(Used_vehicle_tabbarnew.this,
            Favourities.class);

    intent_settings.putExtra("bCarShop", true);
    intent_settings.putExtra("csHeader_logo", csHeader_logo);
    intent_settings.putExtra("Previous_Activity", "More_group_activity");
    // mTabHost.addTab(mTabHost.newTabSpec("tab_my_info").setIndicator(language.txt_myinfo[language.language], getResources().getDrawable(R.drawable.tab_my_info)).setContent(intent_my_info));

     TabHost.TabSpec spec = mTabHost.newTabSpec("tab_favourites");      

        View tabIndicator = LayoutInflater.from(this).inflate(R.layout.tab_layout, getTabWidget(), false);

         title2 = (TextView) tabIndicator.findViewById(R.id.tab_title);
        title2.setText("My favourites");
        title2.setTextColor(Color.parseColor("#ffffff"));
        ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon);
        icon.setImageResource(R.drawable.tab_favourites);

        spec.setIndicator(tabIndicator);
        spec.setContent(intent_settings);
        mTabHost.addTab(spec);


}



public void createProfileTab(TabHost mTabHost)
{
    // Create Intent for My Info

    Intent intent_settings = new Intent(Used_vehicle_tabbarnew.this,
            MyProfile.class);

    intent_settings.putExtra("bCarShop", true);
    intent_settings.putExtra("csHeader_logo", csHeader_logo);
    intent_settings.putExtra("Previous_Activity", "More_group_activity");
    // mTabHost.addTab(mTabHost.newTabSpec("tab_my_info").setIndicator(language.txt_myinfo[language.language], getResources().getDrawable(R.drawable.tab_my_info)).setContent(intent_my_info));

     TabHost.TabSpec spec = mTabHost.newTabSpec("tab_profile");     

        View tabIndicator = LayoutInflater.from(this).inflate(R.layout.tab_layout, getTabWidget(), false);

         title3 = (TextView) tabIndicator.findViewById(R.id.tab_title);
        title3.setText("My profile");
        title3.setTextColor(Color.parseColor("#ffffff"));
        ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon);
        icon.setImageResource(R.drawable.tab_profile);

        spec.setIndicator(tabIndicator);
        spec.setContent(intent_settings);
        mTabHost.addTab(spec);



}

 public void showCutomMessage(String message)
    {

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

        builder.setMessage(message).setCancelable(true).setPositiveButton(
                "Yes", new DialogInterface.OnClickListener()
                {
                    public void onClick(DialogInterface dialog, int id)
                    {

                        dialog.cancel();

                        if(csEmergencyNumber.length() <= 0 && emergencyNumberList.size() <= 0)
                        {
                            showMessage("No Emergency Number found.");
                         }
                         else if (csEmergencyNumber.length() > 0)
                         {
                             String url = "tel:" + csEmergencyNumber;
                             Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
                             startActivity(intent);
                         }
                         else
                         {
                             if (emergencyNumberList.size() == 1)
                             {
                                 String url = "tel:" + emergencyNumberList.get(0);
                                 Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
                                 startActivity(intent);
                             }
                             else
                             {
                                 showEmergencyNumberDialog();
                             }
                         }

                    }
                }).setNegativeButton(
                        "No", new DialogInterface.OnClickListener()
                        {
                            public void onClick(DialogInterface dialog, int id)
                            {

                                dialog.cancel();

                            }
                        });


        AlertDialog alert = builder.create();
        alert.setMessage(message);


        alert.show();

    }

 public void UpdateList()
 {
     try
        {
            emergencyNumberList.clear();
            jsonNumberArray = new JSONArray(DealerEmergencyNumbers.toString());

            SharedPreferences app_preferences = PreferenceManager.getDefaultSharedPreferences(Used_vehicle_tabbarnew.this);
            csSavedDealerID = app_preferences.getString("DealerID", null);

            for(int nJasonIndex = 0; nJasonIndex < jsonNumberArray.length(); nJasonIndex++)
            {
                JSONObject emergencyObj =jsonNumberArray.getJSONObject(nJasonIndex);
                String csNumber = emergencyObj.getString("EmergencyNumber");
                String csNumber2 = emergencyObj.getString("office_hours_num"); 

                String csMakeDealerID = emergencyObj.getString("DealerID");
                if(csSavedDealerID != null && csSavedDealerID.equalsIgnoreCase(csMakeDealerID))
                {
                    csEmergencyNumber = csNumber;
                }
                else
                {
                    if(!csNumber.equals(""))
                        emergencyNumberList.add("After Hours: " + csNumber);

                    if(!csNumber2.equals(""))
                        emergencyNumberList.add("Office Hours: " + csNumber2);
                }

            }

            if(emergencyNumberList.size() > 1)
            {
                myEmergencyAdapter =new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,emergencyNumberList); 
                myEmergencyAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            }




        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
 }

 public void showMessage(String message)
    {

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

        builder.setMessage(message).setCancelable(true).setPositiveButton(
                "OK", new DialogInterface.OnClickListener()
                {
                    public void onClick(DialogInterface dialog, int id)
                    {

                        dialog.cancel();
                    }
                });


        AlertDialog alert = builder.create();
        alert.setMessage(message);


        alert.show();

    }

 void showEmergencyNumberDialog()
 {
     ArrayAdapter adapter = new  ArrayAdapter(this,android.R.layout.simple_spinner_dropdown_item, emergencyNumberList);

     AlertDialog.Builder builder = new AlertDialog.Builder(this);
     builder.setTitle("");
     builder.setAdapter(adapter, new DialogInterface.OnClickListener() {
         public void onClick(DialogInterface dialog, int item)
         {
             if (item >= 0)
             {
                 String emergencyNumber = emergencyNumberList.get(item);
                 String[] parts = emergencyNumber.split(": ");
                 String number = parts[1]; // 034556

                 String url = "tel:" + number;
                 Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
                 startActivity(intent);
             }

         }
     });

     AlertDialog alert = builder.create();
     alert.show();
 }


}

1 个答案:

答案 0 :(得分:0)

抱歉,我不太关注你的问题。如果你能再次解释,也许是一个点形式,那将是很棒的。我的理解是,你希望有不同的行为取决于&#34; Home&#34;按钮被选中或首先被选中。

以下是我在选择标签时覆盖TabHost默认处理例程的答案: https://stackoverflow.com/a/26475299/3731039

我认为我的解决方案不适合您。如果你进一步解释,我可能会修改我的答案。

初始化标签:

TabWidget w = tabHost.getTabWidget();
View view = w.getChildAt(w.getChildCount() - 1);
// here is the magic!
view.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        handleHomeSelected();
    }
});

您的自定义操作:

protected void handleHomeSelected(){
    // do your logic
}