gpt4 book ai didi

java - Android开发-使用多个图像按钮

转载 作者:行者123 更新时间:2023-12-01 13:47:01 26 4
gpt4 key购买 nike

我正在尝试创建一个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.
}
}));
}

}

我不知道我是否错过了一些愚蠢的事情,或者我是否以完全错误的方式设置了它。但如果能有一双额外的眼睛来观察它,我们将不胜感激。

最佳答案

试试这个。不要忘记将这些 Activity 添加到 list 中。您可以使用 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.
}
}));
}

关于java - Android开发-使用多个图像按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20291633/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com