2017-09-27 94 views
0

我正在開發一個android考勤系統,其中用戶在Main_Activity中的類中輸入學生的總數(int),並且在第二個活動中創建了許多數量的按鈕。如何在運行時使用用戶輸入來創建按鈕列表(用於標記存在或不存在)。如何在運行時創建按鈕?

代碼來保存int值

EditText total_roll_Name = (EditText) findViewById(R.id.roll_total_input); 
String total_roll_text = total_roll_Name.getText().toString(); 

i.putExtra("totalRoll", total_roll_text); 
+0

改爲使用RecyclerView。 – Maddy

回答

1

您可以像這樣動態創建按鈕。使用此代碼 onCreate

此處「num_array_name.length」是按鈕長度的總數。

linear = (LinearLayout) findViewById(R.id.linear); 
LayoutParams param = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1.0f); 

Button[] btn = new Button[num_array_name.length]; 
for (int i = 0; i < num_array_name.length; i++) { 
    btn[i] = new Button(getApplicationContext()); 
    btn[i].setText(num_array_name[i].toString()); 
    btn[i].setTextColor(Color.parseColor("#000000")); 
    btn[i].setTextSize(18); 
    btn[i].setHeight(110); 
    btn[i].setLayoutParams(param); 
    btn[i].setPadding(10, 5, 10, 5); 
    linear.addView(btn[i]); 

    btn[i].setOnClickListener(handleOnClick(btn[i])); 

} 

的onCreate後創建的返回類型的一種方法View.OnClickListener 這樣的..

View.OnClickListener handleOnClick(final Button button) { 
    return new View.OnClickListener() { 
     public void onClick(View v) { 
     } 
    }; 
} 

希望你得到的答案,可能這個答案可以幫助你。

1

創建按鈕運行時通過下面的代碼,並添加到線性佈局

Button dButton= new Button(this); 
      myButton.setText("Add Me"); 

      LinearLayout lay= (LinearLayout)findViewById(R.id.buttonlayout); 
      LayoutParams layPar= new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); 
      lay.addView(dButton, layPar); 
+0

如果使用100次或更多次循環代碼,應用程序是否會保持穩定? –

+0

在運行時添加視圖沒有什麼不同.App會很穩定 –

0

我認爲你需要考勤模塊,對不對?。​​你擁有所有的學生在數據庫中滾動否。如果你有自定義列表視圖中的數據庫與複選框,你可以很容易標記或取消標記存在或不。我已經開發了這種類型的系統。如果你需要然後我送你。

+0

如果您發送鏈接到它,它將非常有幫助。 –

+0

,但首先你要說明你需要哪種類型的模塊。因此,我發送單獨的模塊 –

+0

你在所有的應用程序中看到的應用程序管理系統與列表視圖與複選框,你可以檢查有關的學生。你需要這種類型的模塊? –