我写了以下代码:
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);
}
当我运行它时,它不显示任何错误但也不起作用。它显示一条错误消息,说不幸的是app已停止工作。我该怎么做才能使我的xml页面有2个按钮button1打开一个活动,imagebutton1显示下一个片段。这就是我想要实现的目标。
答案 0 :(得分:1)
您需要做的就是检查onClick
内部通过检查主叫视图的ID来检查哪个按钮。从那里你可以决定每个人会做什么。
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;
}
}
答案 1 :(得分:1)
按如下方式更改onClick方法的实现。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.beforemain);
Button mBtn1 = (Button) findViewById(R.id.button1);
mBtn1.setOnClickListener(this);
Button mBtn2 = (Button) findViewById(R.id.button1);
mBtn2.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if(mBtn1.getId()==v.getID())
{
Intent i1=new Intent(beforemain.this, Splash1.class);
startActivity(i1);
}
else
{
Intent i=new Intent(beforemain.this, Splash2.class);
startActivity(i);
}
}
答案 2 :(得分:1)
Button mBtn1 = (Button) findViewById(R.id.button1);
Button mBtn2 = (Button) findViewById(R.id.button2);
mBtn1.setOnClickListener(this);
mBtn2.setOnClickListener(this);
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
int id = v.getId();
switch(id) {
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;
}
}
答案 3 :(得分:0)
您只需为所有按钮设置onClickListener
,然后在if else
或switch
案例中比较其ID,并执行您想做的任何事情
例如。
@Override
public void onClick(View v)
{
if(v.getId()==R.id.button1)
{
Intent i=new Intent(beforemain.this, Splash1.class);
startActivity(i);
}
else if(v.getId()==R.id.button2)
{
Intent i=new Intent(beforemain.this, Splash2.class);
startActivity(i);}
}
}