Android 从MainActivity访问片段元素和属性
我想根据web服务消息更改片段中的按钮状态和源,但ı无法从MainActivity访问片段的元素 下面是示例片段代码:Android 从MainActivity访问片段元素和属性,android,android-fragments,Android,Android Fragments,我想根据web服务消息更改片段中的按钮状态和源,但ı无法从MainActivity访问片段的元素 下面是示例片段代码: private final String deviceID = "Phone"; WebSocketConnection mConnection = Connection.getInstance(); ImageButton LigthBtn = null; Boolean LigthBtnState = false; @Over
private final String deviceID = "Phone";
WebSocketConnection mConnection = Connection.getInstance();
ImageButton LigthBtn = null;
Boolean LigthBtnState = false;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.room1fragment, container, false);
LigthBtn = (ImageButton)view.findViewById(R.id.lightBtn);
LigthBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (LigthBtnState) {
JSONObject jsonObj =new JSONObject();
JSONObject jsonMessage = new JSONObject();
try {
jsonMessage.put("from",deviceID);
jsonMessage.put("message","LIGTH OFF");
jsonMessage.put("to","1");
jsonObj.put("Type","Message");
jsonObj.put("Data",jsonMessage);
} catch (JSONException e) {
e.printStackTrace();
}
mConnection.sendTextMessage(jsonObj.toString());
LigthBtn.setImageResource(R.drawable.ligth_off);
LigthBtnState = false;
}
else {
JSONObject jsonObj =new JSONObject();
JSONObject jsonMessage = new JSONObject();
try {
jsonMessage.put("from",deviceID);
jsonMessage.put("message","LIGTH ON");
jsonMessage.put("to","1");
jsonObj.put("Type","Message");
jsonObj.put("Data",jsonMessage);
} catch (JSONException e) {
e.printStackTrace();
}
mConnection.sendTextMessage(jsonObj.toString());
LigthBtn.setImageResource(R.drawable.ligth_on);
LigthBtnState = true;
}
}
});
return view;
}
主要活动如下:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
start();
tabsviewPager = (ViewPager) findViewById(R.id.tabspager);
mTabsAdapter = new Tabsadapter(getSupportFragmentManager());
tabsviewPager.setAdapter(mTabsAdapter);
getSupportActionBar().setHomeButtonEnabled(false);
getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
Tab room1Tab = getSupportActionBar().newTab().setText("Room1")
.setTabListener(this);
Tab room2Tab = getSupportActionBar().newTab().setText("Room2")
.setTabListener(this);
Tab room3Tab = getSupportActionBar().newTab().setText("Room3")
.setTabListener(this);
getSupportActionBar().addTab(room1Tab);
getSupportActionBar().addTab(room2Tab);
getSupportActionBar().addTab(room3Tab);
// This helps in providing swiping effect for v7 compat library
tabsviewPager.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
// TODO Auto-generated method stub
getSupportActionBar().setSelectedNavigationItem(position);
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
@Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
});
if (!isMyServiceRunning()) {
Intent serviceIntent = new Intent(
"com.example.androidusersmarthome.NotificationService");
serviceIntent.setPackage("com.example.androidusersmarthome");
context.startService(serviceIntent);//
}
}
private boolean isMyServiceRunning() {
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager
.getRunningServices(Integer.MAX_VALUE)) {
if (NotificationService.class.getName().equals(
service.service.getClassName())) {
return true;
}
}
return false;
}
void displayMessage(String text) {
Toast.makeText(getApplicationContext(), text, Toast.LENGTH_LONG).show();
}
@Override
public void onTabReselected(Tab arg0, FragmentTransaction arg1) {
// TODO Auto-generated method stub
}
@Override
public void onTabSelected(Tab selectedtab, FragmentTransaction arg1) {
// TODO Auto-generated method stub
tabsviewPager.setCurrentItem(selectedtab.getPosition()); // update tab
// position
// on tap
}
@Override
public void onTabUnselected(Tab arg0, FragmentTransaction arg1) {
// TODO Auto-generated method stub
}
我想从mainActivity中更改例如LightBtnState和LightBtn image按钮的来源。是否有任何方法可以做到这一点。提前谢谢。只是一个提示:您真的需要从activity中执行此操作,或者您可以安全地将其委托给片段本身?注:我想像这样的问题已经有了答案,但我还没找到……是的,它应该在mainActivity中,不幸的是。@dodotu为什么?活动不需要知道或关心它所承载的片段。。。是否有更好的设计决策可以绕过这样的设计?我只有一个连接,所有消息都在mainActivity中解释。根据消息,ı应该能够更改按钮status@RyanJI已使用ObserverPattern解决。感谢大家的帮助