简体   繁体   English

如何在前一个屏幕android中使用给定输入数字的edittext,textview和图像创建动态布局?

[英]How to create dynamic layout with edittext , textview and images with given input number in previous screen android?

Hi in previous screen, say for eg: if user selects 2, then in next screen I need to display as, 嗨,在上一个屏幕中,例如:如果用户选择2,那么在下一个屏幕中我需要显示为,

static image> followed by Student No 1. --> TextView 静态图像>后跟学生编号1. - > TextView

Student Name-->TextView -> EditText 学生姓名 - > TextView - > EditText

Roll No------> TextView -> EditText 滚动否------> TextView - > EditText

static image> Student No 2. --> TextView 静态图像>学生2号。 - > TextView

Student Name-->TextView -> EditText 学生姓名 - > TextView - > EditText

Roll No------> TextView -> EditText 滚动否------> TextView - > EditText

So based on the given input, the given fields should increase. 因此,基于给定的输入,给定的字段应该增加。 How to do and proceed. 怎么办,继续。 Please help me. 请帮我。

Thanks. 谢谢。

Here is what I tried and strucked. 这是我尝试和打击的内容。 From the given link I got the required number and stored in preference. 从给定的链接,我得到了所需的数字,并优先存储。

String number = mPreference.getStringFromPreference("number"); 字符串编号= mPreference.getStringFromPreference(“number”); //say for eg: this number has 2 //比如说:这个数字有2个

How to dynamically increase the layout fields based on given input. 如何根据给定的输入动态增加布局字段。

Here is xml design and static xml design of my output. 这是我输出的xml设计和静态xml设计。

   <?xml version="1.0" encoding="utf-8"?>
   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical" android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:background="#EAEAEA">
  <RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <LinearLayout
        android:id="@+id/personal_info_container_layout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/nav_login_grey"/>
        <TextView
            android:id="@+id/numbers"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textStyle="bold"
            android:text="Student No:1"
            android:paddingLeft="10dp"
            android:paddingTop="10dp"
            android:textSize="20sp"/>
            </LinearLayout>
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="1dip"
            android:orientation="vertical" >
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:text="Telephone">
            </TextView>

            <EditText
                android:layout_width="match_parent"
                android:layout_height="30dp"
                android:inputType="phone"
                android:ems="10"
                android:background="@drawable/edittext_border"
                android:padding="5dp"
                android:id="@+id/editText" />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:text="Email*"/>

            <EditText
                android:layout_width="match_parent"
                android:layout_height="30dp"
                android:inputType="textEmailAddress"
                android:ems="10"
                android:padding="5dp"
                android:background="@drawable/edittext_border"
                android:id="@+id/email" />

        </LinearLayout>

    </LinearLayout>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Next"
        android:textColor="#FFFFFF"
        android:id="@+id/button"
        android:background="#8C8C8C"
        android:layout_alignParentBottom="true"
        android:textAllCaps="false"/>
</RelativeLayout>

Here is static output. 这是静态输出。

在此输入图像描述

But expected output should be in dynamic generation based on given input. 但是预期的输出应该基于给定的输入进行动态生成。 在此输入图像描述

Please help me to solve this. 请帮我解决这个问题。

Thanks 谢谢

You can use RecyclerView or ListView . 您可以使用RecyclerViewListView They meant to be used for dynamic views like in your case. 它们意味着用于像您的情况一样的动态视图。

您可以使用RecyclerView ,每次只更新列表项的大小,并且您必须在单独的xml中创建一次视图(即列表项)。

Try this. 尝试这个。 Customize as per your need. 根据您的需要定制。

MainActivity 主要活动

public class MainActivity extends Activity {

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

   Intent i = new Intent(MainActivity.this,SecondActivity.class);
   i.putExtra("VALUE", 2); //Dynamic value
   startActivity(i);


}

}

SecondActivity SecondActivity

public class SecondActivity extends Activity {

LinearLayout LLMain;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.second);

    LLMain = (LinearLayout)findViewById(R.id.Linearroot);

    Intent intent = getIntent();
    int Value = intent.getIntExtra("VALUE", 0);

    for(int i = 0;i < Value;i++)
    {
        CreateLayout(i);
    }

}

public void CreateLayout(int val)
{
    LinearLayout LLTop = new LinearLayout(this);
    LLTop.setOrientation(LinearLayout.HORIZONTAL);

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

    LinearLayout.LayoutParams paramT,paramB,param1,param2,param3,param4,param5,param6;

    paramT = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    paramT.setMargins(20, 10, 20, 10);

    paramB = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    paramB.setMargins(20, 10, 20, 10);

    param1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);

    param2 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    param2.gravity = Gravity.CENTER_VERTICAL;

    param3 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);

    param4 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);

    param5 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);

    param6 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);

    ImageView iv = new ImageView(this);
    iv.setBackgroundResource(R.drawable.ic_launcher);

    TextView tv = new TextView(this);
    tv.setText("Student" + val);


    TextView tv1 = new TextView(this);
    tv1.setText("Phone");

    TextView tv2 = new TextView(this);
    tv2.setText("Email");

    EditText et = new EditText(this);

    EditText et1 = new EditText(this);

    LLTop.addView(iv, param1);
    LLTop.addView(tv, param2);

    LLBtm.addView(tv1, param3);
    LLBtm.addView(et, param4);
    LLBtm.addView(tv2,param5);
    LLBtm.addView(et1,param6);


    LLMain.addView(LLTop, paramT);
    LLMain.addView(LLBtm,paramB);


}

}

second.xml second.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/Linearroot"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".SecondActivity" >

</LinearLayout>

Hope it helps.! 希望能帮助到你。!

如果您有动态添加的有限项目,则通过创建RelativityLayout或LinearLayout添加您的edittext或textview或视图,并使用addView()方法添加视图,或者如果您有无限项动态添加,则意味着使用ListView或RecyclerView

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM