2012-07-29 14 views
7

मैं बटन के बाईं ओर स्थित संपादन के आधार पर लेआउट में एक और बटन जोड़ने के लिए एक ऐड बटन प्राप्त करने का प्रयास कर रहा हूं। बिंदु एक व्यक्ति के लिए अपने घर के कमरे सूचीबद्ध करने के लिए है, और फिर जब वे प्रत्येक कमरे में टाइप करते हैं, तो एक नया बटन उत्पन्न होता है ताकि वे कमरे पर क्लिक कर सकें और फिर अगले पृष्ठ पर काम करना शुरू कर सकें।एंड्रॉइड: प्रोग्रामिंग रूप से लेआउट में बटन जोड़ना

मेरे पास एक एक्सएमएल लेआउट था, और फिर मुझे एहसास हुआ कि मैं "प्रोग्रामेटिक रूप से" बटन जोड़ रहा हूं, इसलिए मैंने लेआउट को प्रोग्रामेटिक रूप से ले लिया, और फिर स्विच बटन के लिए स्विच/केस (इस तरह मैं ऑनक्लिक करता हूं) में मैंने दृश्य में एक बटन जोड़ने की कोशिश की, लेकिन यह बहुत मुश्किल हो रहा है। मैं संपादन के नीचे एक स्क्रॉलव्यू देखना चाहता हूं और बटन जोड़ना चाहता हूं, और जैसे ही वे अपने कमरे में सभी कमरे जोड़ते हैं, अंत में यह उनके पूरे घर के लिए बटन की स्क्रोल करने योग्य सूची के साथ पॉप्युलेट होता है। Xml'd लेआउट में प्रोग्रामेटिक रूप से बटन जोड़ने का कोई तरीका है। मैं सोच रहा था कि आप कर सकते हैं लेकिन मैं जो कुछ भी कोशिश कर रहा हूं वह काम नहीं कर रहा है।

आपकी मदद के लिए धन्यवाद, आपकी सिफारिशों की बहुत सराहना की जाएगी।

पहले संपादित

मेरे एक्सएमएल फ़ाइल (तनुज के समाधान के जवाब में) (यकीन नहीं है कि हम इस का उपयोग करने के लिए या सिर्फ जावा का उपयोग जा रहे हैं):

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

और जावा। यह बिल्कुल सही नहीं है, जैसा कि जावा में मैं ऊपर लेआउट का उपयोग करने के बजाय पूरा लेआउट बना रहा हूं। बस यकीन नहीं है कि मैं दोनों को पुल कर सकता हूं। इस के लिए अपने onClick बदलने

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

और फिर:

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);' –

+0

इस http मदद कर सकता है: // stackoverflow.com/questions/4269660/ गतिशील रूप से-adding-content-to-relativelayout –

3

मैं एक्सएमएल में अपने LinearLayout को एक आईडी जोड़ना होगा

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

+0

ऑनक्लिक आग क्या है? पता लगाने के लिए वहां एक लॉग आउटपुट जोड़ें। या ऑनक्लिक विधि और डीबग के अंदर ब्रेकपॉइंट सेट करें। मैं इस बटन को समर्पित एक क्लिक श्रोता बनाना चाहता हूं इसलिए स्विच स्टेटमेंट का उपयोग करने की कोई आवश्यकता नहीं है। –

+0

ठीक है इसलिए मैंने एक लॉग करने की कोशिश की लेकिन उसने इस बात को परमाणु विस्फोट की तरह उड़ा दिया, इसलिए मुझे पता नहीं है कि लॉग कैसे करें। तो, इसके साथ ही मैंने ऐसा करने का एक नया तरीका सोचा। मैं सोच रहा हूं कि जब मैं एक बटन बनाने के बजाय जोड़ता हूं क्लिक करता हूं, तो मैं एक SQLite डेटाबेस में कमरे की मेज में कमरा जोड़ने जा रहा हूं। और फिर मैं SQLite डेटाबेस से पूछताछ करने और डेटाबेस में डेटा के आधार पर बटन बनाने जा रहा हूं। अगर कमरे हैं, तो यह मुझे बटन देगा, और यदि कमरे नहीं हैं तो यह "कोई कमरा नहीं" कहेंगे। – bluej

1

प्रत्येक बटन को मुझे बताने के लिए ऑनक्लिकलिस्टर होना चाहिए टी क्या करना है। इसे आपके जावा कोड में जोड़ा जा सकता है जहां आप अपना बटन बताते हैं।

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

जो गतिशील रूप से बनाए बटन और लेआउट में जोड़ने के लिए एक तरीका है:

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

code you want implemented 

} 
6

इस कोड का प्रयास करें।

याद है कि जब आप बटन बनाने के प्रोग्राम के रूप में आप सिर्फ इस नहीं 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); 
    } 
} 
संबंधित मुद्दे