Java 如何在以前的android屏幕上使用edittext、textview和具有给定输入编号的图像创建动态布局?
在上一个屏幕中,比如说:如果用户选择2,那么在下一个屏幕中,我需要显示为 静态图像>后跟1号学生。-->文本视图 学生姓名-->文本视图 ->编辑文本 卷号------->TextView ->编辑文本 静态图像>学生2号。-->文本视图 学生姓名-->文本视图 ->编辑文本 卷号------->TextView ->编辑文本 因此,基于给定的输入,给定的字段应该增加。如何做和进行。请帮帮我 谢谢 这是我尝试和尝试的。 我得到了所需的号码并优先存储 字符串编号=mPreference.getStringFromPreference(“编号”); //例如:这个数字有2个 如何根据给定的输入动态增加布局字段 下面是我的输出的xml设计和静态xml设计Java 如何在以前的android屏幕上使用edittext、textview和具有给定输入编号的图像创建动态布局?,java,android,xml,android-layout,android-studio,Java,Android,Xml,Android Layout,Android Studio,在上一个屏幕中,比如说:如果用户选择2,那么在下一个屏幕中,我需要显示为 静态图像>后跟1号学生。-->文本视图 学生姓名-->文本视图 ->编辑文本 卷号------->TextView ->编辑文本 静态图像>学生2号。-->文本视图 学生姓名-->文本视图 ->编辑文本 卷号------->TextView ->编辑文本 因此,基于给定的输入,给定的字段应该增加。如何做和进行。请帮帮我 谢谢 这是我尝试和尝试的。 我得到了所需的号码并优先存储 字符串编号=mPreference.getSt
<?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>
这里是静态输出
但预期输出应该是基于给定输入的动态生成。
请帮我解决这个问题
谢谢您可以使用或。它们用于动态视图,如您的情况。您可以使用它,每次只需更新列表项的大小,您就必须创建视图(即列表项)仅在单独的xml中添加一次。如果要动态添加的项目有限,则表示通过创建RelativityLayout或LinearLayout来添加edittext或textview或视图,并使用addView()方法添加视图;如果要动态添加的项目不限,则表示使用ListView或RecyclerView尝试此操作。根据您的需要定制 主要活动
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);
}
}
第二项活动
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);
}
}
公共类SecondActivity扩展活动{
线性布局;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
//TODO自动生成的方法存根
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
LLMain=(LinearLayout)findViewById(R.id.Linearroot);
Intent=getIntent();
int Value=intent.getIntExtra(“Value”,0);
for(int i=0;i
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>
希望能有所帮助谢谢..你有任何示例或参考链接吗?你检查了我附加到RecyclerView的链接了吗?它已经很好地解释了示例。你只需要用他的视图替换你的视图。