2013-02-26 35 views
13

LinksLàm thế nào để tạo Nút động trong Android?

Tôi muốn tạo một trang như thế này. 7 nút này đã tồn tại nhưng nếu người dùng muốn thêm nhiều danh mục (nút) thì anh ta có thể sử dụng nút + và xóa bằng cách sử dụng nút -. Bất kỳ ý tưởng hoặc hướng dẫn nào về việc này?

Trả lời

22

Tạo/nút Remove onClick của + button- button như sau:

public void onClick(View v) { 

    switch(v.getId()){ 
    case (R.id.plusbutton): 
       Button myButton = new Button(this); 
       myButton.setText("Add Me"); 

       LinearLayout ll = (LinearLayout)findViewById(R.id.buttonlayout); 
       LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); 
       ll.addView(myButton, lp); 
       break;. 
    case (R.id.minusbutton): 
       Button myButton = new Button(this); 
       myButton.setText("Remove Me"); 

       LinearLayout ll = (LinearLayout)findViewById(R.id.buttonlayout); 
       LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); 
       ll.removeView(myButton, lp); 
       break; 
      } 
     } 
+0

Cảm ơn bạn sir :) đánh giá cao sự giúp đỡ của bạn –

+3

buttonlayout là gì? làm thế nào để tạo ra rằng –

8

này là dành cho nút tạo động trong android

LinearLayout row2 = (LinearLayout) findViewById(R.id.hll2); 
Button ivBowl = new Button(this); 
ivBowl.setText("hi"); 
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(70, 70); 
layoutParams.setMargins(5, 3, 0, 0); // left, top, right, bottom 
ivBowl.setLayoutParams(layoutParams); 
row2.addView(ivBowl); 
+1

+1 câu trả lời tốt. – Aravin

+0

@Aravinth thanks bro – Rohit

3

Nó khá đơn giản.

Button button1=new Button(context); 
    button1.setText("test"); 
    button1.setId(id); 
containerlayout.add(button1); 

Hy vọng điều này sẽ giúp bạn.

4
LinearLayout mainLayout = (LinearLayout)findViewById(R.id.yourlayoutidthatisonethepicture); 

Button addButton =new Button(this); 
addButton.setText("add"); 

mainLayout.addView(addButton); 

để loại bỏ là như nhau chỉ cần thay đổi này "mainLayout.addView(addButton)" để removeView hoặc setVisibility nút để View.GONE

+0

Vẫn không nhận được "R.id.yourlayoutidthatisonethepicture" xin vui lòng cho tôi biết –

+0

Xong: D cảm ơn người đàn ông! yo là thiên tài: D –

+0

vui vì tôi có thể giúp;) –

0

Nếu bạn muốn tạo chế độ xem động (như EditText, textview, v.v.) thì chỉ cần sử dụng mã này và chạy nó trong ứng dụng của bạn.

MyActivity.java://your java tập tin

LinearLayout ll = (LinearLayout)findViewById(R.id.linearLayout1); 
EditText et = new EditText(v.getContext()); 
et.setText("My new Edit Text); 
et.setMinLines(1); 
et.setMaxLines(3); 
ll.addView(et); 

Trong XML File:

<LinearLayout 
android:id="@+id/linearLayout1" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:layout_alignBottom="@+id/TextView01" 
android:layout_below="@+id/relativeLayout1" 
android:orientation="vertical" > 

Các vấn đề liên quan