我正在尝试创建一个Android应用程序,我是一个相对新手。 我试着使用多个图像按钮,但是,我无法让它工作,这是我正在使用的代码。
Public class MapScreen extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.belfast_map);
ImageButton ib1,ib2,ib3,ib4,ib5;
ib1= (ImageButton) findViewById(R.id.go_to_lagan_screen);
ib2= (ImageButton) findViewById(R.id.go_to_city);
ib3= (ImageButton) findViewById(R.id.go_to_university);
ib4= (ImageButton) findViewById(R.id.go_to_icon_screen);
ib5= (ImageButton) findViewById(R.id.map_to_home_screen);
ib1.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Intent intent = new Intent (v.getContext(), LaganArea.class);
startActivityForResult(intent,0);
//To change body of implemented methods use File | Settings | File Templates.
}
} );
ib2.setOnClickListener((new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Intent intent1= new Intent (v.getContext(), CityCentre.class);
startActivityForResult(intent1,0);
//To change body of implemented methods use File | Settings | File Templates.
}
}));
ib3.setOnClickListener((new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Intent intent2= new Intent (v.getContext(), UniversityArea.class);
startActivityForResult(intent2,0);
//To change body of implemented methods use File | Settings | File Templates.
}
}));
ib4.setOnClickListener((new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Intent intent3= new Intent (v.getContext(), TheIcons.class);
startActivityForResult(intent3,0);
//To change body of implemented methods use File | Settings | File Templates.
}
}));
ib5.setOnClickListener((new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Intent intent4= new Intent (v.getContext(), MyActivity.class);
startActivityForResult(intent4,0);
//To change body of implemented methods use File | Settings | File Templates.
}
}));
}
}
我不知道我是否错过了一些愚蠢的事情,或者我是否以完全错误的方式对待它。但是,额外的一双眼睛将非常感激。
答案 0 :(得分:1)
试试这个。不要忘记在清单上添加这些活动。你可以使用switch / case来获得干净的小代码。
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.belfast_map);
ImageButton ib1,ib2,ib3,ib4,ib5;
ib1= (ImageButton) findViewById(R.id.go_to_lagan_screen);
ib2= (ImageButton) findViewById(R.id.go_to_city);
ib3= (ImageButton) findViewById(R.id.go_to_university);
ib4= (ImageButton) findViewById(R.id.go_to_icon_screen);
ib5= (ImageButton) findViewById(R.id.map_to_home_screen);
ib1.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Intent intent = new Intent (MapScreen.this, LaganArea.class);
startActivity(intent);
}
} );
ib2.setOnClickListener((new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Intent intent1= new Intent (MapScreen.this, CityCentre.class);
startActivity(intent1);
//To change body of implemented methods use File | Settings | File Templates.
}
}));
ib3.setOnClickListener((new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Intent intent2= new Intent (MapScreen.this, UniversityArea.class);
startActivity(intent2);
//To change body of implemented methods use File | Settings | File Templates.
}
}));
ib4.setOnClickListener((new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Intent intent3= new Intent (MapScreen.this, TheIcons.class);
startActivity(intent3);
//To change body of implemented methods use File | Settings | File Templates.
}
}));
ib5.setOnClickListener((new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Intent intent4= new Intent (MapScreen.this, MyActivity.class);
startActivity(intent4);
//To change body of implemented methods use File | Settings | File Templates.
}
}));
}
答案 1 :(得分:0)
首先在Manifest中声明活动。
尝试这样的每项活动:
ib1.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
startActivity(new Intent (MapScreen.this , LaganArea.class));
}
} );