gpt4 book ai didi

android - 如何从 android 中的 fragment tabhost 调用 fragment 选项卡中的函数?

转载 作者:行者123 更新时间:2023-11-30 02:49:04 25 4
gpt4 key购买 nike

所以,在我的项目中有 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com