如何打开一个按钮,另一个按钮另一个活动? [英] How do I open a button another activity with another button?

查看:119
本文介绍了如何打开一个按钮,另一个按钮另一个活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已成功我的ImageButton打开花药活动,但问题是使用同样的方法在其他的ImageButton它与一个错误说的方法已经被应用在主要活动。

I have made successfully my Imagebutton to open anther activity but the issue is by using the same method on another ImageButton it comes up with an error saying the method is already used in "Main Activity".

public class MainActivity extends ActionBarActivity {
      private static ImageButton ImageButton_sbm;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        OnClickImageButtonListener();
    }

    public void OnClickImageButtonListener() {
        ImageButton_sbm = (ImageButton)findViewById(R.id.imageButton);
        ImageButton_sbm.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent("saintbedeslytham.saintbedes.event");
                        startActivity(intent);

                    }
                }

        );

    }

    private static ImageButton ImageButton2_sbm;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        OnClickImageButtonListener();
    }

    public void OnClickImageButtonListener() {
        ImageButton_sbm = (ImageButton)findViewById(R.id.imageButton2);
        ImageButton_sbm.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent("saintbedeslytham.saintbedes.news");
                        startActivity(intent);

                    }
                }

        );

    }

如何,如果有的话,我可以申请另一种方法为saintbedeslytham.saintbedes.news

推荐答案

在这里你的问题是,你无法创建两个方法相同的定义。 您有两种:

Your problem here is that you cannot create two method with the same definition. You have two:

protected void onCreate(Bundle savedInstanceState);

和二:

public void OnClickImageButtonListener();

您不能有2种方法与2简单的原因相同的定义。
试想一下,2人具有相同的名称,叫他们的名字。

You can't have 2 methods with the same definition for 2 simple reasons.
Imagine 2 people with the same name, call for their name.

  • 谁来回答?
  • 你是谁打来的?

编辑:
因此,您可以:


So you can:

public class MainActivity extends ActionBarActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        prepareClicks();
    }

    private void prepareClicks() {
        findViewById(R.id.imageButton).setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent("saintbedeslytham.saintbedes.event");
                        startActivity(intent);
                    }
                }
        );
        findViewById(R.id.imageButton2).setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent("saintbedeslytham.saintbedes.news");
                        startActivity(intent);
                    }
                }
        );
    }
}

这篇关于如何打开一个按钮,另一个按钮另一个活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆