如何在Android中单击按钮获取动态布局/字段

时间:2015-12-02 11:06:54

标签: android layout dynamic

在我的android项目中,我在activity_investments.xml中使用了三个edittext&4和四个按钮。  在activity_investments中如果我按下添加字段按钮,整个布局我的意思是三个带有四个按钮的edittext应该再次显示在下面   以前的布局..我不知道该怎么做。

请帮我解决这个问题..

以下是我的代码..任何帮助将不胜感激...提前感谢..

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>

见下图... Image

1 个答案:

答案 0 :(得分:0)

您可以创建和添加如下

Scanner input = new Scanner(System.in);

int RoxasC = 0;
int DuterteC = 0;

int yaya = 5;
   int x = 10;
    do{
     System.out.println("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=");
     System.out.println("Vote Ballot");
     System.out.println("Below are the 2 Canditates you can choose to vote from");
     System.out.println("Mar Roxas --- Code: 11");
     System.out.println("Duterte ---- Code: 12"); 
     System.out.println("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=");
     System.out.println("Who do you vote? Enter numbers only!"); 
     int choice = input.nextInt();
     System.out.println("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=");
     if (choice == 11) 
     { 

         System.out.println("You have voted Mar Roxas and not Duterte");


     } else if ( choice == 12 ) 
     {


         System.out.println("You have voted Duterte and not Mar Roxas");


     }
     else 
     {

         System.out.println("You have entered an invalid number");

     }
     String confirm = "confirm";
     String deny = "deny";
     int conf=1;
     int den=2;
     System.out.println("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=");
     System.out.println("Do you want to let another voter vote? Or would you like to end the program at hand?");

     int ans = input.nextInt();
     System.out.println("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=");
     if ( ans==1 )
     {

         System.out.println("The program would now repeat");


          if ( choice ==11 )
          {  

              RoxasC+=1;

              System.out.println("Mar roxas recieved " +RoxasC+ " number of vote/s and Duterte Recieved " +DuterteC+ 
              " number of votes");

          } else if ( choice ==12)
          {


              DuterteC+=1;
              System.out.println("Duterte recieved " +DuterteC+ " number of vote/s Roxas received " +RoxasC+ 
              " number of votes");


          }


     }
     else if (ans==2 ) 
     {


          if ( choice ==11 )
          {  



              System.out.println("Mar roxas recieved " +RoxasC+ " number of vote/s and Duterte Recieved " +DuterteC+ 
              " number of votes");

          } else if ( choice ==12)
          {


              System.out.println("Duterte recieved " +DuterteC+ " number of vote/s Roxas received " +RoxasC+ 
              " number of votes");


          }
          System.out.println("Program will end as per request");
          break;


     } else
     {
         System.out.println("You entered an invalid keyword program would still repeat");
        }

     System.out.println("\n");

   }while( yaya==5 ); //Program Runs Infinitely