欢迎访问我的个人网站==》 jiashubing.cn

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

我的MainActivity.java部分代码

 1 public class MainActivity extends ActionBarActivity {
 2     //不要定义button类型,会出错
 3     View imageButton1, imageButton2,imageButton3,imageButton4,
 4     imageButton5,imageButton6;
 5     @Override
 6     protected void onCreate(Bundle savedInstanceState) {
 7         super.onCreate(savedInstanceState);
 8         setContentView(R.layout.activity_main);
 9 
10         if (savedInstanceState == null) {
11             getSupportFragmentManager().beginTransaction()
12             .add(R.id.container, new PlaceholderFragment()).commit();
13         }
14         //根据ID找到界面中的组件按钮
15         imageButton1=findViewById(R.id.imageButton1);
16         imageButton2=findViewById(R.id.imageButton2);
17         imageButton3=findViewById(R.id.imageButton3);
18         imageButton4=findViewById(R.id.imageButton4);
19         imageButton5=findViewById(R.id.imageButton5);
20         imageButton6=findViewById(R.id.imageButton6);
21         //创建监听器对象
22         ButtonListener bt = new ButtonListener();
23         //注册监听
24         imageButton1.setOnClickListener(bt);
25         imageButton2.setOnClickListener(bt);
26         imageButton3.setOnClickListener(bt);
27         imageButton4.setOnClickListener(bt);
28         imageButton5.setOnClickListener(bt);
29         imageButton6.setOnClickListener(bt);
30     }
31 
32     class ButtonListener implements OnClickListener
33     {
34         //实现单击事件处理方法
35         @Override
36         public void onClick(View v) {
37             // TODO Auto-generated method stub
38             if(v == imageButton1)
39             {
40                 //导航
41                 Intent intent = new Intent();
42                 intent.setClass(MainActivity.this, OtherActivity.class);
43                 startActivity(intent);
44             }
45             if(v == imageButton2)
46             {
47                 //搜索
48                 Intent intent = new Intent();
49                 intent.setClass(MainActivity.this, OtherActivity.class);
50                 startActivity(intent);
51             }
52             if(v == imageButton3)
53             {
54                 //地图
55                 Intent intent = new Intent();
56                 intent.setClass(MainActivity.this, OtherActivity.class);
57                 startActivity(intent);
58             }
59             if(v == imageButton4)
60             {
61                 //周边
62                 Intent intent = new Intent();
63                 intent.setClass(MainActivity.this, OtherActivity.class);
64                 startActivity(intent);
65             }
66             if(v == imageButton5)
67             {
68                 //推荐
69                 Intent intent = new Intent();
70                 intent.setClass(MainActivity.this, OtherActivity.class);
71                 startActivity(intent);
72             }
73             if(v == findViewById(R.id.imageButton6))
74             {
75                 //其他
76                 Intent intent = new Intent();
77                 intent.setClass(MainActivity.this, OtherActivity.class);
78                 startActivity(intent);
79             }
80         }
81 
82     }

 

posted @ 2014-04-21 11:27  贾树丙  阅读(3358)  评论(0编辑  收藏  举报