【问题标题】:TextInputLayout is not showing programmaticallyTextInputLayout 未以编程方式显示
【发布时间】:2020-09-09 15:47:36
【问题描述】:

所以我面临一个问题,我有一个微调器,它有 4 个选项 1-4,我有 4 个 TextInputLayout 现在我想要实现的是让片段开始我希望我的 textinputlayout 被隐藏。当我单击微调器并选择第一个选项时,应该只有一个 TextInputLayout 可见。当我选择 2 个选项时,我需要 2 个 TextInputLayout 可见,依此类推。现在的问题是我的TextInputLayout 仍然不可见我提供了switch 的情况,其中选择了1 然后使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>

我的片段类

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) {

    }
});

【问题讨论】:

  • 请提供显示您尝试过的代码
  • @AbhishekChoudhary 这是我的代码
  • 我猜是不完整的,c13、c14、c15变量是什么?那些是 textInputLayouts 吗?
  • 这些是 TextInputLayout 我的开关工作正常,如果我尝试在我的开关盒中不可见,TextInputLayout 的边框只会隐藏。我只提供了出现问题的代码@AbhishekChoudhary
  • 你有多少个纺纱机?

标签: java android android-layout material-components-android android-textinputlayout


【解决方案1】:

像这样初始化你的 TextInputEditText 变量:

TextInputEditText myEditText = findViewById(R.id.my_edit_text);

然后执行

myEditText.setVisibility(View.VISIBLE)

而不是

myTextInputLayout.getEditText().setVisibility(View.VISIBLE)。 (这就是你正在做的,在 textInputLayout 变量上调用 getEditText())。

请注意,您需要为位于 textInputLayout 中的 TextInputEditText 指定 id。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-03
    • 2020-11-15
    • 2017-03-11
    • 2020-06-08
    • 1970-01-01
    相关资源
    最近更新 更多