2012-07-29 177 views
7

我試圖獲得一個添加按鈕,以基於按鈕左側的edittext向佈局添加另一個按鈕。重點是讓一個人在房間裏列出房間,然後當他們在每個房間裏輸入時,會產生一個新的按鈕,以便他們可以點擊房間,然後開始下一頁的工作。Android:以編程方式將按鈕添加到佈局

我有一個xml佈局全部完成,然後我意識到我是以編程的方式添加按鈕,所以我以編程方式重新編排佈局,然後在開關/案例中(這是我如何做onclicks)添加按鈕我試圖添加一個按鈕到視圖,但它變得非常棘手。我想在edittext和添加按鈕下面有一個滾動視圖,當他們將所有的房間添加到他們的房子時,它最終會爲他們整個房屋填充一個可滾動的按鈕列表。有沒有一種方法來以編程方式將按鈕添加到xml的佈局。我在想你可以,但是我所嘗試的一切都不起作用。

感謝您的幫助每個人,任何建議你將不勝感激。

首先編輯(響應Tanuj的解決方案)

我的XML文件(不知道如果我們要使用這個或者只是使用java):

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

<TextView 
    android:id="@+id/tvAddARoom" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/tvAddARoom" /> 

<EditText 
    android:id="@+id/etAddARoom" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:hint="@string/etAddARoom" /> 


<Button 
    android:id="@+id/btnAddARoom" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/btnAdd" /> 

<TextView 
    android:id="@+id/tvSelectARoom" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/tvSelectARoom" /> 

<TextView 
    android:id="@+id/tvNoRooms" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/tvNoRooms" /> 

<Button 
    android:id="@+id/btnViewAll" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/btnViewAll" /> 

</LinearLayout> 

和Java。這是不正確的,因爲在java中我創建整個佈局,而不是使用上面的佈局。只是不確定我能否將這兩者聯繫起來。

package com.bluej.movingbuddy; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.ViewGroup.LayoutParams; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.LinearLayout; 
//import android.widget.ScrollView; 
import android.widget.TextView; 

public class EstimatorByRoom extends Activity implements OnClickListener { 
String roomName; 
EditText etAddARoom; 
LinearLayout layout; 
LinearLayout.LayoutParams layoutParam; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    //setContentView(R.layout.estimatorbyroom); 

    LayoutParams params = 
      new LinearLayout.LayoutParams(
        LayoutParams.FILL_PARENT, 
        LayoutParams.WRAP_CONTENT); 

    //create a layout 
    LinearLayout layout = new LinearLayout(this); 
    layout.setOrientation(LinearLayout.VERTICAL); 

    //create a text view 
    TextView tvAddARoom = new TextView(this); 
    tvAddARoom.setText("Add a Room"); 
    tvAddARoom.setLayoutParams(params); 

    //create an edittext 
    EditText etAddARoom = new EditText(this); 
    etAddARoom.setHint("Living Room, Dining Room, etc."); 
    etAddARoom.setLayoutParams(params); 


    //create a button 
    Button btnAddARoom = new Button(this); 
    btnAddARoom.setText("Add"); 
    btnAddARoom.setLayoutParams(params); 

    //adds the textview 
    layout.addView(tvAddARoom); 

    //add the edittext 
    layout.addView(etAddARoom); 
    //add the button 
    layout.addView(btnAddARoom); 

    //create the layout param for the layout 
    LinearLayout.LayoutParams layoutParam = new LinearLayout.LayoutParams(
      LayoutParams.FILL_PARENT, 
      LayoutParams.WRAP_CONTENT); 

    this.addContentView(layout, layoutParam); 
} 

public void onClick(View v) { 
    // TODO Auto-generated method stub 
    switch (v.getId()) { 

    case R.id.btnAddARoom: 
     //add a room 

     //this part isn't working! 
     roomName = etAddARoom.getText().toString(); 
     Button createdButton = new Button(this); 
     createdButton.setText(roomName); 
     layout.addView(createdButton); 
     this.addContentView(layout, layoutParam); 

     //if no rooms make tvnorooms disappear 

     break; 
    } 

} 
} 

回答

13

試試這個:

//the layout on which you are working 
    LinearLayout layout = (LinearLayout) findViewById(R.id.linear_layout_tags); 

    //set the properties for button 
    Button btnTag = new Button(this); 
    btnTag.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 
    btnTag.setText("Button"); 
    btnTag.setId(some_random_id); 

    //add button to the layout 
    layout.addView(btnTag); 
+0

嘗試刪除'this.addContentView(佈局,layoutParam);' – 2012-07-29 16:52:34

+0

這可能幫助的http:// stackoverflow.com/questions/4269660/dynamically-adding-content-to-relativelayout – 2012-07-29 16:53:00

3

我會在XML添加ID到您的LinearLayout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@id/llContainer" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" > 

,然後改變你的onClick這樣:

public void onClick(View v) { 
    switch (v.getId()) { 

    case R.id.btnAddARoom: 
     //add a room 
     //Find you parent layout which we'll be adding your button to: 
     LinearLayout layout = (LinearLayout) findViewById(R.id.llContainer); 

     roomName = etAddARoom.getText().toString(); 
     Button createdButton = new Button(this); 
     createdButton.setText(roomName); 
     createdButton.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,   LayoutParams.WRAP_CONTENT)); 
     layout.addView(createdButton); 

     //if no rooms make tvnorooms disappear 

     break; 
    } 
} 
+0

弗拉基米爾,謝謝你考慮看看。我嘗試了你上面提供的內容,並且同意你的想法,但是當按下添加按鈕時,該應用程序似乎不會執行任何操作。任何其他想法?我們缺少什麼? – bluej 2012-07-29 16:51:34

+0

onClick是否觸發?在那裏添加一個日誌輸出來查明。或者在onClick方法和調試中設置一個斷點。 我會創建一個專用於此按鈕的點擊偵聽器,因此不需要使用switch語句。 – 2012-07-29 16:59:35

+0

好吧,我試着做了一個日誌,但是這個東西像爆炸一樣爆炸了,所以我顯然不知道如何做日誌。所以,儘管如此,我想到了一個新的方法來做到這一點。我在想,當我點擊添加,而不是創建按鈕時,我要添加房間到SQLite數據庫中的房間表。然後我將查詢SQLite數據庫並基於數據庫中的數據創建按鈕。如果有房間,那麼它會給我按鈕,如果沒有房間,它會說「沒有房間」。 – bluej 2012-07-29 17:23:00

1

每個按鈕都需要一個onclicklistener來告訴我該怎麼做。這可以添加到您的Java代碼下您的按鈕狀態。

Button createdButton = new Button(this); 
createdButton.setOnClickListener(new OnClickListener() 
{ 

code you want implemented 

} 
6

試試這個代碼:

LinearLayout l_layout = (LinearLayout) findViewById(R.id.linear_layout); 
l_layout.setOrientation(LinearLayout.VERTICAL); 

Button btn1 = new Button(this); 
btn1.setText("Button_text"); 

l_layout.addView(btn1); 

btn1.setOnClickListener(new OnClickListener() 
{ 
    @Override 
    public void onClick(View v) { 
      // put code on click operation 
    } 
} 

是動態創建按鈕,添加在佈局方式。

記住,當你創建按鈕編程您只需使用沒有Class_name.this

1
public class AndroidWalkthroughApp1 extends Activity implements View.OnClickListener { 

    final int TOP_ID = 3; 
    final int BOTTOM_ID = 4; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     // create two layouts to hold buttons 
     LinearLayout top = new LinearLayout(this); 
     top.setId(TOP_ID); 
     LinearLayout bottom = new LinearLayout(this); 
     bottom.setId(BOTTOM_ID); 

     // create buttons in a loop 
     for (int i = 0; i < 2; i++) { 
      Button button = new Button(this); 
      button.setText("Button " + i); 
      // R.id won't be generated for us, so we need to create one 
      button.setId(i); 

      // add our event handler (less memory than an anonymous inner class) 
      button.setOnClickListener(this); 

      // add generated button to view 
      if (i == 0) { 
       top.addView(button); 
      } 
      else { 
       bottom.addView(button); 
      } 
     } 

     // add generated layouts to root layout view 
     LinearLayout root = (LinearLayout)this.findViewById(R.id.root_layout); 
     root.addView(top); 
     root.addView(bottom); 
    } 

    @Override 
    public void onClick(View v) { 
     // show a message with the button's ID 
     Toast toast = Toast.makeText(AndroidWalkthroughApp1.this, "You clicked button " + v.getId(), Toast.LENGTH_LONG); 
     toast.show(); 

     // get the parent layout and remove the clicked button 
     LinearLayout parentLayout = (LinearLayout)v.getParent(); 
     parentLayout.removeView(v); 
    } 
} 
相關問題