Java android新手,如何初始化片段中元素的事件处理程序
(免责声明:android非常新)所以我尝试使用android Studio制作一个选项卡式应用程序,下面的例子是在线的,我可以使用actionbar设置2个选项卡和片段,如下所示: 在MainActivity.java中Java android新手,如何初始化片段中元素的事件处理程序,java,android,android-fragments,Java,Android,Android Fragments,(免责声明:android非常新)所以我尝试使用android Studio制作一个选项卡式应用程序,下面的例子是在线的,我可以使用actionbar设置2个选项卡和片段,如下所示: 在MainActivity.java中 ActionBar bar = getActionBar(); bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); bar.setTitle("My App Test"); Tab t
ActionBar bar = getActionBar();
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
bar.setTitle("My App Test");
Tab tabSearch = bar.newTab().setText("Search").setIcon(android.R.drawable.ic_menu_search);
Tab tabReport = bar.newTab().setText("Report").setIcon(android.R.drawable.ic_menu_report_image);
Fragment stfrag = new SearchTabFragment();
Fragment rptfrag = new ReportFragment();
tabSearch.setTabListener(new MyTabsListener(stfrag));
tabReport.setTabListener(new MyTabsListener(rptfrag));
bar.addTab(tabSearch);
bar.addTab(tabReport);
public class SearchTabFragment extends Fragment {
public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState){
return inflater.inflate(R.layout.fragment_1,container,false);
}
}
在ReportFragment/SearchTabFragment.java中
ActionBar bar = getActionBar();
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
bar.setTitle("My App Test");
Tab tabSearch = bar.newTab().setText("Search").setIcon(android.R.drawable.ic_menu_search);
Tab tabReport = bar.newTab().setText("Report").setIcon(android.R.drawable.ic_menu_report_image);
Fragment stfrag = new SearchTabFragment();
Fragment rptfrag = new ReportFragment();
tabSearch.setTabListener(new MyTabsListener(stfrag));
tabReport.setTabListener(new MyTabsListener(rptfrag));
bar.addTab(tabSearch);
bar.addTab(tabReport);
public class SearchTabFragment extends Fragment {
public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState){
return inflater.inflate(R.layout.fragment_1,container,false);
}
}
我基本上只是直接从我找到的教程中复制了ActionBar侦听器的示例:
class MyTabsListener implements ActionBar.TabListener {
public Fragment fragment;
public MyTabsListener(Fragment fragment){
this.fragment = fragment;
};
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft){
ft.replace(R.id.wrap,fragment);
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft){
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft){
}
}
现在我的问题是,如何将EventHandler附加到fragments.xml中定义的元素?之前在我玩的一个“单活动”应用程序中,我只是在onCreate事件之后将其放在主java文件中。例如:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
(TextView) tv = (TextView) findViewByid(R.id.textviewidhere);
//now I attach the handlers as I need
提前谢谢!(为清晰起见进行了编辑)您可能应该读一点关于。您需要在此处覆盖onViewCreated和“捕获”视图:
public class MyFragment extends Fragment {
View rootView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.my_fragment, container,
false);
return rootView;
}
//and you use rootView to call findViewById
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Button myButton = (Button) rootView.findViewById(R.id.mybutton);
//or you can set some other listener, or "catch" some different view -checkbox, //textview etc
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//do something you want
}
});
}
你可能应该读一点关于。您需要在此处覆盖onViewCreated和“捕获”视图:
public class MyFragment extends Fragment {
View rootView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.my_fragment, container,
false);
return rootView;
}
//and you use rootView to call findViewById
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Button myButton = (Button) rootView.findViewById(R.id.mybutton);
//or you can set some other listener, or "catch" some different view -checkbox, //textview etc
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//do something you want
}
});
}
你可能应该读一点关于。您需要在此处覆盖onViewCreated和“捕获”视图:
public class MyFragment extends Fragment {
View rootView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.my_fragment, container,
false);
return rootView;
}
//and you use rootView to call findViewById
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Button myButton = (Button) rootView.findViewById(R.id.mybutton);
//or you can set some other listener, or "catch" some different view -checkbox, //textview etc
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//do something you want
}
});
}
你可能应该读一点关于。您需要在此处覆盖onViewCreated和“捕获”视图:
public class MyFragment extends Fragment {
View rootView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.my_fragment, container,
false);
return rootView;
}
//and you use rootView to call findViewById
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Button myButton = (Button) rootView.findViewById(R.id.mybutton);
//or you can set some other listener, or "catch" some different view -checkbox, //textview etc
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//do something you want
}
});
}
例如:
public class SearchTabFragment extends Fragment {
public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState){
View root = inflater.inflate(R.layout.fragment_1,container,false);
TextView text = (TextView)root.findViewById(R.id.mybutton);
return root;
}
}
这将允许在片段内使用文本视图。示例:
public class SearchTabFragment extends Fragment {
public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState){
View root = inflater.inflate(R.layout.fragment_1,container,false);
TextView text = (TextView)root.findViewById(R.id.mybutton);
return root;
}
}
这将允许在片段内使用文本视图。示例:
public class SearchTabFragment extends Fragment {
public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState){
View root = inflater.inflate(R.layout.fragment_1,container,false);
TextView text = (TextView)root.findViewById(R.id.mybutton);
return root;
}
}
这将允许在片段内使用文本视图。示例:
public class SearchTabFragment extends Fragment {
public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState){
View root = inflater.inflate(R.layout.fragment_1,container,false);
TextView text = (TextView)root.findViewById(R.id.mybutton);
return root;
}
}
这将允许在片段中使用文本视图。修改的代码直接从以下内容中提取: 脚本: 假设你有一个
片段
,里面有一个按钮。您希望将活动作为事件侦听器附加到片段内的按钮。那么
public static class ButtonFragment extends Fragment implements OnClickListener {
private OnClickListener mListener;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// inflate the corresponding fragment XML
View v = inflater.inflate(R.layout.example_fragment, container, false);
// grab the button and attach this fragment as its listener
((Button)v.findViewById(R.id.btn)).setOnClickListener(this);
return v;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
// once this fragment is attached to its activity, check to see
// if it implements OnClickListener
mListener = (OnClickListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString() + " doesn't implement OnClickListener");
}
}
...
public void onClick(View v) {
if(mListener){
// once the button inside fragment is clicked,
// notify the activity about the same
mListener.onClickListener(v);
}
}
}
修改后的代码直接来自: 脚本: 假设你有一个
片段
,里面有一个按钮。您希望将活动作为事件侦听器附加到片段内的按钮。那么
public static class ButtonFragment extends Fragment implements OnClickListener {
private OnClickListener mListener;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// inflate the corresponding fragment XML
View v = inflater.inflate(R.layout.example_fragment, container, false);
// grab the button and attach this fragment as its listener
((Button)v.findViewById(R.id.btn)).setOnClickListener(this);
return v;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
// once this fragment is attached to its activity, check to see
// if it implements OnClickListener
mListener = (OnClickListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString() + " doesn't implement OnClickListener");
}
}
...
public void onClick(View v) {
if(mListener){
// once the button inside fragment is clicked,
// notify the activity about the same
mListener.onClickListener(v);
}
}
}
修改后的代码直接来自: 脚本: 假设你有一个
片段
,里面有一个按钮。您希望将活动作为事件侦听器附加到片段内的按钮。那么
public static class ButtonFragment extends Fragment implements OnClickListener {
private OnClickListener mListener;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// inflate the corresponding fragment XML
View v = inflater.inflate(R.layout.example_fragment, container, false);
// grab the button and attach this fragment as its listener
((Button)v.findViewById(R.id.btn)).setOnClickListener(this);
return v;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
// once this fragment is attached to its activity, check to see
// if it implements OnClickListener
mListener = (OnClickListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString() + " doesn't implement OnClickListener");
}
}
...
public void onClick(View v) {
if(mListener){
// once the button inside fragment is clicked,
// notify the activity about the same
mListener.onClickListener(v);
}
}
}
修改后的代码直接来自: 脚本: 假设你有一个
片段
,里面有一个按钮。您希望将活动作为事件侦听器附加到片段内的按钮。那么
public static class ButtonFragment extends Fragment implements OnClickListener {
private OnClickListener mListener;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// inflate the corresponding fragment XML
View v = inflater.inflate(R.layout.example_fragment, container, false);
// grab the button and attach this fragment as its listener
((Button)v.findViewById(R.id.btn)).setOnClickListener(this);
return v;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
// once this fragment is attached to its activity, check to see
// if it implements OnClickListener
mListener = (OnClickListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString() + " doesn't implement OnClickListener");
}
}
...
public void onClick(View v) {
if(mListener){
// once the button inside fragment is clicked,
// notify the activity about the same
mListener.onClickListener(v);
}
}
}
是的!:)如果它帮助你认为它是正确的;是的!:)如果它帮助你认为它是正确的;是的!:)如果它帮助你认为它是正确的;是的!:)如果它帮助你认为它是正确的;