【问题标题】:Get a button index using onClick method使用 onClick 方法获取按钮索引
【发布时间】:2016-10-22 04:20:14
【问题描述】:

所以,我并没有说清楚。我在尝试从矩阵中获取按钮索引时遇到了一些麻烦。 我有这个矩阵 bt[4][4],每个按钮都有这样的值: btn11 = bt[0][0]。 现在我想做的是,每当我点击一个按钮时,我都会得到它的“坐标”。 例如: Bt11 会给出 [0] 和 [0] 。 现在我遇到的问题:

  1. 我已经为每个按钮设置了监听器,但我无法实现“onClick”方法。 每当我尝试在这里实现“public class easy extends Activity”时,我都会收到一条错误消息。
  2. 第二个问题,我不知道从 bt[x][y] 获取坐标。

总结一下。我想设置一个 onClick 方法,以便每次单击按钮时都会得到它的 [x][y] 坐标。 这是我的代码:

public class easy extends Activity { //Can't implement OnClick Listener
    Button bt[][] = new Button[4][4];
    Button up;
    Button down;
    Button left;
    Button right;

    int listaTroca[] = new int[10]; // lista pra receber os valores de retorno da logica



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_easy);





        //Linha 1
        bt[0][0] = (Button) findViewById(R.id.bt11);
        bt[0][0].setOnClickListener((View.OnClickListener) this);

        bt[0][1] = (Button) findViewById(R.id.bt12);
        bt[0][1].setOnClickListener((View.OnClickListener) this);

        bt[0][2] = (Button) findViewById(R.id.bt13);
        bt[0][2].setOnClickListener((View.OnClickListener) this);

        bt[0][3] = (Button) findViewById(R.id.bt14);
        bt[0][3].setOnClickListener((View.OnClickListener) this);
        //--------------------- Linha 2
        bt[1][0] = (Button) findViewById(R.id.bt21);
        bt[1][0].setOnClickListener((View.OnClickListener) this);

        bt[1][1] = (Button) findViewById(R.id.bt22);
        bt[1][1].setOnClickListener((View.OnClickListener) this);

        bt[1][2] = (Button) findViewById(R.id.bt23);
        bt[1][2].setOnClickListener((View.OnClickListener) this);

        bt[1][3] = (Button) findViewById(R.id.bt24);
        bt[1][3].setOnClickListener((View.OnClickListener) this);
        //------------------------Linha 3
        bt[2][0] = (Button) findViewById(R.id.bt31);
        bt[2][0].setOnClickListener((View.OnClickListener) this);

        bt[2][1] = (Button) findViewById(R.id.bt32);
        bt[2][1].setOnClickListener((View.OnClickListener) this);

        bt[2][2] = (Button) findViewById(R.id.bt33);
        bt[2][2].setOnClickListener((View.OnClickListener) this);

        bt[2][3] = (Button) findViewById(R.id.bt34);
        bt[2][3].setOnClickListener((View.OnClickListener) this);
        //---------------------Linha 4
        bt[3][0] = (Button) findViewById(R.id.bt41);
        bt[3][0].setOnClickListener((View.OnClickListener) this);

        bt[3][1] = (Button) findViewById(R.id.bt42);
        bt[3][1].setOnClickListener((View.OnClickListener) this);

        bt[3][2] = (Button) findViewById(R.id.bt43);
        bt[3][2].setOnClickListener((View.OnClickListener) this);

        bt[3][3] = (Button) findViewById(R.id.bt44);
        bt[3][3].setOnClickListener((View.OnClickListener) this);
        //----------------------FIM DECLARAÇÃO + OUVIDOS
        listaTroca = Logic.Logic_main(1,1,1);



    }

}

【问题讨论】:

  • 尝试使用button.setTag(key, value)。然后在调用 onClick 时,您可以使用 v.getTag(key) 返回值
  • 您的帖子中没有问题。您需要什么/什么不起作用?
  • @Vucko 已经编辑了问题。对不起。

标签: android class button return


【解决方案1】:

我不完全确定您的问题是什么,但据我了解,您希望在每个按钮上都有一个View.OnClickListener(),并且当单击该按钮时您想做正确的事情吗?

已编辑:我已编辑答案,因为问题已在下面的 cmets 中得到澄清。

您想要做的,只是简单地迭代您的bt[][] 以找到正确的按钮,如下所示:

public class Easy extends Activity {

    Button[][] bt = new Button[4][4];

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_easy);

        //Linha 1
        bt[0][0] = (Button) findViewById(R.id.bt11);
        bt[0][0].setOnClickListener(mOnClickListener);

        bt[0][1] = (Button) findViewById(R.id.bt12);
        bt[0][1].setOnClickListener(mOnClickListener);

        // ADD YOUR OTHER findViewByID

    }

    View.OnClickListener mOnClickListener = new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            ButtonCoordinate bc = getButtonCoordinate(v);
            if (bc != null) {
                // You now have your button, and the coordinates
                Log.d("Easy", "Button.position[ x:" + bc.x + ", y:" + bc.y + " ]");
            }
        }
    };

    private ButtonCoordinate getButtonCoordinate(View v) {
        for (int x = 0 ; x < bt.length ; x++ ) {
            for (int y = 0 ; y < bt[x].length ; y++) {
                Button b = bt[x][y];
                if (v == b) {
                    return new ButtonCoordinate(x, y, b);
                }
            }
        }
        return null;
    }

    public static class ButtonCoordinate {
        public final int x;
        public final int y;
        public final Button button;
        public ButtonCoordinate(int x, int y, Button button) {
            this.x = x;
            this.y = y;
            this.button = button;
        }
    }

}

您只需调用getButtonCoordinate(View),它将返回一个包含ButtonButtonCoordinate,它是xybt[][] 中的坐标

【讨论】:

  • 我正在尝试在 bt[x][y] 处获取该索引,例如:bt11 将返回到 int 列表 [0,0]
  • 而且,我需要它是一个通用方法。因此,当您单击任何这些按钮 (bt[x][y]) 时,您将得到它的 x 和 y 值。
  • 好的,我现在明白了。我已经编辑了我的答案,上面的代码现在应该适合你了。
  • 这就是我想要的。谢谢楼主
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-05-22
  • 2014-02-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-27
相关资源
最近更新 更多