您的位置:首页 > 移动开发 > Android开发

android开发 单击按钮 实现页面间的跳转

2014-04-21 11:27 441 查看
我的MainActivity.java部分代码

public class MainActivity extends ActionBarActivity {
//不要定义button类型,会出错
View imageButton1, imageButton2,imageButton3,imageButton4,
imageButton5,imageButton6;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
//根据ID找到界面中的组件按钮
imageButton1=findViewById(R.id.imageButton1);
imageButton2=findViewById(R.id.imageButton2);
imageButton3=findViewById(R.id.imageButton3);
imageButton4=findViewById(R.id.imageButton4);
imageButton5=findViewById(R.id.imageButton5);
imageButton6=findViewById(R.id.imageButton6);
//创建监听器对象
ButtonListener bt = new ButtonListener();
//注册监听
imageButton1.setOnClickListener(bt);
imageButton2.setOnClickListener(bt);
imageButton3.setOnClickListener(bt);
imageButton4.setOnClickListener(bt);
imageButton5.setOnClickListener(bt);
imageButton6.setOnClickListener(bt);
}

class ButtonListener implements OnClickListener
{
//实现单击事件处理方法
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v == imageButton1)
{
//导航
Intent intent = new Intent();
intent.setClass(MainActivity.this, OtherActivity.class);
startActivity(intent);
}
if(v == imageButton2)
{
//搜索
Intent intent = new Intent();
intent.setClass(MainActivity.this, OtherActivity.class);
startActivity(intent);
}
if(v == imageButton3)
{
//地图
Intent intent = new Intent();
intent.setClass(MainActivity.this, OtherActivity.class);
startActivity(intent);
}
if(v == imageButton4)
{
//周边
Intent intent = new Intent();
intent.setClass(MainActivity.this, OtherActivity.class);
startActivity(intent);
}
if(v == imageButton5)
{
//推荐
Intent intent = new Intent();
intent.setClass(MainActivity.this, OtherActivity.class);
startActivity(intent);
}
if(v == findViewById(R.id.imageButton6))
{
//其他
Intent intent = new Intent();
intent.setClass(MainActivity.this, OtherActivity.class);
startActivity(intent);
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: