java - 无法实现方法

标签 java android oncreate

我想实现以下方法:

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 android.widget.Button;
import android.widget.LinearLayout;


public class initButtons {

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

最佳答案

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

关于java - 无法实现方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29353172/

相关文章:

java - JDialog 不显示组件

java - 禁止在 FlowLayout 中使用多行?

java - 用\$转义$

java - 在 onCreate() 方法运行之前命名 View 时出错

java - *更改应用程序加载时的 Activity 背景(在onCreate之前)

java - 使用httpurlconnection调用第三方api

java - Android 文本转语音按钮点击问题

android - 打开电子邮件附件时如何获取文件名和扩展名

java - Android 应用程序 - onCreateView 中的 HttpURLConnection

android - 向 onStart 与 onCreate 添加监听器