我正在尝试做3个意图来开始新活动, 但是我发现我总是得到一个错误。 我把我的代码放到main.java上 代码:
public class Main extends Activity {
Button service;
Button gallery;
Button contact;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
service = (Button)findViewById(R.id.Services);
service.setOnClickListener(new View.OnClickListener() {
});
gallery = (Button)findViewById(R.id.Gallery);
gallery.setOnClickListener(new View.OnClickListener() {
});
contact = (Button)findViewById(R.id.Contact);
contact.setOnClickListener(new View.OnClickListener() {
});
public void onClick (View v) {
// Inflate the menu; this adds items to the action bar if it is present.
Intent intent = new Intent (Main.this, servicesActivity.class);
startActivity(intent);
}
public void onClick1 (View v) {
Intent intent1 = new Intent (Main.this, galleryActivity.class);
startActivity(intent1);
}
public void onClick2 (View v) {
Intent intent2 = new Intent (Main.this, contactActivity.class);
startActivity(intent2);
}
}
}
我尝试了不同的方法,甚至将括号放在不同的地方。我也在互联网上搜索了几个星期,发现没有什么可以超过1。 我在 OnClick和新的View.OnClickListener()上遇到错误。 我有一个工作,这就是为什么我用3个按钮尝试相同的代码。 基本上我在main activity.xml上有3个按钮。我想做的就是:
>'button1 >goes> activity1'
>'button2 >goes> activity2'
>'button3 >goes> activity3'
请给我任何提示或提示,因为我是Android开发人员的新手。
答案 0 :(得分:3)
您可以在同一listener
内完成所有这些操作。以这种方式设置所有listeners
service.setOnClickListener(this);
gallery.setOnClickListener(this);
然后使用一个函数并检查点击的id
的{{1}}
View
你可以实际清理它,以便不用这样的东西重复变量
public void onClick2 (View v) {
Intent intent = new Intent();
switch (v.getId()) // get the id of the Button clicked
{
case (R.id.Services):
intent = new Intent(Main.this, servicesActivity.class);
break;
case (R.id.Gallery):
intent = new Intent(Main.this, galleryActivity.class);
break;
...
}
startActivity(intent);
正如dymeh指出的那样,请确保public void onClick(View v)
{
Intent intent = new Intent(); // create an Intent
String act = null; // name for Activity to start with Intent
String shield = "com.your.package."; // set package name
switch (v.getId()) // get the id of the Button clicked
{
case (R.id.Services):
act = package + "Services"; // if Services button clicked use Services as the activity
break;
case (R.id.Gallery):
act = package + "GalleryActivity";
break;
...
}
try
{
intent = new Intent(Main.this, Class.forName(act)); // create your Intent by changing your String act to a class name
startActivity(intent); // start the Intent as normal
}
catch (ClassNotFoundException e){ // don't forget to catch invalid class names
e.printsStackTrace();
}
Activity
这可能会被清理一点,可能看起来更难,但我在自定义菜单和其他地方使用这样的东西,它很好地工作。它减少了单独的功能并创建了单独的implements OnClickListener
。如果您以后必须添加内容或想要重用代码,那么它会更容易一些,恕我直言
答案 1 :(得分:1)
public class Main extends Activity {
Button service;
Button gallery;
Button contact;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
service = (Button)findViewById(R.id.Services);
service.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
Intent intent = new Intent (Main.this, servicesActivity.class);
startActivity(intent);
}
});
gallery = (Button)findViewById(R.id.Gallery);
gallery.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
Intent intent1 = new Intent (Main.this, galleryActivity.class);
startActivity(intent1);
}
});
contact = (Button)findViewById(R.id.Contact);
contact.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
Intent intent2 = new Intent (Main.this, contactActivity.class);
startActivity(intent2);
}
});
}