2015-12-02 52 views
0

在我的android項目中,我在activity_investments.xml中使用了三個edittext和四個按鈕。 在activity_investments如果我按下添加字段按鈕,整個佈局我的意思是三個edittext與四個按鈕應該再次顯示 以前的佈局..我不知道該怎麼做。如何在android中的按鈕中獲取動態佈局/字段

請幫我弄這個..

下面是我code..any幫助將不勝感激...在此先感謝..

Investments.java

package com.example.miisky; 

    import android.app.Activity; 
    import android.content.Context; 
    import android.content.Intent; 
    import android.graphics.Color; 
    import android.graphics.Typeface; 
    import android.net.Uri; 
    import android.os.Bundle; 
    import android.text.SpannableString; 
    import android.text.method.LinkMovementMethod; 
    import android.text.style.ClickableSpan; 
    import android.text.style.ForegroundColorSpan; 
    import android.view.Menu; 
    import android.view.MenuItem; 
    import android.view.View; 
    import android.view.View.OnClickListener; 
    import android.widget.Button; 
    import android.widget.CheckBox; 
    import android.widget.TextView; 
    import android.widget.TextView.BufferType; 

    public class Investments extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_investments); 

     Button btn = (Button)findViewById(R.id.button10); 



     Typeface font1 = Typeface.createFromAsset(getAssets(), "fontawesome-webfont.ttf"); 
     TextView txt1 = (TextView)findViewById(R.id.imageView6); 
     txt1.setTypeface(font1); 

     Typeface font2 = Typeface.createFromAsset(getAssets(), "fontawesome-webfont.ttf"); 
     TextView txt2 = (TextView)findViewById(R.id.imageView7); 
     txt2.setTypeface(font2); 

     Typeface font3 = Typeface.createFromAsset(getAssets(), "fontawesome-webfont.ttf"); 
     TextView txt3 = (TextView)findViewById(R.id.imageView8); 
     txt3.setTypeface(font3); 

     Typeface font4 = Typeface.createFromAsset(getAssets(), "fontawesome-webfont.ttf"); 
     TextView txt4 = (TextView)findViewById(R.id.imageView9); 
     txt4.setTypeface(font4); 

     Typeface font5 = Typeface.createFromAsset(getAssets(), "fontawesome-webfont.ttf"); 
     TextView txt5 = (TextView)findViewById(R.id.imageView10); 
     txt5.setTypeface(font5); 

     txt1.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       Uri uri = Uri.parse("http://www.facebook.com"); // missing 'http://' will cause crashed 
       Intent intent = new Intent(Intent.ACTION_VIEW, uri); 
       startActivity(intent); 
      } 
     }); 

     txt2.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       Uri uri = Uri.parse("http://www.twitter.com"); // missing 'http://' will cause crashed 
       Intent intent = new Intent(Intent.ACTION_VIEW, uri); 
       startActivity(intent); 
      } 
     }); 

     txt3.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       Uri uri = Uri.parse("http://www.linkedin.com"); // missing 'http://' will cause crashed 
       Intent intent = new Intent(Intent.ACTION_VIEW, uri); 
       startActivity(intent); 
      } 
     }); 

     txt4.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       Uri uri = Uri.parse("http://www.googleplus.com"); // missing 'http://' will cause crashed 
       Intent intent = new Intent(Intent.ACTION_VIEW, uri); 
       startActivity(intent); 
      } 
     }); 

     txt5.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       Uri uri = Uri.parse("http://www.skype.com"); // missing 'http://' will cause crashed 
       Intent intent = new Intent(Intent.ACTION_VIEW, uri); 
       startActivity(intent); 
      } 
     }); 


    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.investments, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 
     if (id == R.id.action_settings) { 
      return true; 
     } 
     return super.onOptionsItemSelected(item); 
    } 
    } 

activity_investments.xml

<?xml version="1.0" encoding="utf-8"?> 
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#fff"> 

    <LinearLayout android:id="@+id/header" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:background="#fff" 
      android:paddingLeft="0dp" 
      android:paddingRight="0dp" 
      android:paddingTop="5dip" 
      android:paddingBottom="5dip"> 

    <ImageView 
     android:id="@+id/imageView1" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_marginTop="0dp" 
     android:src="@drawable/miilogo" /> 
    </LinearLayout> 

    <ScrollView 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_below="@+id/header" > 

    <LinearLayout 
    android:id="@+id/root_layout" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="#FFFFFFFF" 
    android:layout_weight="1" 
    android:layout_marginTop="10dp" 
    android:orientation="vertical"> 


    <TextView 
       android:id="@+id/textview1" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Investments" 
       android:layout_gravity="center" 
       android:textSize="20dp" 
       android:layout_marginBottom="10dp" 
       android:layout_marginLeft="9dp" 
       android:layout_marginTop="15dp" 
       android:layout_marginRight="9dp" 
       android:textColor="#000" 
       android:textStyle="bold"/> 

     <LinearLayout 
      android:id="@+id/panelInvest" 
      android:orientation="vertical" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:background="#FFFFFFFF"> 

      <LinearLayout 
       android:id="@+id/panelInvest1" 
       android:layout_width="280dp" 
       android:layout_height="40dp" 
       android:layout_weight="1" 
       android:layout_marginTop="17dp" 
       android:layout_gravity="center_horizontal" 
       android:background="#FFFFFFFF" 
       android:orientation="horizontal" > 

       <EditText 
        android:id="@+id/mutual" 
        android:layout_width="wrap_content" 
        android:layout_height="match_parent" 
        android:layout_weight="0.81" 
        android:background="@drawable/verifyedit" 
        android:ems="10" 
        android:paddingLeft="10dp" 
        android:hint="Mutual funds" > 

        <requestFocus /> 
       </EditText> 
      </LinearLayout> 

     <LinearLayout 
       android:id="@+id/panelInvest2" 
       android:layout_width="280dp" 
       android:layout_height="40dp" 
       android:layout_weight="1" 
       android:layout_marginTop="8dp" 
       android:layout_gravity="center_horizontal" 
       android:background="#FFFFFFFF" 
       android:orientation="horizontal" > 

      <EditText 
       android:id="@+id/houseprop" 
       android:layout_width="wrap_content" 
       android:layout_height="match_parent" 
       android:layout_weight="0.81" 
       android:background="@drawable/verifyedit" 
       android:ems="10" 
       android:paddingLeft="10dp" 
       android:hint="House Property" /> 

     </LinearLayout> 

     <LinearLayout 
      android:id="@+id/panelInvest3" 
      android:layout_width="280dp" 
      android:layout_height="40dp" 
      android:layout_weight="1" 
      android:layout_marginTop="8dp" 
      android:layout_gravity="center_horizontal" 
      android:background="#FFFFFFFF" 
      android:orientation="horizontal" > 

      <EditText 
       android:id="@+id/shares" 
       android:layout_width="wrap_content" 
       android:layout_height="match_parent" 
       android:layout_weight="0.81" 
       android:background="@drawable/verifyedit" 
       android:ems="10" 
       android:paddingLeft="10dp" 
       android:hint="Shares" /> 

     </LinearLayout> 



     <LinearLayout 
      android:id="@+id/panelInvest5" 
      android:layout_width="240dp" 
      android:layout_height="35dp" 
      android:layout_marginTop="5dp" 
      android:layout_marginBottom="20dp" 
      android:background="#FFFFFFFF" 
      android:orientation="horizontal" 
      android:layout_gravity="center"> 

      <Button 
       android:id="@+id/button7" 
       android:layout_width="240dp" 
       android:layout_height="35dp" 
       android:background="#042a36" 
       android:text="Save" 
       android:clickable="true" 
       android:textColor="#fff" 
       android:textSize="15dp" 
       android:textStyle="bold" /> 

      </LinearLayout> 

     <LinearLayout 
      android:id="@+id/panelInvest6" 
      android:layout_width="240dp" 
      android:layout_height="35dp" 
      android:layout_marginBottom="20dp" 
      android:background="#FFFFFFFF" 
      android:orientation="horizontal" 
      android:layout_gravity="center"> 

      <Button 
       android:id="@+id/button8" 
       android:layout_width="240dp" 
       android:layout_height="35dp" 
       android:background="#042a36" 
       android:text="Save and Continue" 
       android:clickable="true" 
       android:textColor="#fff" 
       android:textSize="15dp" 
       android:textStyle="bold" /> 

      </LinearLayout> 

      <LinearLayout 
      android:id="@+id/panelInvest7" 
      android:layout_width="240dp" 
      android:layout_height="35dp" 
      android:layout_marginBottom="20dp" 
      android:background="#FFFFFFFF" 
      android:orientation="horizontal" 
      android:layout_gravity="center"> 

      <Button 
       android:id="@+id/button9" 
       android:layout_width="240dp" 
       android:layout_height="35dp" 
       android:background="#042a36" 
       android:text="Edit" 
       android:clickable="true" 
       android:textColor="#fff" 
       android:textSize="15dp" 
       android:textStyle="bold" /> 

      </LinearLayout> 

      <LinearLayout 
      android:id="@+id/panelInvest8" 
      android:layout_width="240dp" 
      android:layout_height="35dp" 
      android:layout_marginBottom="20dp" 
      android:background="#FFFFFFFF" 
      android:orientation="horizontal" 
      android:layout_gravity="center"> 

      <Button 
       android:id="@+id/button10" 
       android:layout_width="240dp" 
       android:layout_height="35dp" 
       android:background="#042a36" 
       android:text="Add Fields" 
       android:clickable="true" 
       android:textColor="#fff" 
       android:textSize="15dp" 
       android:textStyle="bold" /> 

      </LinearLayout> 

     </LinearLayout> 

    <LinearLayout 
     android:id="@+id/foot" 
     android:layout_width="fill_parent" 
     android:layout_height="50dp" 
     android:background="#1a292e" 
     android:orientation="horizontal" 
     android:paddingBottom="0dp" 
     android:paddingLeft="0dp" 
     android:paddingRight="0dp" > 

      <TextView 
      android:id="@+id/txtview" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_horizontal" 
      android:paddingLeft="110dp" 
      android:paddingTop="5dp" 
      android:text="FOOTER" 
      android:textColor="#006064" 
      android:textSize="30dp" /> 

     </LinearLayout> 

     <LinearLayout 
       android:id="@+id/header1" 
       android:orientation="horizontal" 
       android:layout_width="fill_parent" 
       android:layout_height="25dp" 
       android:background="#042a36" 
       android:paddingRight="0dp" 
       android:paddingTop="5dip" 
       android:paddingLeft="22dp" 
       android:paddingBottom="5dip"> 

       <TextView 
       android:id="@+id/textview22" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center_horizontal" 
       android:textSize="10dp" 
       android:clickable="true" 
       android:layout_marginLeft="27dp" 
       android:paddingRight="22dp" 
       android:text="@string/Copyright" 
       android:textColor="#fff" /> 

       </LinearLayout> 

       <LinearLayout 
        android:id="@+id/header2" 
        android:orientation="horizontal" 
        android:layout_width="fill_parent" 
        android:layout_height="40dp" 
        android:background="#042a36" 
        android:paddingLeft="0dp" 
        android:paddingRight="0dp" 
        android:paddingBottom="2dp" 
        android:layout_gravity="center_horizontal" 
        > 

       <TextView 
       android:id="@+id/imageView6" 
       android:layout_width="22dp" 
       android:layout_height="22dp" 
       android:layout_marginLeft="85dp" 
       android:layout_below="@+id/header1" 
       android:text="@string/fb1" 
       android:clickable="true" 
       android:textColor="#9e9e9e" 
       android:textSize="23dp"/> 

       <TextView 

        android:id="@+id/imageView7" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_marginLeft="10dp" 
        android:layout_below="@+id/header1" 
        android:text="@string/twitter" 
        android:clickable="true" 
        android:textColor="#9e9e9e" 
        android:textSize="23dp"/> 

       <TextView 

        android:id="@+id/imageView8" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_marginLeft="10dp" 
        android:layout_below="@+id/header1" 
        android:text="@string/linkedin1" 
        android:clickable="true" 
        android:textColor="#9e9e9e" 
        android:textSize="23dp"/> 

       <TextView 

        android:id="@+id/imageView9" 
        android:layout_width="22dp" 
        android:layout_height="22dp" 
        android:layout_marginLeft="10dp" 
        android:layout_below="@+id/header1" 
        android:text="@string/googleplus" 
        android:clickable="true" 
        android:textColor="#9e9e9e" 
        android:textSize="23dp"/> 


       <TextView 

        android:id="@+id/imageView10" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_marginLeft="10dp" 
        android:layout_below="@+id/header1" 
        android:text="@string/skype" 
        android:clickable="true" 
        android:textColor="#9e9e9e" 
        android:textSize="23dp"/> 

      </LinearLayout> 

    </LinearLayout> 
    </ScrollView> 
    </RelativeLayout> 
低於3210

見圖片... Image

+0

它只是谷歌,很多教程可供選擇。 [可能的重複。](http://stackoverflow.com/questions/6216547/android-dynamically-add-views-into-view) –

+0

在許多教程中只顯示一個字段..但在這裏我的情況下,我想整個佈局應該顯示一個按鈕後點擊 –

+1

http://android-er.blogspot.in/2013/05/add-and-remove-view-dynamically.html看到這個 –

回答

0

,您可以創建並添加像下面

protected void onCreate(Bundle savedInstanceState) 
    { 
EditText etDateF,etDateT,etLocation,etFormNo,etVIN,etEngine,etName; 
    Button btSearch; 
    LinearLayout liBottom; 
    static int iState = 0; // 1=complaint;2=failure 
    String[][] saData = null; 
     super.onCreate(savedInstanceState); 
     methods = new gtCommanMethods(this); 
     tableMaster = new TableMaster(this); 

     Display display= ((WindowManager) getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay(); 
     bConstants.SCR_WIDTH = display.getWidth(); 
     bConstants.SCR_HEIGHT = display.getHeight(); 

     LinearLayout liMain = new LinearLayout(this); 
     liMain.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT)); 
     liMain.setOrientation(LinearLayout.VERTICAL); 

     etDateF = new EditText(this); 
     etDateT = new EditText(this); 
     etLocation = new EditText(this); 
     etFormNo = new EditText(this); 
     etVIN = new EditText(this); 
     etEngine = new EditText(this); 
     etName = new EditText(this); 

     etDateF.setLayoutParams(new LinearLayout.LayoutParams(150, LinearLayout.LayoutParams.WRAP_CONTENT)); 
     etDateT.setLayoutParams(new LinearLayout.LayoutParams(150, LinearLayout.LayoutParams.WRAP_CONTENT)); 
     etLocation.setLayoutParams(new LinearLayout.LayoutParams(150, LinearLayout.LayoutParams.WRAP_CONTENT)); 
     etFormNo.setLayoutParams(new LinearLayout.LayoutParams(150, LinearLayout.LayoutParams.WRAP_CONTENT)); 
     etVIN.setLayoutParams(new LinearLayout.LayoutParams(150, LinearLayout.LayoutParams.WRAP_CONTENT)); 
     etEngine.setLayoutParams(new LinearLayout.LayoutParams(150, LinearLayout.LayoutParams.WRAP_CONTENT)); 
     etName.setLayoutParams(new LinearLayout.LayoutParams(150, LinearLayout.LayoutParams.WRAP_CONTENT)); 
     String search = getResources().getString(R.string.search); 
     btSearch = new Button(this); 
     btSearch.setText(search); 
     btSearch.setBackgroundResource(R.drawable.green_patch); 



     LinearLayout li1 = new LinearLayout(this); 
     li1.setOrientation(LinearLayout.HORIZONTAL); 
     li1.addView(new custText(this, getResources().getString(R.string.from_date))); 
     li1.addView(etDateF); 
     li1.addView(new custText(this, getResources().getString(R.string.to_date))); 
     li1.addView(etDateT); 

     LinearLayout li3 = new LinearLayout(this); 
     li3.setOrientation(LinearLayout.HORIZONTAL); 
     li3.addView(new custText(this, getResources().getString(R.string.location))); 
     li3.addView(etLocation); 

     if(iState == 1) 
      li3.addView(new custText(this, getResources().getString(R.string.complaint_no))); 
     else if(iState == 2) 
      li3.addView(new custText(this, getResources().getString(R.string.failure_no))); 

     li3.addView(etFormNo); 

     LinearLayout li5 = new LinearLayout(this); 
     li5.setOrientation(LinearLayout.HORIZONTAL); 
     li5.addView(new custText(this, getResources().getString(R.string.vin))); 
     li5.addView(etVIN); 
     li5.addView(new custText(this, getResources().getString(R.string.eng_no))); 
     li5.addView(etEngine); 

     LinearLayout li7 = new LinearLayout(this); 
     li7.setOrientation(LinearLayout.HORIZONTAL); 
     li7.addView(new custText(this, getResources().getString(R.string.name))); 
     li7.addView(etName); 
     li7.addView(btSearch); 

     LinearLayout liUpper = new LinearLayout(this); 
     liUpper.setOrientation(LinearLayout.VERTICAL); 

     liUpper.addView(li1); 
     liUpper.addView(li3); 
     liUpper.addView(li5); 
     liUpper.addView(li7); 

     liBottom = new LinearLayout(this); 
     liBottom.setOrientation(LinearLayout.VERTICAL); 

     LinearLayout liTableHeader = new LinearLayout(this); 
     liTableHeader.setOrientation(LinearLayout.HORIZONTAL); 
     liTableHeader.setBackgroundColor(Color.LTGRAY); 
     liTableHeader.setPadding(0, 5, 0, 5); 


     liTableHeader.addView(new custText(this,getResources().getString(R.string.name),true),new LinearLayout.LayoutParams(bConstants.SCR_WIDTH/3, LinearLayout.LayoutParams.WRAP_CONTENT)); 
     liTableHeader.addView(new custText(this, getResources().getString(R.string.phone_no),true),new LinearLayout.LayoutParams(bConstants.SCR_WIDTH/3, LinearLayout.LayoutParams.WRAP_CONTENT)); 
     liTableHeader.addView(new custText(this, getResources().getString(R.string.frame_vin_no),true),new LinearLayout.LayoutParams(bConstants.SCR_WIDTH/3, LinearLayout.LayoutParams.WRAP_CONTENT)); 


     liMain.addView(liUpper,new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)); 

     liMain.addView(liTableHeader,new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)); 
     liMain.addView(liBottom,new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)); 
     setContentView(liMain);}