如何使用一个按钮在我的项目打开一个xml文件 [英] How can i use a button to open an xml file in my project
问题描述
我想开在我的项目上的按钮,一个XML文件,点击这是我的code可能有人请建议我如何能做到这一点。我无法做到这一点的一个我没有使用onclicklistener了。此外,我如何让第二个片段的默认片段,这样,当我从左至右滑动我可以查看的第一个片段。
I want to open an xml file in my project on button click this is my code could somebody please suggest me how can i do it. I'm unable to do it the one i did was using onclicklistener. Also how can i make the second fragment the default fragment so that when i slide from left to right i can view the first fragment.
public class FragmentFirstPage extends Fragment {
View root;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle bundle){
root = inflater.inflate(R.layout.fragment_page1, container, false);
//If you want to retrieve a textview inside fragment_page1.xml for example, you shall do:
//TextView text = (TextView) root.findViewById(R.id.blablabla);
Button bt = (Button) root.findViewById(R.id.button1);
// final ViewPager viewPager = (ViewPager) getActivity().findViewById (R.id.activity_main_viewpager);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick (View v){
Intent i=new Intent(fragment_page1.this, SplashActivity.class);
startActivity(i);
}
});
return root;
}
给我的错误
意图I =新意图(fragment_page1.this,SplashActivity.class);
说fragment_page1不能被解析为一个类型,否则
Gives me error at Intent i=new Intent(fragment_page1.this, SplashActivity.class); says fragment_page1 cannot be resolved to a type or else
意图I =新意图(FragmentFirstPage.this,SplashActivity.class);
说,在这条线多个标记
- 构造意图(FragmentFirstPage,类)
未定义
Intent i=new Intent(FragmentFirstPage.this, SplashActivity.class); says Multiple markers at this line - The constructor Intent(FragmentFirstPage, Class) is undefined
推荐答案
您有FragmentFirstPage和FragmentSecondPage,你可能有fragment_page1.xml和fragment_page2.xml
You have FragmentFirstPage and FragmentSecondPage, you may have fragment_page1.xml and fragment_page2.xml
PS:删除任何Android:onclick属性
PS: remove any android:onClick attribute
您FirstFragmentPage内部,对onCreateView(),添加以下,如果你的按钮上fragment_page1.xml
Inside your FirstFragmentPage, on onCreateView (), , add the following if your button is on fragment_page1.xml
Button bt = (Button) root.findViewById(R.id.your_button_id);
ViewPager viewPager = (ViewPager) getActivity().findViewById (R.id.your_viewpager_id ();
bt.setOnClickListener(new View.onClickListener (){
@Override
public void onClick (View v){
viewPager.setCurrentItem (viewPager.getCurrentItem() + 1); //or -1 to go previous
}
}
请原谅我的code画幅的,我是从我的智能手机输入:/
Forgive my code format, I'm typing from my smartphone : /
这篇关于如何使用一个按钮在我的项目打开一个xml文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!