TextInputLayout 未以编程方式显示
TextInputLayout is not showing programmatically
所以我面临一个问题,我有一个微调器,它有 4 个选项 1-4,我有 4 个 TextInputLayout
现在我想要实现的是让片段开始我想要我的 textinputlayout
被隐藏。当我点击微调器和 select 第一个选项时,只有一个 TextInputLayout
应该是可见的。当我 select 2 个选项时,我需要 2 TextInputLayout
才能可见,依此类推。现在的问题是我的 TextInputLayout
仍然不可见 我提供了 switch
案例,其中 1 被 selected 然后使 TextInputLayout
可见但它不起作用。任何帮助将不胜感激
XML 文件:
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="60dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="Choose Number of vehicle and quantity"
android:layout_marginLeft="12dp"/>
<Spinner
android:id="@+id/spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:background="@drawable/spinner_border"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/VehicleNoText1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginTop="16dp"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:layout_weight="0.5"
android:hint="Vehicle No"
android:visibility="invisible"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="12sp"
android:imeOptions="actionNext"
android:inputType="text"/>
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/QuantityText1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:hint="Quantity"
android:layout_marginTop="16dp"
android:layout_marginLeft="9dp"
android:layout_marginRight="12dp"
android:layout_weight="0.5"
android:visibility="invisible"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="12sp"
android:inputType="text"/>
</com.google.android.material.textfield.TextInputLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/VehicleNoText2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:hint="Vehicle No"
android:layout_marginTop="16dp"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:layout_weight="0.5"
android:visibility="invisible"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="12sp"
android:imeOptions="actionNext"
android:inputType="text"/>
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/QuantityText2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:hint="Quantity"
android:layout_marginTop="16dp"
android:layout_marginLeft="9dp"
android:layout_marginRight="12dp"
android:layout_weight="0.5"
android:visibility="invisible"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="12sp"
android:inputType="text"/>
</com.google.android.material.textfield.TextInputLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/VehicleNoText3"
android:layout_width="0dp"
android:layout_height="match_parent"
android:hint="Vehicle No"
android:layout_marginTop="16dp"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:layout_weight="0.5"
android:visibility="invisible"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="12sp"
android:imeOptions="actionNext"
android:inputType="text"/>
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/QuantityText3"
android:layout_width="0dp"
android:layout_height="match_parent"
android:hint="Quantity"
android:layout_marginTop="16dp"
android:layout_marginLeft="9dp"
android:layout_marginRight="12dp"
android:layout_weight="0.5"
android:visibility="invisible"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="12sp"
android:inputType="text"/>
</com.google.android.material.textfield.TextInputLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/VehicleNoText4"
android:layout_width="0dp"
android:layout_height="match_parent"
android:hint="Vehicle No"
android:layout_marginTop="16dp"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:layout_weight="0.5"
android:visibility="invisible"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="12sp"
android:imeOptions="actionNext"
android:inputType="text"/>
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/QuantityText4"
android:layout_width="0dp"
android:layout_height="match_parent"
android:hint="Quantity"
android:layout_marginTop="16dp"
android:layout_marginLeft="9dp"
android:layout_marginRight="12dp"
android:layout_weight="0.5"
android:visibility="invisible"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="12sp"
android:inputType="text"/>
</com.google.android.material.textfield.TextInputLayout>
</LinearLayout>
</LinearLayout>
</ScrollView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
我的片段class
List < String > categories = new ArrayList < > ();
categories.add("1");
categories.add("2");
categories.add("3");
categories.add("4");
ArrayAdapter < String > dataAdapter = new ArrayAdapter < > (this.getActivity(), android.R.layout.simple_spinner_item, categories);
// Drop down layout style - list view with radio button
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// attaching data adapter to spinner
vehicleSelector.setAdapter(dataAdapter);
vehicleSelector.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView << ? > adapterView, View view, int i, long l) {
String selectedItem = adapterView.getItemAtPosition(i).toString();
Log.i("Values", selectedItem);
switch (selectedItem) {
case "1":
c13.getEditText().setVisibility(View.VISIBLE);
Objects.requireNonNull(c14.getEditText()).setVisibility(View.VISIBLE);
break;
case "2":
Objects.requireNonNull(c13.getEditText()).setVisibility(View.VISIBLE);
Objects.requireNonNull(c14.getEditText()).setVisibility(View.VISIBLE);
Objects.requireNonNull(c15.getEditText()).setVisibility(View.VISIBLE);
Objects.requireNonNull(c16.getEditText()).setVisibility(View.VISIBLE);
break;
case "3":
Objects.requireNonNull(c13.getEditText()).setVisibility(View.VISIBLE);
Objects.requireNonNull(c14.getEditText()).setVisibility(View.VISIBLE);
Objects.requireNonNull(c15.getEditText()).setVisibility(View.VISIBLE);
Objects.requireNonNull(c16.getEditText()).setVisibility(View.VISIBLE);
Objects.requireNonNull(c17.getEditText()).setVisibility(View.VISIBLE);
Objects.requireNonNull(c18.getEditText()).setVisibility(View.VISIBLE);
break;
case "4":
Objects.requireNonNull(c13.getEditText()).setVisibility(View.VISIBLE);
Objects.requireNonNull(c14.getEditText()).setVisibility(View.VISIBLE);
Objects.requireNonNull(c15.getEditText()).setVisibility(View.VISIBLE);
Objects.requireNonNull(c16.getEditText()).setVisibility(View.VISIBLE);
Objects.requireNonNull(c17.getEditText()).setVisibility(View.VISIBLE);
Objects.requireNonNull(c18.getEditText()).setVisibility(View.VISIBLE);
Objects.requireNonNull(c19.getEditText()).setVisibility(View.VISIBLE);
Objects.requireNonNull(c20.getEditText()).setVisibility(View.VISIBLE);
break;
}
}
@Override
public void onNothingSelected(AdapterView << ? > adapterView) {
}
});
像这样初始化您的 TextInputEditText 变量:
TextInputEditText myEditText = findViewById(R.id.my_edit_text);
然后执行
myEditText.setVisibility(View.VISIBLE)
而不是
myTextInputLayout.getEditText().setVisibility(View.VISIBLE)
。 (这就是您正在做的,在 textInputLayout 变量上调用 getEditText()
)。
请注意,您需要为位于 textInputLayout 中的 TextInputEditText 指定 ID。
所以我面临一个问题,我有一个微调器,它有 4 个选项 1-4,我有 4 个 TextInputLayout
现在我想要实现的是让片段开始我想要我的 textinputlayout
被隐藏。当我点击微调器和 select 第一个选项时,只有一个 TextInputLayout
应该是可见的。当我 select 2 个选项时,我需要 2 TextInputLayout
才能可见,依此类推。现在的问题是我的 TextInputLayout
仍然不可见 我提供了 switch
案例,其中 1 被 selected 然后使 TextInputLayout
可见但它不起作用。任何帮助将不胜感激
XML 文件:
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="60dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="Choose Number of vehicle and quantity"
android:layout_marginLeft="12dp"/>
<Spinner
android:id="@+id/spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:background="@drawable/spinner_border"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/VehicleNoText1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginTop="16dp"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:layout_weight="0.5"
android:hint="Vehicle No"
android:visibility="invisible"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="12sp"
android:imeOptions="actionNext"
android:inputType="text"/>
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/QuantityText1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:hint="Quantity"
android:layout_marginTop="16dp"
android:layout_marginLeft="9dp"
android:layout_marginRight="12dp"
android:layout_weight="0.5"
android:visibility="invisible"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="12sp"
android:inputType="text"/>
</com.google.android.material.textfield.TextInputLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/VehicleNoText2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:hint="Vehicle No"
android:layout_marginTop="16dp"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:layout_weight="0.5"
android:visibility="invisible"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="12sp"
android:imeOptions="actionNext"
android:inputType="text"/>
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/QuantityText2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:hint="Quantity"
android:layout_marginTop="16dp"
android:layout_marginLeft="9dp"
android:layout_marginRight="12dp"
android:layout_weight="0.5"
android:visibility="invisible"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="12sp"
android:inputType="text"/>
</com.google.android.material.textfield.TextInputLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/VehicleNoText3"
android:layout_width="0dp"
android:layout_height="match_parent"
android:hint="Vehicle No"
android:layout_marginTop="16dp"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:layout_weight="0.5"
android:visibility="invisible"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="12sp"
android:imeOptions="actionNext"
android:inputType="text"/>
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/QuantityText3"
android:layout_width="0dp"
android:layout_height="match_parent"
android:hint="Quantity"
android:layout_marginTop="16dp"
android:layout_marginLeft="9dp"
android:layout_marginRight="12dp"
android:layout_weight="0.5"
android:visibility="invisible"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="12sp"
android:inputType="text"/>
</com.google.android.material.textfield.TextInputLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/VehicleNoText4"
android:layout_width="0dp"
android:layout_height="match_parent"
android:hint="Vehicle No"
android:layout_marginTop="16dp"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:layout_weight="0.5"
android:visibility="invisible"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="12sp"
android:imeOptions="actionNext"
android:inputType="text"/>
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/QuantityText4"
android:layout_width="0dp"
android:layout_height="match_parent"
android:hint="Quantity"
android:layout_marginTop="16dp"
android:layout_marginLeft="9dp"
android:layout_marginRight="12dp"
android:layout_weight="0.5"
android:visibility="invisible"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="12sp"
android:inputType="text"/>
</com.google.android.material.textfield.TextInputLayout>
</LinearLayout>
</LinearLayout>
</ScrollView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
我的片段class
List < String > categories = new ArrayList < > ();
categories.add("1");
categories.add("2");
categories.add("3");
categories.add("4");
ArrayAdapter < String > dataAdapter = new ArrayAdapter < > (this.getActivity(), android.R.layout.simple_spinner_item, categories);
// Drop down layout style - list view with radio button
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// attaching data adapter to spinner
vehicleSelector.setAdapter(dataAdapter);
vehicleSelector.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView << ? > adapterView, View view, int i, long l) {
String selectedItem = adapterView.getItemAtPosition(i).toString();
Log.i("Values", selectedItem);
switch (selectedItem) {
case "1":
c13.getEditText().setVisibility(View.VISIBLE);
Objects.requireNonNull(c14.getEditText()).setVisibility(View.VISIBLE);
break;
case "2":
Objects.requireNonNull(c13.getEditText()).setVisibility(View.VISIBLE);
Objects.requireNonNull(c14.getEditText()).setVisibility(View.VISIBLE);
Objects.requireNonNull(c15.getEditText()).setVisibility(View.VISIBLE);
Objects.requireNonNull(c16.getEditText()).setVisibility(View.VISIBLE);
break;
case "3":
Objects.requireNonNull(c13.getEditText()).setVisibility(View.VISIBLE);
Objects.requireNonNull(c14.getEditText()).setVisibility(View.VISIBLE);
Objects.requireNonNull(c15.getEditText()).setVisibility(View.VISIBLE);
Objects.requireNonNull(c16.getEditText()).setVisibility(View.VISIBLE);
Objects.requireNonNull(c17.getEditText()).setVisibility(View.VISIBLE);
Objects.requireNonNull(c18.getEditText()).setVisibility(View.VISIBLE);
break;
case "4":
Objects.requireNonNull(c13.getEditText()).setVisibility(View.VISIBLE);
Objects.requireNonNull(c14.getEditText()).setVisibility(View.VISIBLE);
Objects.requireNonNull(c15.getEditText()).setVisibility(View.VISIBLE);
Objects.requireNonNull(c16.getEditText()).setVisibility(View.VISIBLE);
Objects.requireNonNull(c17.getEditText()).setVisibility(View.VISIBLE);
Objects.requireNonNull(c18.getEditText()).setVisibility(View.VISIBLE);
Objects.requireNonNull(c19.getEditText()).setVisibility(View.VISIBLE);
Objects.requireNonNull(c20.getEditText()).setVisibility(View.VISIBLE);
break;
}
}
@Override
public void onNothingSelected(AdapterView << ? > adapterView) {
}
});
像这样初始化您的 TextInputEditText 变量:
TextInputEditText myEditText = findViewById(R.id.my_edit_text);
然后执行
myEditText.setVisibility(View.VISIBLE)
而不是
myTextInputLayout.getEditText().setVisibility(View.VISIBLE)
。 (这就是您正在做的,在 textInputLayout 变量上调用 getEditText()
)。
请注意,您需要为位于 textInputLayout 中的 TextInputEditText 指定 ID。