簡體   English   中英

如何在沒有xml文件的情況下在android studio中制作100個按鈕?

[英]How to make 100 buttons in android studio without xml file?

我正在尋找不需要長 .xml 的代碼,我可以輕松地將按鈕數量更改為 200 或所有內容。

public class buttons extends Activity {
    Button button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_buttons);
    }
}

在活動文件中添加:

    // create buttons in a loop
    for (int i = 0; i < 200; i++) {
        Button button = new Button(this);
        button.setText("Button " + i);
   }

類似的東西

public class Buttons extends Activity {
{

Button button;
List<Button> buttonList = new ArrayList<Button>();
LinearLayout.LayoutParams params;
LinearLayout list;
OnClickListener listener;


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

params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
        LinearLayout.LayoutParams.WRAP_CONTENT);
list = (LinearLayout) findViewById(R.id.list);

listener = new OnClickListener() {
@Override
public void onClick(View v) {

int id = v.getId();
Button b = buttonList.get(id);
///

}
};

for (int i = 0; i < 200; i++)
addButton();

}


public void addButton()
{
button = new CheckBox(this);
button.setLayoutParams(params);
button.setText("TEXT");
button.setId(buttonList.size());
button.setOnClickListener(listener);
buttonList.add(button);
list.addView(button);
}
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM