1

我正在尝试做 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 和 new View.OnClickListener()上遇到错误。我有一个工作,这就是为什么我用 3 个按钮尝试相同的代码。基本上我在主activity.xml 上有3 个按钮。我想做的就是:

>'button1 >goes> activity1'
>'button2 >goes> activity2'
>'button3 >goes> activity3'

请给我任何提示或提示,因为我是 Android 开发新手。

4

2 回答 2

3

您可以在同一个listener. 全部listeners这样设置

service.setOnClickListener(this);
gallery.setOnClickListener(this);

然后使用一个功能并检查被点击idView

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);

您实际上可以进一步清理它,以免重复使用类似这样的变量

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();
}

正如 dymeh 指出的那样,确保你的Activity implements OnClickListener

这可能会被清理得更多,看起来更困难,但我在自定义菜单和其他地方使用了类似的东西,它工作得很好。它减少了单独的功能并创建了单独Intents的 . 如果您以后必须添加一些东西或想要重用代码,它会更容易一些,恕我直言

于 2013-05-28T21:00:02.567 回答
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);

        }

    });

}
于 2013-05-28T20:59:10.833 回答