Android tabhost + RadioGroup + PopupWindow

Source: Internet
Author: User

I made some changes based on the Code on the Internet, and kept records for learning.

As follows:

Package com. andyidea. tabdemo; import android. app. tabActivity; import android. content. intent; import android. OS. bundle; import android. util. displayMetrics; import android. view. display; import android. view. layoutInflater; import android. view. view; import android. view. view. onClickListener; import android. view. window; import android. widget. compoundButton; import android. widget. linearLayout; import android Oid. widget. popupWindow; import android. widget. radioButton; import android. widget. compoundButton. onCheckedChangeListener; import android. widget. tabHost; import android. widget. toast; public class MainTabActivity extends TabActivity implements OnCheckedChangeListener, OnClickListener {private TabHost mTabHost; private Intent mAIntent; private Intent mBIntent; private Intent mCIntent; private Intent MDIntent; private Intent mEIntent; private PopupWindow mPopupWindow; private region mR1; private RadioButton mR2; private region mR3; private region mR4; private LinearLayout region; private LinearLayout mLinearLayout4; private LinearLayout mLinearLayout5;/*** baseline screen density */public double STANDARD_DENSITY = 160;/** Called when the activity is first created. * // @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); requestWindowFeature (Window. FEATURE_NO_TITLE); setContentView (R. layout. maintabs); this. mAIntent = new Intent (this, AActivity. class); this. mBIntent = new Intent (this, BActivity. class); this. mCIntent = new Intent (this, CActivity. class); this. mDInte Nt = new Intent (this, DActivity. class); this. mEIntent = new Intent (this, EActivity. class); mR1 = (RadioButton) findViewById (R. id. radio_button0); mR1.setOnCheckedChangeListener (this); mR2 = (RadioButton) findViewById (R. id. radio_button1); mR2.setOnCheckedChangeListener (this); mR3 = (RadioButton) findViewById (R. id. radio_button2); mR3.setOnCheckedChangeListener (this); mR4 = (RadioButton) findVi EwById (R. id. radio_button3); mR4.setOnCheckedChangeListener (this); setupIntent (); initPopupMenu ();} private void initPopupMenu () {View mPopupMenu = LayoutInflater. from (this ). inflate (R. layout. popupmenu, null); mPopupWindow = new PopupWindow (rows, (int) (getDensityRatio () * 90), (int) (getDensityRatio () * 180); mLinearLayout1 = (LinearLayout) mPopupMenu. findViewById (R. id. zhanghao); mLinearL Ayout2 = (LinearLayout) mPopupMenu. findViewById (R. id. firav); mLinearLayout3 = (LinearLayout) mPopupMenu. findViewById (R. id. yijian); mLinearLayout4 = (LinearLayout) mPopupMenu. findViewById (R. id. abort); mLinearLayout5 = (LinearLayout) mPopupMenu. findViewById (R. id. update); mLinearLayout1.setOnClickListener (this); mLinearLayout2.setOnClickListener (this); mLinearLayout3.setOnClickListener (this); mLinear Layout4.setOnClickListener (this); mLinearLayout5.setOnClickListener (this);} private void dissmissPopupWindows () {if (mPopupWindow! = Null & mPopupWindow. isShowing () {mPopupWindow. dismiss () ;}} private double getDensityRatio () {DisplayMetrics displayMetrics = new DisplayMetrics (); Display display = getWindowManager (). getdefadisplay display (); display. getMetrics (displayMetrics); double w = displayMetrics. widthPixels; double h = displayMetrics. heightPixels; int CURRENT_DENSITY = displayMetrics. densityDpi; double DENSITY_RATIO = C URRENT_DENSITY/STANDARD_DENSITY; return DENSITY_RATIO ;}@ Override public void onCheckedChanged (CompoundButton buttonView, boolean isChecked) {if (isChecked) {System. out. println ("isChecked"); dissmissPopupWindows (); switch (buttonView. getId () {case R. id. radio_button0: this. mTabHost. setCurrentTabByTag ("A_TAB"); break; case R. id. radio_button1: this. mTabHost. setCurrentTabByTag ("B _TAB"); break; Case R. id. radio_button2: this. mTabHost. setCurrentTabByTag ("C_TAB"); break; case R. id. radio_button3: mPopupWindow. showAsDropDown (mR4, 0, 0); break ;}} private void setupIntent () {this. mTabHost = getTabHost (); TabHost localTabHost = this. mTabHost; localTabHost. addTab (buildTabSpec ("A_TAB", R. string. main_news, R. drawable. icon_0000n, this. mAIntent); localTabHost. addTab (buildTabSpec ("B _TAB", R. string. Main_down, R. drawable. icon_2_n, this. mBIntent); localTabHost. addTab (buildTabSpec ("C_TAB", R. string. main_message, R. drawable. icon_3_n, this. mCIntent); localTabHost. addTab (buildTabSpec ("D_TAB", R. string. more, R. drawable. icon_4_n, this. mDIntent);} private TabHost. tabSpec buildTabSpec (String tag, int resLabel, int resIcon, final Intent content) {return this. mTabHost. newTabSpec (tag ). setIndicato R (getString (resLabel), getResources (). getDrawable (resIcon )). setContent (content) ;}@ Override public void onClick (View v) {dissmissPopupWindows (); switch (v. getId () {case R. id. zhanghao: Toast. makeText (MainTabActivity. this, "Account Management", Toast. LENGTH_SHORT ). show (); break; case R. id. firav: Toast. makeText (MainTabActivity. this, "favorites", Toast. LENGTH_SHORT ). show (); break; case R. id. yijian: Toast. makeText (MainTabActivity. this, "feedback", Toast. LENGTH_SHORT ). show (); break; case R. id. abort: Toast. makeText (MainTabActivity. this, "About Us", Toast. LENGTH_SHORT ). show (); break; case R. id. update: Toast. makeText (MainTabActivity. this, "version Update", Toast. LENGTH_SHORT ). show (); break ;}} package com. andyidea. tabdemo; import android. app. tabActivity; import android. content. intent; import android. OS. bundle; import android. u Til. displayMetrics; import android. view. display; import android. view. layoutInflater; import android. view. view; import android. view. view. onClickListener; import android. view. window; import android. widget. compoundButton; import android. widget. linearLayout; import android. widget. popupWindow; import android. widget. radioButton; import android. widget. compoundButton. onCheckedChangeListener; import android. widget. ta BHost; import android. widget. toast; public class MainTabActivity extends TabActivity listener, OnClickListener {private TabHost mTabHost; private Intent mAIntent; private Intent mBIntent; private Intent mCIntent; private Intent mDIntent; private Intent mEIntent; private PopupWindow listener; private RadioButton mR1; private RadioButton mR2; private RadioButton mR3; private RadioBut Ton mR4; private LinearLayout region; private LinearLayout mLinearLayout2; private LinearLayout mLinearLayout3; private LinearLayout region;/*** benchmark screen density */public double STANDARD_DENSITY = 160; /** Called when the activity is first created. * // @ Overridepublic void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); requestWindowFeatur E (Window. FEATURE_NO_TITLE); setContentView (R. layout. maintabs); this. mAIntent = new Intent (this, AActivity. class); this. mBIntent = new Intent (this, BActivity. class); this. mCIntent = new Intent (this, CActivity. class); this. mDIntent = new Intent (this, DActivity. class); this. mEIntent = new Intent (this, EActivity. class); mR1 = (RadioButton) findViewById (R. id. radio_button0); mR1.setOnCheckedChangeListener (thi S); mR2 = (RadioButton) findViewById (R. id. radio_button1); mR2.setOnCheckedChangeListener (this); mR3 = (RadioButton) findViewById (R. id. radio_button2); mR3.setOnCheckedChangeListener (this); mR4 = (RadioButton) findViewById (R. id. radio_button3); mR4.setOnCheckedChangeListener (this); setupIntent (); initPopupMenu ();} private void initPopupMenu () {View mPopupMenu = LayoutInflater. from (this ). inflate (R. layout. p Opupmenu, null); mPopupWindow = new PopupWindow (rows, (int) (getDensityRatio () * 90), (int) (getDensityRatio () * 180); mLinearLayout1 = (LinearLayout) mPopupMenu. findViewById (R. id. zhanghao); mLinearLayout2 = (LinearLayout) mPopupMenu. findViewById (R. id. firav); mLinearLayout3 = (LinearLayout) mPopupMenu. findViewById (R. id. yijian); mLinearLayout4 = (LinearLayout) mPopupMenu. findViewById (R. id. abort); mL InearLayout5 = (LinearLayout) mPopupMenu. findViewById (R. id. update); listener (this); mLinearLayout2.setOnClickListener (this); mLinearLayout3.setOnClickListener (this); listener (this);} private void dissmissPopupWindows () {if! = Null & mPopupWindow. isShowing () {mPopupWindow. dismiss () ;}} private double getDensityRatio () {DisplayMetrics displayMetrics = new DisplayMetrics (); Display display = getWindowManager (). getdefadisplay display (); display. getMetrics (displayMetrics); double w = displayMetrics. widthPixels; double h = displayMetrics. heightPixels; int CURRENT_DENSITY = displayMetrics. densityDpi; double DENSITY_RATIO = CURRENT_DENSITY/STANDARD_DENSITY; return success ;}@ Overridepublic void onCheckedChanged (CompoundButton buttonView, boolean isChecked) {if (isChecked) {System. out. println ("isChecked"); dissmissPopupWindows (); switch (buttonView. getId () {case R. id. radio_button0: this. mTabHost. setCurrentTabByTag ("A_TAB"); break; case R. id. radio_button1: this. mTabHost. setCurrentTabByTag ("B _TAB"); break; case R. id. radio_button2: this. mTabHost. setCurrentTabByTag ("C_TAB"); break; case R. id. radio_button3: mPopupWindow. showAsDropDown (mR4, 0, 0); break ;}} private void setupIntent () {this. mTabHost = getTabHost (); TabHost localTabHost = this. mTabHost; localTabHost. addTab (buildTabSpec ("A_TAB", R. string. main_news, R. drawable. icon_0000n, this. mAIntent); localTabHost. addTab (buildTabSpec ("B _TAB", R. string. main_down, R. drawable. icon_2_n, this. mBIntent); localTabHost. addTab (buildTabSpec ("C_TAB", R. string. main_message, R. drawable. icon_3_n, this. mCIntent); localTabHost. addTab (buildTabSpec ("D_TAB", R. string. more, R. drawable. icon_4_n, this. mDIntent);} private TabHost. tabSpec buildTabSpec (String tag, int resLabel, int resIcon, final Intent content) {return this. mTabHost. newTabSpec (tag ). setIndicator (getString (resLabel), getResources (). getDrawable (resIcon )). setContent (content) ;}@ Overridepublic void onClick (View v) {dissmissPopupWindows (); switch (v. getId () {case R. id. zhanghao: Toast. makeText (MainTabActivity. this, "Account Management", Toast. LENGTH_SHORT ). show (); break; case R. id. firav: Toast. makeText (MainTabActivity. this, "favorites", Toast. LENGTH_SHORT ). show (); break; case R. id. yijian: Toast. makeText (MainTabActivity. this, "feedback", Toast. LENGTH_SHORT ). show (); break; case R. id. abort: Toast. makeText (MainTabActivity. this, "About Us", Toast. LENGTH_SHORT ). show (); break; case R. id. update: Toast. makeText (MainTabActivity. this, "version Update", Toast. LENGTH_SHORT ). show (); break ;}}}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.