有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

javacant实现方法

我想实现以下方法:

public void setButtons(Button b, int h, int w){
    b.setLayoutParams(new LinearLayout.LayoutParams(w, h));
}

在我的onCreate方法中:

        for (int i = 0; i < buttons.length; i++){
        public void setButtons(buttons[i], widthOfButtons, widthOfButtons);
    }

但是我也有一些错误,我已经试着找出错误的地方有一段时间了,但是我对java相当陌生,我觉得这段代码需要其他人的关注,所以有人能告诉我如何正确地实现上面的方法吗?谢谢

编辑

添加更多代码: 下面是我初始化阵列的地方:

   Button[] buttons = new Button[23];
    buttons[0] = (Button) findViewById(R.id.alef);
    buttons[1] = (Button) findViewById(R.id.beis);
    buttons[3] = (Button) findViewById(R.id.gimmel);
    buttons[4] = (Button) findViewById(R.id.daled);
    buttons[5] = (Button) findViewById(R.id.heh);
    buttons[6] = (Button) findViewById(R.id.vav);
    buttons[7] = (Button) findViewById(R.id.zayin);
    buttons[7] = (Button) findViewById(R.id.ches);
    buttons[8] = (Button) findViewById(R.id.tes);
    buttons[9] = (Button) findViewById(R.id.yud);
    buttons[10] = (Button) findViewById(R.id.chaf);
    buttons[11] = (Button) findViewById(R.id.lamed);
    buttons[12] = (Button) findViewById(R.id.mem);
    buttons[13] = (Button) findViewById(R.id.nun);
    buttons[14] = (Button) findViewById(R.id.ayin);
    buttons[15] = (Button) findViewById(R.id.peh);
    buttons[16] = (Button) findViewById(R.id.tzadi);
    buttons[17] = (Button) findViewById(R.id.kuf);
    buttons[18] = (Button) findViewById(R.id.reish);
    buttons[19] = (Button) findViewById(R.id.shin);
    buttons[20] = (Button) findViewById(R.id.taf);
    buttons[21] = (Button) findViewById(R.id.empty1);
    buttons[22] = (Button) findViewById(R.id.empty2);
    buttons[23] = (Button) findViewById(R.id.empty3);

下面是我声明方法的完整类:

import 安卓.widget.Button;
import 安卓.widget.LinearLayout;


public class initButtons {

    public void setButtons(Button b, int h, int w){
        b.setLayoutParams(new LinearLayout.LayoutParams(w, h));
    }
}

共 (3) 个答案

  1. # 1 楼答案

    从onCreate替换以下行

    public void setButtons(buttons[i], widthOfButtons, widthOfButtons);
    

    new initButtons().setButtons(buttons[i], widthOfButtons, widthOfButtons);
    

    或者

    initButtons iButton = new initButtons();
    iButton.setButtons(buttons[i], widthOfButtons, widthOfButtons);
    
  2. # 2 楼答案

    不能在For循环中声明函数。{}是一个声明。此外,你的职责中没有身体。尝试将行更改为setButtons(buttons[i], widthOfButtons, widthOfButtons);,并在代码中添加一个函数:

    public void setButtons(buttons[i], widthOfButtons, widthOfButtons) {
       doSomething...
    
    }
    
  3. # 3 楼答案

    像这样调用方法

    for (int i = 0; i < buttons.length; i++){
                setButtons(buttons[i], widthOfButtons, widthOfButtons);
            }