我的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);
}
} }