尝试使用2个不同的按钮,2个不同的XML页面 [英] Trying to open 2 different xml page using 2 different buttons

查看:110
本文介绍了尝试使用2个不同的按钮,2个不同的XML页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

@ Takendarkk:

@ Takendarkk :

public class FragmentFirstPage extends Fragment implements OnClickListener{


View root;


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle bundle){

    root = inflater.inflate(R.layout.fragment_page1, container, false); 

    Button bt = (Button) root.findViewById(R.id.button1);
    bt.setOnClickListener(this);

    Button mbt = (Button) root.findViewById(R.id.imageButton1);
    mbt.setOnClickListener(new View.OnClickListener() { 
    final ViewPager viewPager = (ViewPager) getActivity().findViewById (R.id.activity_main_viewpager);

         @Override
          public void onClick (View v){
          viewPager.setCurrentItem (viewPager.getCurrentItem() + 1); //or -1 to go previous

          } 
    });             
    return root;

}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    Intent myIntent = new Intent(getActivity(), SplashActivity.class);
    getActivity().startActivity(myIntent);

}

这是code我写它不显示任何错误,但不起作用显示错误消息,不幸的是应用程序已停止工作。我应该怎么做才能得到这个权利我的XML页面有2个按钮,按钮1打开活动和imagebutton1显示下一个片段。这就是我想要达到的。

this is the code i wrote it doesn't shows any error but doesn't work shows an error message saying unfortunately app has stopped working. What should i do to get this right my xml page has 2 buttons button1 open an activity and imagebutton1 shows the next fragment. This is what i want to achieve.

推荐答案

所有你需要做的是里面检查你的的onClick 哪个按钮被点击通过检查的标识调用视图。从那里,你可以决定了每个人会做。

All you need to do is check inside your onClick which button was clicked by checking the id of the calling view. From there you can decide what each one will do.

public class beforemain extends Activity implements OnClickListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.beforemain);

    Button mBtn1 = (Button) findViewById(R.id.button1);
    Button mBtn2 = (Button) findViewById(R.id.button2); //Just like #1
    mBtn1.setOnClickListener(this);
    mBtn2.setOnClickListener(this); //Also like #1

}

@Override
public void onClick(View v) {
    switch(v.getId()) { //Get the id of the button that was clicked
    case R.id.button1:
        Intent i = new Intent(beforemain.this, Splash1.class);
        startActivity(i);
        break;
    case R.id.button2:
        Intent i = new Intent(beforemain.this, Splash2.class);
        startActivity(i);
        break;
    }
}

这篇关于尝试使用2个不同的按钮,2个不同的XML页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆