嗨,我对我的应用程序中的android非常陌生,我创建了两个片段,分别是“ MenuFragment”和“ TextFragment”好的
但是在我的TextFragment中我有一个方法,因此我想从我的MenuFragment类中调用该方法
我想从我的MenuFragment调用的public void change(String txt,String txt1)方法
我该怎么做请帮我一些
文字片段:-
public class TextFragment extends Fragment {
TextView text,vers;
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) {
return view;
}
public void change(String txt, String txt1){
text.setText(txt);
vers.setText(txt1);
}
}
MenuFragment:-
public class MenuFragment extends Fragment {
String[] AndroidOS = new String[]{"Cupcake", "Donut", "Eclair", "Froyo", "Gingerbread", "Honeycomb", "Ice Cream SandWich", "Jelly Bean", "KitKat", "Jelly Bean", "KitKat"};
String[] Version = new String[]{"1.5", "1.6", "2.0-2.1", "2.2", "2.3", "3.0-3.2", "4.0", "4.1-4.3", "4.4", "4.1-4.3", "4.4"};
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
}
}
最佳答案
你可以试试看
对于TextFragment:-
public class TextFragment extends Fragment{
public static TextView text,vers;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return super.onCreateView(inflater, container, savedInstanceState);
}
public static void change(String txt, String txt1){
text.setText(txt);
vers.setText(txt1);
}
}
对于MenuFragment:-
public class MenuFragment extends Fragment {
ListView list;
Button button1, button2;
String[] AndroidOS = new String[]{"Cupcake", "Donut", "Eclair", "Froyo", "Gingerbread", "Honeycomb", "Ice Cream SandWich", "Jelly Bean", "KitKat", "Jelly Bean", "KitKat"};
String[] Version = new String[]{"1.5", "1.6", "2.0-2.1", "2.2", "2.3", "3.0-3.2", "4.0", "4.1-4.3", "4.4", "4.1-4.3", "4.4"};
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
TextFragment.change(AndroidOS[position],Version[position]);
}
}
希望对您有所帮助。谢谢