如何点击ImageButton实现页面之间的切换
2014-09-03
3 回答
@Override public void onClick(View v) { // TODO Auto-generated method stub if( v == imagebutton1 ){ setContentView(R.layout.firsttitle); }else if( v == imagebutton2){ setContentView(R.layout.stationaround); }else if( v == imagebutton3){ setContentView(R.layout.transferbus); } } 这里判断时使用switch判断id,跳转时使用Intent和startAvtivity()
1
public class MainActivity extends Activity implements OnClickListener{
private ImageButton imagebutton1,imagebutton2;
private ImageButton imagebutton3,imagebutton4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.firsttitle);
// 查找,周边站点,换乘,更多的ImageButton
imagebutton1 = (ImageButton) findViewById(R.id.fristtitle_leftBtn);
imagebutton2 = (ImageButton) findViewById(R.id.fristtitle_leftBtn2);
imagebutton3 = (ImageButton) findViewById(R.id.fristtitle_rightBtn2);
imagebutton4 = (ImageButton) findViewById(R.id.fristtitle_rightBtn);
imagebutton1.setOnClickListener(this);
imagebutton2.setOnClickListener(this);
imagebutton3.setOnClickListener(this);
imagebutton4.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if( v == imagebutton1 ){
setContentView(R.layout.firsttitle);
}else if( v == imagebutton2){
setContentView(R.layout.stationaround);
}else if( v == imagebutton3){
setContentView(R.layout.transferbus);
}
}
}
2
public class TransferBus extends Activity{
private ImageButton Ibt1,Ibt2;
private Context mContext;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.transferbus);
mContext = this;
Ibt1 = (ImageButton) findViewById(R.id.fristtitle_leftBtn);
Ibt2 = (ImageButton) findViewById(R.id.fristtitle_leftBtn2);
// Ibt3 = (ImageButton) findViewById(R.id.fristtitle_rightBtn2);
// Ibt4 = (ImageButton) findViewById(R.id.fristtitle_rightBtn);
Ibt1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intnet = new Intent(mContext,MainActivity.class);
}
});
Ibt2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intnet = new Intent(mContext,StationAround.class);
}
});
}
}
举报