我想要我的按钮“configbutton”的ID,它位于 Activity 的 fragment 中,以便我可以调用 Activity 中声明的一些方法。但我不能这样做。 有什么办法可以做到这一点吗? Activity 类
public class UartLoopBackActivity extends FragmentActivity implements
ActionBar.TabListener {
Button configButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.data_analysis);
configButton = (Button) findViewById(R.id.configButton);
}
fragment 类
public class DataShown extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.d("3", "started");
View rootView = inflater.inflate(R.layout.datashown, container, false);
Log.d("3", "closed");
Button tv = (Button) rootView.findViewById(R.id.configButton);
return rootView;
}
}
如果你知道请分享代码。 我想引用 Activity 类中 fragment 中的按钮,以便可以从 Activity 类中调用该按钮的监听器。有可能吗? 谢谢
最佳答案
你不能在fragment中定义这个按钮,然后像这样调用activity方法吗?
((YourActivityClassName)getActivity()).yourPublicMethod();
关于java - 尝试引用 Activity 中 fragment 的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25959571/