2012-07-29 39 views
7

Tôi đang cố gắng thêm nút thêm để thêm nút khác vào bố cục, dựa trên văn bản bên trái của nút. Vấn đề là để một người liệt kê các phòng trong nhà của họ, và sau đó khi họ gõ vào mỗi phòng, một nút mới được tạo ra để họ có thể bấm vào phòng, và sau đó bắt đầu làm việc trên trang tiếp theo.Android: thêm nút lập trình vào bố cục

Tôi đã hoàn thành bố cục xml và sau đó tôi nhận ra rằng tôi đang "thêm lập trình" thêm nút, vì vậy tôi đã xóa lại bố cục theo chương trình và sau đó trong chuyển đổi/trường hợp (đó là cách tôi thực hiện onclicks) cho nút thêm Tôi đã cố gắng thêm một nút để xem, nhưng nó nhận được rất phức tạp. Tôi muốn có một scrollview dưới edittext và thêm các nút, và khi họ thêm tất cả các phòng vào nhà của họ nó cuối cùng là dân cư với một danh sách các nút cuộn cho toàn bộ nhà của họ. Có cách nào để thêm các nút lập trình vào bố cục xml'd hay không. Tôi đã nghĩ bạn có thể nhưng mọi thứ tôi đang cố gắng không hoạt động.

Cảm ơn sự giúp đỡ của bạn mọi người, mọi đề xuất bạn có sẽ được đánh giá cao.

Đầu tiên Edit (để đáp ứng với giải pháp Tanuj của)

tập tin XML của tôi (không chắc chắn nếu chúng ta sẽ sử dụng này hoặc chỉ cần sử dụng 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> 

Và Java. Điều này không hoàn toàn chính xác, như trong java tôi đang tạo toàn bộ bố cục thay vì sử dụng bố cục ở trên. Chỉ cần không chắc chắn nếu tôi có thể cầu cả hai.

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; 
    } 

} 
} 

Trả lời

13

Hãy thử điều này:

//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

thử tháo 'this.addContentView (bố trí, layoutParam); ' –

+0

này có thể giúp http: // stackoverflow.com/questions/4269660/dynamically-adding-content-to-relativelayout –

3

tôi sẽ thêm một id để LinearLayout của bạn trong xml:

<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" > 

Và sau đó thay đổi onClick của bạn như thế này:

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

Vladimir, cảm ơn vì đã xem. Tôi đã thử những gì bạn cung cấp ở trên và tôi đồng ý với ý tưởng của bạn, nhưng ứng dụng dường như không làm bất cứ điều gì khi nút thêm được nhấn. Bất kỳ ý tưởng nào khác? Chúng ta đang thiếu gì? – bluej

+0

Bật lửa onClick? Thêm đầu ra nhật ký ở đó để tìm hiểu. Hoặc đặt điểm ngắt bên trong phương thức onClick và gỡ lỗi. Tôi sẽ tạo trình nghe nhấp chuột dành riêng cho nút này để không cần sử dụng lệnh chuyển đổi. –

+0

Tất cả các quyền vì vậy tôi đã cố gắng làm một bản ghi nhưng đó thổi lên điều này giống như một vụ nổ hạt nhân, vì vậy tôi rõ ràng không có ý tưởng làm thế nào để làm đăng nhập. Vì vậy, với điều đó mặc dù tôi nghĩ ra một cách mới để làm điều này. Tôi nghĩ rằng khi tôi nhấp vào thêm rằng thay vì tạo ra một nút, tôi sẽ thêm phòng vào một bảng phòng trong một cơ sở dữ liệu SQLite. Và sau đó tôi sẽ truy vấn cơ sở dữ liệu SQLite và tạo các nút dựa trên dữ liệu trong cơ sở dữ liệu. Nếu có phòng, sau đó nó sẽ cho tôi nút, và nếu không có phòng nó sẽ nói "Không có phòng." – bluej

1

Mỗi nút cần phải có một onclicklistener để nói với tôi phải làm gì. điều này có thể được thêm vào mã java của bạn dưới nơi bạn đặt nút của bạn.

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

code you want implemented 

} 
6

Hãy thử mã này:

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 
    } 
} 

đó là một cách để tạo nút tự động và thêm vào Layout.

nhớ rằng khi bạn tạo nút lập trình bạn chỉ cần sử dụng này không 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); 
    } 
} 
Các vấn đề liên quan