- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以,在我的项目中有 6 个选项卡。在主要 Activity 中,它使用这样的标签创建标签主机
tabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
tabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
tabHost.addTab(tabHost.newTabSpec("main").setIndicator(""),Home.class, null);
tabHost.addTab(tabHost.newTabSpec("assistant").setIndicator("",getResources().getDrawable(R.drawable.btn_assistant)), Shop.class,null); //not clickable right now
tabHost.addTab(tabHost.newTabSpec("coupon").setIndicator("",getResources().getDrawable(R.drawable.btn_coupon)), Coupon.class,null);
tabHost.addTab(tabHost.newTabSpec("shop").setIndicator("",getResources().getDrawable(R.drawable.btn_shop)), Shop.class,null);
然后,在某个时候,我想实现:
1) 将选项卡更改为选项卡 2
2) 调用选项卡 2 中的函数
所以,我尝试使用广播接收器,但它似乎运行但 onReceive 未在选项卡 2 上触发
在主类中
setTab(0);
msg.putExtra("isEnter", 0);
sendBroadcast(msg);
Log.d("test1","boardcast");
在tab2类中
private class DataUpdateReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.d("test1","test");
if (intent.getAction().equals("shopStatus")) {
Log.d("test1","test2");
int type = intent.getIntExtra("isEnter",-1);
Log.d("test1",""+type);
if (type == 0) {
helper.setVisibility(View.VISIBLE);
helper_txt.setText(R.string.enter_mall);
yes.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
prefs.edit().putBoolean("isShowHelper", true).commit();
ctxActivity.setTab(4);
}
});
no.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
helper.setVisibility(View.GONE);
}
});
}
}
}
感谢您的帮助,任何可以实现的方式都会接受。
最佳答案
我已经在下面发布了我的代码。我不知道它是否适合你。但是,这段代码会给你一些想法。
公共(public)类 CollisionTabView 扩展了 FragmentActivity 实现OnClickListener、OnTabChangeListener、CallAPIFinishedListener、OtherDriverInfoData、AccidentInfoData、AdditionalDataInfo、PoliceInfoData{
private FragmentTabHost mTabHost;
Button collisionhome, collision_center;
TextView title;
int previousId = 0, previous = 0;
String tTitle = null;
boolean back = false;
private enum Tabs {
accident_info, other_driver, police_info, additional_nfo;
}
SQLiteDatabase database;
DatabaseHelper dataHelper;
DealershipApplication application;
CallAPIFinishedListener callAPIFinishedListener;
CollisionTabView mContext;
static Bundle data;
private ExceptionHandler miCrashHandler;
@Override
public void onStop() {
super.onStop();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.collision_tabview);
callAPIFinishedListener = this;
mContext = this;
miCrashHandler=new ExceptionHandler(mContext);
Thread.setDefaultUncaughtExceptionHandler(miCrashHandler);
new Accident_info(this,this);
new Other_Driver_Info(this,this);
new AdditionalInfo(this,this);
new PoliceInfo(this,this);
application = (DealershipApplication) getApplicationContext();
mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
title = (TextView) findViewById(R.id.collisiontitle);
title.setText(getResources().getString(R.string.collisiontab_collisioninfo));
title.setTextColor(Color.WHITE);
collisionhome = (Button) findViewById(R.id.collisionhome);
collisionhome.setText("Back");
collisionhome.setOnClickListener(this);
application.changeTypeface(title, collisionhome);
collisionhome.setVisibility(View.VISIBLE);
collision_center = (Button) findViewById(R.id.collision_centerbutton);
collision_center.setOnClickListener(this);
mTabHost.addTab(
mTabHost.newTabSpec("accident_info").setIndicator(
"Accident Info"), Accident_info.class, null);
mTabHost.addTab(
mTabHost.newTabSpec("other_driver").setIndicator(
"Other Driver's Info"), Other_Driver_Info.class, null);
mTabHost.addTab(
mTabHost.newTabSpec("police_info").setIndicator("Police Info"),
PoliceInfo.class, null);
mTabHost.addTab(
mTabHost.newTabSpec("additional_nfo").setIndicator(
"Additional Info"), AdditionalInfo.class, null);
mTabHost.setOnTabChangedListener(this);
setTabsStyle();
mTabHost.setCurrentTab(0);
setActive(0, R.drawable.tab_active, R.drawable.tab_inactive,
R.drawable.tab_inactive, R.drawable.tab_inactive);
back = false;
database = DealershipApplication.openDb(mContext);
dataHelper = DealershipApplication.getDatabaseHelper(mContext);
}
@Override
public void onBackPressed() {
try{
/*if(tabreceiver!=null) CollisionTabView.this.unregisterReceiver(tabreceiver);*/
Accident_info.updated = false;
Other_Driver_Info.updated = false;
PoliceInfo.updated = false;
AdditionalInfo.updated = false;
}catch(Exception e){
//e.printStackTrace();
}
super.onBackPressed();
}
private void setTabsStyle() {
// TODO Auto-generated method stub
for (int i = 0; i < mTabHost.getTabWidget().getChildCount(); i++) {
final View view = mTabHost.getTabWidget().getChildTabViewAt(i);
if (view != null) {
final View textView = view.findViewById(android.R.id.title);
if (textView instanceof TextView) {
((TextView) textView).setGravity(Gravity.CENTER);
((TextView) textView).setSingleLine(false);
textView.getLayoutParams().height = ViewGroup.LayoutParams.MATCH_PARENT;
textView.getLayoutParams().width = ViewGroup.LayoutParams.WRAP_CONTENT;
if (i == 0) {
((TextView) textView).setGravity(Gravity.CENTER);
mTabHost.getTabWidget().getChildAt(0)
.setBackgroundResource(R.drawable.tab_active);
((TextView) textView).setTextAppearance(this,
R.style.collisiontext);
((TextView) textView).setTextColor(Color.WHITE);
} else if (i == 1) {
((TextView) textView).setGravity(Gravity.CENTER);
((TextView) textView).setTextAppearance(this,
R.style.collisiontext);
((TextView) textView).setTextColor(Color.WHITE);
mTabHost.getTabWidget().getChildAt(1)
.setBackgroundResource(R.drawable.tab_inactive);
} else if (i == 2) {
((TextView) textView).setGravity(Gravity.CENTER);
// title.setText(getResources().getString(R.string.collisiontab_policeinfo));
((TextView) textView).setTextAppearance(this,
R.style.collisiontext);
((TextView) textView).setTextColor(Color.WHITE);
mTabHost.getTabWidget().getChildAt(2)
.setBackgroundResource(R.drawable.tab_inactive);
} else if (i == 3) {
((TextView) textView).setGravity(Gravity.CENTER);
// title.setText(getResources().getString(R.string.collisiontab_additionalinfo));
((TextView) textView).setTextAppearance(this,
R.style.collisiontext);
((TextView) textView).setTextColor(Color.WHITE);
mTabHost.getTabWidget().getChildAt(3)
.setBackgroundResource(R.drawable.tab_inactive);
}
}
}
}
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.collision_centerbutton:
collision_centerbutton();
break;
case R.id.collisionhome:
back = true;
collisionhome();
break;
default:
break;
}
}
private void collisionhome() {
// TODO Auto-generated method stub
boolean check1 = Accident_info.updated;
boolean check2 = Other_Driver_Info.updated;
boolean check3 = PoliceInfo.updated;
boolean check4 = AdditionalInfo.updated;
if (check1 == false && check2 == false && check3 == false
&& check4 == false) {
finish();
} else {
switch (mTabHost.getCurrentTab()) {
case 0:
tTitle = "Accident Info";
break;
case 1:
tTitle = "Other Driver's Info";
break;
case 2:
tTitle = "Police Info";
break;
case 3:
tTitle = "Additional Info";
break;
default:
break;
}
setDialog(mTabHost.getCurrentTab(), tTitle, true);
}
}
private void collision_centerbutton() {
// TODO Auto-generated method stub
try {
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent
.setData(Uri.parse("tel:"
+ getResources().getString(
R.string.collision_center_phno)));
startActivity(callIntent);
} catch (ActivityNotFoundException activityException) {
Log.e("Calling a Phone Number", "Call failed"+ activityException);
}
}
private void setTab(int pre, boolean clear) {
callFunction(pre, clear);
}
@Override
public void onTabChanged(String tabId) {
// TODO Auto-generated method stub
boolean tab3 = PoliceInfo.updated;
boolean tab2 = Other_Driver_Info.updated;
boolean tab4 = AdditionalInfo.updated;
boolean tab1 = Accident_info.updated;
boolean vehicle = Accident_info.vehicleSelected;
if (!vehicle) {
new DoToast(getBaseContext(), getResources().getString(
R.string.select_vehicle_toast), 200);
mTabHost.setCurrentTab(0);
} else {
Log.d("Collistion Tab View:::::::::", "Inside Else::::of Tab Changed::::");
InputMethodManager inputManager = (InputMethodManager)
CollisionTabView.this.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(CollisionTabView.this.getCurrentFocus().getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);
// previous = mTabHost.getCurrentTab();
Tabs tabs = Tabs.valueOf(tabId);
Log.i("previous", previous + " previous");
previousId = previous;
switch (tabs) {
case accident_info:
previous = 0;
setActive(0, R.drawable.tab_active, R.drawable.tab_inactive,
R.drawable.tab_inactive, R.drawable.tab_inactive);
tTitle = "Accident Info";
break;
case other_driver:
Log.d("Collistion Tab View:::::::::", "Inside Other Driver infor::::");
previous = 1;
tTitle = "Other Driver's Info";
setActive(1, R.drawable.tab_inactive, R.drawable.tab_active,
R.drawable.tab_inactive, R.drawable.tab_inactive);
break;
case police_info:
previous = 2;
tTitle = "Police Info";
setActive(2, R.drawable.tab_inactive, R.drawable.tab_inactive,
R.drawable.tab_active, R.drawable.tab_inactive);
break;
case additional_nfo:
previous = 3;
tTitle = "Additional Info";
setActive(3, R.drawable.tab_inactive, R.drawable.tab_inactive,
R.drawable.tab_inactive, R.drawable.tab_active);
break;
default:
break;
}
if (tab1)
setDialog(0, "Accident Info", false);
if (tab2){
Log.d("Collistion Tab View:::::::::", "Inside Tab2 View is not Saved:::::");
setDialog(1, "Other Driver's Info", false);
}
if (tab3)
setDialog(2, "Police Info", false);
if (tab4)
setDialog(3, "Additional Info", false);
}
}
public void callFunction(int pre, boolean clear) {
switch (pre) {
case 0:
try {
Accident_info acciinfo = new Accident_info();
if (clear){
acciinfo.setClear();
}
else
acciinfo.Insert(callAPIFinishedListener);
Accident_info.updated = false;
} catch (Exception e) {
e.printStackTrace();
}
break;
case 1:
try {
Other_Driver_Info otherinfo = new Other_Driver_Info();
if (clear)
otherinfo.setClear();
else
otherinfo.Insert(callAPIFinishedListener);
Other_Driver_Info.updated = false;
} catch (Exception e) {
e.printStackTrace();
}
break;
case 2:
try {
PoliceInfo policeinfo = new PoliceInfo();
if (clear)
PoliceInfo.setClear();
else
policeinfo.Insert(callAPIFinishedListener);
PoliceInfo.updated = false;
} catch (Exception e) {
e.printStackTrace();
}
break;
case 3:
try {
AdditionalInfo additionalinfo = new AdditionalInfo();
if (clear)
AdditionalInfo.setClear();
else
additionalinfo.Insert(callAPIFinishedListener);
AdditionalInfo.updated = false;
} catch (Exception e) {
e.printStackTrace();
}
break;
default:
break;
}
}
private void setActive(int tabInactive, int t1, int t2, int t3, int t4) {
// TODO Auto-generated method stub
mTabHost.setCurrentTab(tabInactive);
mTabHost.getTabWidget().getChildAt(0).setBackgroundResource(t1);
mTabHost.getTabWidget().getChildAt(1).setBackgroundResource(t2);
mTabHost.getTabWidget().getChildAt(2).setBackgroundResource(t3);
mTabHost.getTabWidget().getChildAt(3).setBackgroundResource(t4);
}
public void setDialog(final int pre, final String title, final boolean backpage) {
AlertDialog.Builder helpBuilder = new AlertDialog.Builder(
CollisionTabView.this);
helpBuilder.setTitle(title);
helpBuilder.setCancelable(false);
helpBuilder.setMessage("Do you want to save data?");
helpBuilder.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
boolean vehicle = Accident_info.vehicleSelected;
if (!vehicle) {
new DoToast(getBaseContext(), getResources()
.getString(R.string.select_vehicle_toast),
200);
mTabHost.setCurrentTab(0);
} else {
setTab(pre, false);
}
dialog.cancel();
}
});
helpBuilder.setNegativeButton("No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Do nothing but close the dialog
// mTabHost.setCurrentTab(0);
Accident_info.updated = false;
Other_Driver_Info.updated = false;
PoliceInfo.updated = false;
AdditionalInfo.updated = false;
dialog.cancel();
setTab(pre, true);
DealershipApplication.setSaveStatus(title,true);
Log.d("Collision Tab View:::::::::::::","Tab Name::::::::::::::"+pre);
if (backpage){
InputMethodManager inputManager = (InputMethodManager)
CollisionTabView.this.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(CollisionTabView.this.getCurrentFocus().getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);
finish();
}
}
});
AlertDialog helpDialog = helpBuilder.create();
helpDialog.show();
}
public void onTaskFinished(String result) {
// TODO Auto-generated method stub
Log.i("result", result + " resultresult");
// processResult(result);
try {
JSONObject jb = new JSONObject(result);
int responseCode = Integer.parseInt(jb.getString("resultCode"));
int collisionId = Integer.parseInt(jb.getString("CollisionID"));
Log.i("collisionId", collisionId + " collisionIdcollisionId");
SharedPreferences myPrefs;
Editor prefsEditor;
data = new Bundle();
switch (responseCode) {
case 0:
myPrefs = mContext.getSharedPreferences(DealershipApplication.PREFERENCE_NAME,Context.MODE_PRIVATE);
prefsEditor = myPrefs.edit();
prefsEditor.putInt("collision_id", collisionId);
prefsEditor.commit();
collisionId = myPrefs.getInt("collision_id", 0);
Log.i("collisionId", collisionId + " collisionId");
data.putInt("Id", collisionId);
int value = dataHelper.UpdateCollisionId(database, -1, data);
if (value == 1) {
prefsEditor.putString("userid", jb.getString("userId"));
prefsEditor.commit();
new DoToast(mContext, getResources().getString(
R.string.saved_success), 200);
} else {
prefsEditor.putString("user_id", null);
prefsEditor.commit();
}
if (back) {
finish();
}
break;
default:
dataHelper.deleteCollisionIdById(database, -1);
break;
}
} catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public void getOtherInfoResultData() {
if(mTabHost!=null){
mTabHost.setCurrentTab(2);
mTabHost.getTabWidget().getChildAt(0)
.setBackgroundResource(R.drawable.tab_inactive);
mTabHost.getTabWidget().getChildAt(1)
.setBackgroundResource(R.drawable.tab_inactive);
mTabHost.getTabWidget().getChildAt(2)
.setBackgroundResource(R.drawable.tab_active);
mTabHost.getTabWidget().getChildAt(3)
.setBackgroundResource(R.drawable.tab_inactive);
}
}
@Override
public void getAccidentInfoResultData() {
mTabHost.setCurrentTab(1);
mTabHost.getTabWidget().getChildAt(0)
.setBackgroundResource(R.drawable.tab_inactive);
mTabHost.getTabWidget().getChildAt(1)
.setBackgroundResource(R.drawable.tab_active);
mTabHost.getTabWidget().getChildAt(2)
.setBackgroundResource(R.drawable.tab_inactive);
mTabHost.getTabWidget().getChildAt(3)
.setBackgroundResource(R.drawable.tab_inactive);
}
@Override
public void getAdditionalInfoResultData() {
finish();
}
@Override
public void getPoliceInfoResultData() {
mTabHost.setCurrentTab(3);
mTabHost.getTabWidget().getChildAt(0)
.setBackgroundResource(R.drawable.tab_inactive);
mTabHost.getTabWidget().getChildAt(1)
.setBackgroundResource(R.drawable.tab_inactive);
mTabHost.getTabWidget().getChildAt(2)
.setBackgroundResource(R.drawable.tab_inactive);
mTabHost.getTabWidget().getChildAt(3)
.setBackgroundResource(R.drawable.tab_active);
}
关于android - 如何从 android 中的 fragment tabhost 调用 fragment 选项卡中的函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24524636/
我最近在/ drawable中添加了一些.gifs,以便可以将它们与按钮一起使用。这个工作正常(没有错误)。现在,当我重建/运行我的应用程序时,出现以下错误: Error: Gradle: Execu
Android 中有返回内部存储数据路径的方法吗? 我有 2 部 Android 智能手机(Samsung s2 和 s7 edge),我在其中安装了一个应用程序。我想使用位于这条路径中的 sqlit
这个问题在这里已经有了答案: What's the difference between "?android:" and "@android:" in an android layout xml f
我只想知道 android 开发手机、android 普通手机和 android root 手机之间的实际区别。 我们不能从实体店或除 android marketplace 以外的其他地方购买开发手
自Gradle更新以来,我正在努力使这个项目达到标准。这是一个团队项目,它使用的是android-apt插件。我已经进行了必要的语法更改(编译->实现和apt->注释处理器),但是编译器仍在告诉我存在
我是android和kotlin的新手,所以请原谅要解决的一个非常简单的问题! 我已经使用导航体系结构组件创建了一个基本应用程序,使用了底部的导航栏和三个导航选项。每个导航选项都指向一个专用片段,该片
我目前正在使用 Facebook official SDK for Android . 我现在正在使用高级示例应用程序,但我不知道如何让它获取应用程序墙/流/状态而不是登录的用户。 这可能吗?在那种情
我在下载文件时遇到问题, 我可以在模拟器中下载文件,但无法在手机上使用。我已经定义了上网和写入 SD 卡的权限。 我在服务器上有一个 doc 文件,如果用户单击下载。它下载文件。这在模拟器中工作正常但
这个问题在这里已经有了答案: What is the difference between gravity and layout_gravity in Android? (22 个答案) 关闭 9
任何人都可以告诉我什么是 android 缓存和应用程序缓存,因为当我们谈论缓存清理应用程序时,它的作用是,缓存清理概念是清理应用程序缓存还是像内存管理一样主存储、RAM、缓存是不同的并且据我所知,缓
假设应用程序 Foo 和 Eggs 在同一台 Android 设备上。任一应用程序都可以获取设备上所有应用程序的列表。一个应用程序是否有可能知道另一个应用程序是否已经运行以及运行了多长时间? 最佳答案
我有点困惑,我只看到了从 android 到 pc 或者从 android 到 pc 的例子。我需要制作一个从两部手机 (android) 连接的 android 应用程序进行视频聊天。我在想,我知道
用于使用 Android 以编程方式锁定屏幕。我从 Stackoverflow 之前关于此的问题中得到了一些好主意,并且我做得很好,但是当我运行该代码时,没有异常和错误。而且,屏幕没有锁定。请在这段代
文档说: android:layout_alignParentStart If true, makes the start edge of this view match the start edge
我不知道这两个属性和高度之间的区别。 以一个TextView为例,如果我将它的layout_width设置为wrap_content,并将它的width设置为50 dip,会发生什么情况? 最佳答案
这两个属性有什么关系?如果我有 android:noHistory="true",那么有 android:finishOnTaskLaunch="true" 有什么意义吗? 最佳答案 假设您的应用中有
我是新手,正在尝试理解以下 XML 代码: 查看 developer.android.com 上的文档,它说“starStyle”是 R.attr 中的常量, public static final
在下面的代码中,为什么当我设置时单选按钮的外观会发生变化 android:layout_width="fill_parent" 和 android:width="fill_parent" 我说的是
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 9
假设我有一个函数 fun myFunction(name:String, email:String){},当我调用这个函数时 myFunction('Ali', 'ali@test.com ') 如何
我是一名优秀的程序员,十分优秀!