我正在制作自动化应用。用户可以选择添加设备。默认情况下,在此表单中,第一个设备有1个edittext和1个微调器,然后用户可以通过单击按钮来获得更多设备。新的Edittext和微调器将在点击时创建。它现在正在创造完美但我希望其他家电选择作为图像中的第一个家电展示 - Create Appliance Form
public class CreateAppliance extends AppCompatActivity {
int i = 1;
EditText editText;
Spinner spinner;
List<EditText> ed;
List<Spinner> sp;
String[] arraySpinner;
String room;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_appliance);
final LinearLayout textLayout = (LinearLayout) findViewById(R.id.textLayout);
Button btnAddAppliance = (Button) findViewById(R.id.btnAddAppliance);
Button btnCreateAppliance = (Button) findViewById(R.id.btnCreateAppliance);
final EditText txtAppliance = (EditText) findViewById(R.id.r1);
Spinner spnAppliance = (Spinner) findViewById(R.id.spnAppliance);
arraySpinner = new String[]{"ON/OFF", "Dimmer"};
ed = new ArrayList<EditText>();
sp = new ArrayList<Spinner>();
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, arraySpinner);
spnAppliance.setAdapter(adapter);
btnAddAppliance.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
i += 1;
editText = new EditText(CreateAppliance.this);
spinner = new Spinner(CreateAppliance.this);
editText.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
spinner.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
editText.setHint("Appliance" + i);
spinner.setAdapter(adapter);
editText.setInputType(InputType.TYPE_CLASS_TEXT);
textLayout.addView(editText);
textLayout.addView(spinner);
ed.add(editText);
sp.add(spinner);
}
});
}
}
activity_create_appliance.xml
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".CreateAppliance">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_marginTop="20dp"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/r1"
android:textColor="@color/black"
android:inputType="text"
android:layout_weight="1"
android:hint="Appliance 1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"/>
<Spinner
android:id="@+id/spnAppliance"
android:layout_weight="2"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</Spinner>
</LinearLayout>
<LinearLayout
android:id="@+id/textLayout"
android:orientation="vertical"
android:layout_margin="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</LinearLayout>
<Button
android:id="@+id/btnAddAppliance"
android:text="Add Appliance"
android:background="@drawable/button_rounded"
android:backgroundTint="@color/light_blue_900"
android:textColor="@color/white"
android:paddingRight="40dp"
android:paddingLeft="40dp"
android:drawableRight="@drawable/plus"
android:layout_gravity="right"
android:layout_margin="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btnCreateAppliance"
android:text="Create Appliance"
android:layout_gravity="center"
android:background="@drawable/button_rounded"
android:backgroundTint="@color/light_blue_900"
android:textColor="@color/white"
android:paddingLeft="40dp"
android:paddingRight="40dp"
android:layout_marginBottom="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</ScrollView>
</android.support.constraint.ConstraintLayout>
我想要动态创建的edtitext和spinner与此相同
答案 0 :(得分:0)
试试这个可以按照您的要求运作
首先,我在您的activity_create_appliance.xml中进行了一些更改。请仔细查看。根据您的要求设置背景。我删除了它。现在更新的XML文件如下。
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/parentConstraintLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:layout_width="368dp"
android:layout_height="wrap_content"
tools:layout_editor_absoluteY="0dp"
tools:layout_editor_absoluteX="8dp">
<LinearLayout
android:id="@+id/textLayout"
android:orientation="vertical"
android:layout_margin="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</LinearLayout>
</ScrollView>
<LinearLayout
android:layout_width="368dp"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
tools:layout_editor_absoluteX="8dp">
<Button
android:id="@+id/btnAddAppliance"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_margin="10dp"
android:paddingLeft="40dp"
android:paddingRight="40dp"
android:text="Add Appliance"
android:textColor="@color/white" />
<Button
android:id="@+id/btnCreateAppliance"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="10dp"
android:paddingLeft="40dp"
android:paddingRight="40dp"
android:text="Create Appliance"
android:textColor="@color/white" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
现在执行点击时的主要逻辑是
final LinearLayout textLayout = (LinearLayout) findViewById(R.id.textLayout);
Button btnAddAppliance = (Button) findViewById(R.id.btnAddAppliance);
final ArrayList<LinearLayout> linearLayoutArrayList = new ArrayList<>();
btnAddAppliance.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LinearLayout linearLayout = new LinearLayout(CreateAppliance.this);
LinearLayout.LayoutParams linearLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
linearLayout.setLayoutParams(linearLayoutParams);
linearLayout.setId(View.generateViewId());
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
linearLayout.setWeightSum(3);
EditText editText = new EditText(CreateAppliance.this);
LinearLayout.LayoutParams editTextLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1f);
editText.setLayoutParams(editTextLayoutParams);
editText.setMaxLines(1);
editText.setId(View.generateViewId());
linearLayout.addView(editText);
Spinner spinner = new Spinner(CreateAppliance.this);
LinearLayout.LayoutParams spinnerLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, 2f);
spinner.setLayoutParams(spinnerLayoutParams);
spinner.setId(View.generateViewId());
String[] arraySpinner = new String[] {
"ON", "OFF"
};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(CreateAppliance.this,
android.R.layout.simple_spinner_item, arraySpinner);
spinner.setAdapter(adapter);
linearLayout.addView(spinner);
linearLayoutArrayList.add(linearLayout);
textLayout.addView(linearLayout);
}
});