展会信息港展会大全

android的tabhost+RadioGroup+PopupWindow
来源:互联网   发布日期:2015-10-03 11:12:13   浏览:2092次  

导读:根据网上的代码稍作修改了下,放着记录学习。效果图如下:package com.andyidea.tabdemo; import android.app.TabActivity; ...

根据网上的代码稍作修改了下,放着记录学习。

效果图如下:

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.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 RadioButton mR1;

private RadioButton mR2;

private RadioButton mR3;

private RadioButton mR4;

private LinearLayout mLinearLayout1;

private LinearLayout mLinearLayout2;

private LinearLayout mLinearLayout3;

private LinearLayout mLinearLayout4;

private LinearLayout mLinearLayout5;

/**

* 基准屏幕密度

*/

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.mDIntent = 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) findViewById(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(mPopupMenu, (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);

mLinearLayout5 = (LinearLayout) mPopupMenu.findViewById(R.id.update);

mLinearLayout1.setOnClickListener(this);

mLinearLayout2.setOnClickListener(this);

mLinearLayout3.setOnClickListener(this);

mLinearLayout4.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().getDefaultDisplay();

display.getMetrics(displayMetrics);

double w = displayMetrics.widthPixels;

double h = displayMetrics.heightPixels;

int CURRENT_DENSITY = displayMetrics.densityDpi;

double DENSITY_RATIO = CURRENT_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_1_n, 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);

}

@Override

public void onClick(View v)

{

dissmissPopupWindows();

switch (v.getId())

{

case R.id.zhanghao:

Toast.makeText(MainTabActivity.this, "账号管理", Toast.LENGTH_SHORT)

.show();

break;

case R.id.firav:

Toast.makeText(MainTabActivity.this, "收藏夹", Toast.LENGTH_SHORT)

.show();

break;

case R.id.yijian:

Toast.makeText(MainTabActivity.this, "意见反馈", Toast.LENGTH_SHORT)

.show();

break;

case R.id.abort:

Toast.makeText(MainTabActivity.this, "关于我们", Toast.LENGTH_SHORT)

.show();

break;

case R.id.update:

Toast.makeText(MainTabActivity.this, "版本更新", Toast.LENGTH_SHORT)

.show();

break;

}

}

}

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.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 RadioButton mR1;

private RadioButton mR2;

private RadioButton mR3;

private RadioButton mR4;

private LinearLayout mLinearLayout1;

private LinearLayout mLinearLayout2;

private LinearLayout mLinearLayout3;

private LinearLayout mLinearLayout4;

private LinearLayout mLinearLayout5;

/**

* 基准屏幕密度

*/

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.mDIntent = 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) findViewById(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(mPopupMenu, (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);

mLinearLayout5 = (LinearLayout) mPopupMenu.findViewById(R.id.update);

mLinearLayout1.setOnClickListener(this);

mLinearLayout2.setOnClickListener(this);

mLinearLayout3.setOnClickListener(this);

mLinearLayout4.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().getDefaultDisplay();

display.getMetrics(displayMetrics);

double w = displayMetrics.widthPixels;

double h = displayMetrics.heightPixels;

int CURRENT_DENSITY = displayMetrics.densityDpi;

double DENSITY_RATIO = CURRENT_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_1_n, 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);

}

@Override

public void onClick(View v)

{

dissmissPopupWindows();

switch (v.getId())

{

case R.id.zhanghao:

Toast.makeText(MainTabActivity.this, "账号管理", Toast.LENGTH_SHORT)

.show();

break;

case R.id.firav:

Toast.makeText(MainTabActivity.this, "收藏夹", Toast.LENGTH_SHORT)

.show();

break;

case R.id.yijian:

Toast.makeText(MainTabActivity.this, "意见反馈", Toast.LENGTH_SHORT)

.show();

break;

case R.id.abort:

Toast.makeText(MainTabActivity.this, "关于我们", Toast.LENGTH_SHORT)

.show();

break;

case R.id.update:

Toast.makeText(MainTabActivity.this, "版本更新", Toast.LENGTH_SHORT)

.show();

break;

}

}

}

赞助本站

人工智能实验室

相关热词: android开发 教程

AiLab云推荐
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港