TextInputLayout在程序中不显示

huangapple go评论58阅读模式
英文:

TextInputLayout is not showing programmatically

问题

以下是翻译后的内容:

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

// 下拉列表样式 - 带有单选按钮的列表视图
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

// 将数据适配器附加到下拉框
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) {

    }
});
英文:

So I am facing a problem where I have one spinner which has 4 options 1-4 and I have 4 TextInputLayout now what I want to achieve is to have the fragment start I want my textinputlayout to be hidden. When I click on the spinner and select the first option then only one TextInputLayout should be visible. When I select 2 options I need to 2 TextInputLayout to be visible and so on. Now the problem is my TextInputLayout is still not visible I have provided switch case in which of 1 is selected then make TextInputLayout visible but it's not working. Any help would be appreciated

XML FIle:

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;androidx.coordinatorlayout.widget.CoordinatorLayout
    xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
       android:layout_width=&quot;match_parent&quot;
      android:layout_height=&quot;match_parent&quot;
    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;&gt;
    &lt;ScrollView
       android:layout_width=&quot;match_parent&quot;
       android:layout_height=&quot;wrap_content&quot;
       android:layout_marginTop=&quot;60dp&quot;&gt;
        &lt;LinearLayout
    android:layout_width=&quot;match_parent&quot;
    android:layout_height=&quot;wrap_content&quot;
    android:orientation=&quot;vertical&quot;&gt;
            &lt;TextView
            android:layout_width=&quot;match_parent&quot;
            android:layout_height=&quot;wrap_content&quot;
            android:layout_marginTop=&quot;16dp&quot;
            android:text=&quot;Choose Number of vehicle and quantity&quot;
            android:layout_marginLeft=&quot;12dp&quot;/&gt;
            &lt;Spinner
            android:id=&quot;@+id/spinner&quot;
            android:layout_width=&quot;fill_parent&quot;
            android:layout_height=&quot;wrap_content&quot;
            android:layout_marginTop=&quot;16dp&quot;
            android:layout_marginLeft=&quot;12dp&quot;
            android:layout_marginRight=&quot;12dp&quot;
            android:background=&quot;@drawable/spinner_border&quot;/&gt;
            &lt;LinearLayout
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:orientation=&quot;horizontal&quot;&gt;
                &lt;com.google.android.material.textfield.TextInputLayout
        android:id=&quot;@+id/VehicleNoText1&quot;
        android:layout_width=&quot;0dp&quot;
        android:layout_height=&quot;match_parent&quot;
        android:layout_marginTop=&quot;16dp&quot;
        android:layout_marginLeft=&quot;12dp&quot;
        android:layout_marginRight=&quot;12dp&quot;
        android:layout_weight=&quot;0.5&quot;
        android:hint=&quot;Vehicle No&quot;
        android:visibility=&quot;invisible&quot;
        style=&quot;@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox&quot;&gt;
                    &lt;com.google.android.material.textfield.TextInputEditText
            android:layout_width=&quot;match_parent&quot;
            android:layout_height=&quot;wrap_content&quot;
            android:textSize=&quot;12sp&quot;
            android:imeOptions=&quot;actionNext&quot;
            android:inputType=&quot;text&quot;/&gt;
                &lt;/com.google.android.material.textfield.TextInputLayout&gt;
                &lt;com.google.android.material.textfield.TextInputLayout
           android:id=&quot;@+id/QuantityText1&quot;
           android:layout_width=&quot;0dp&quot;
           android:layout_height=&quot;match_parent&quot;
           android:hint=&quot;Quantity&quot;
           android:layout_marginTop=&quot;16dp&quot;
           android:layout_marginLeft=&quot;9dp&quot;
           android:layout_marginRight=&quot;12dp&quot;
           android:layout_weight=&quot;0.5&quot;
           android:visibility=&quot;invisible&quot;
           style=&quot;@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox&quot;&gt;
                    &lt;com.google.android.material.textfield.TextInputEditText
               android:layout_width=&quot;match_parent&quot;
               android:layout_height=&quot;wrap_content&quot;
               android:textSize=&quot;12sp&quot;
               android:inputType=&quot;text&quot;/&gt;
                &lt;/com.google.android.material.textfield.TextInputLayout&gt;
            &lt;/LinearLayout&gt;
            &lt;LinearLayout
            android:layout_width=&quot;match_parent&quot;
            android:layout_height=&quot;wrap_content&quot;
            android:orientation=&quot;horizontal&quot;&gt;
                &lt;com.google.android.material.textfield.TextInputLayout
                android:id=&quot;@+id/VehicleNoText2&quot;
                android:layout_width=&quot;0dp&quot;
                android:layout_height=&quot;match_parent&quot;
                android:hint=&quot;Vehicle No&quot;
                android:layout_marginTop=&quot;16dp&quot;
                android:layout_marginLeft=&quot;12dp&quot;
                android:layout_marginRight=&quot;12dp&quot;
                android:layout_weight=&quot;0.5&quot;
                android:visibility=&quot;invisible&quot;
                
          style=&quot;@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox&quot;&gt;
                    &lt;com.google.android.material.textfield.TextInputEditText
                    android:layout_width=&quot;match_parent&quot;
                    android:layout_height=&quot;wrap_content&quot;
                    android:textSize=&quot;12sp&quot;
                    android:imeOptions=&quot;actionNext&quot;
                    android:inputType=&quot;text&quot;/&gt;
                &lt;/com.google.android.material.textfield.TextInputLayout&gt;
                &lt;com.google.android.material.textfield.TextInputLayout
                android:id=&quot;@+id/QuantityText2&quot;
                android:layout_width=&quot;0dp&quot;
                android:layout_height=&quot;match_parent&quot;
                android:hint=&quot;Quantity&quot;
                android:layout_marginTop=&quot;16dp&quot;
                android:layout_marginLeft=&quot;9dp&quot;
                android:layout_marginRight=&quot;12dp&quot;
                android:layout_weight=&quot;0.5&quot;
                android:visibility=&quot;invisible&quot;
                
             
             
       style=&quot;@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox&quot;&gt;
                    &lt;com.google.android.material.textfield.TextInputEditText
                    android:layout_width=&quot;match_parent&quot;
                    android:layout_height=&quot;wrap_content&quot;
                    android:textSize=&quot;12sp&quot;
                    android:inputType=&quot;text&quot;/&gt;
                &lt;/com.google.android.material.textfield.TextInputLayout&gt;
            &lt;/LinearLayout&gt;
            &lt;LinearLayout
            android:layout_width=&quot;match_parent&quot;
            android:layout_height=&quot;wrap_content&quot;
            android:orientation=&quot;horizontal&quot;&gt;
                &lt;com.google.android.material.textfield.TextInputLayout
                android:id=&quot;@+id/VehicleNoText3&quot;
                android:layout_width=&quot;0dp&quot;
                android:layout_height=&quot;match_parent&quot;
                android:hint=&quot;Vehicle No&quot;
                android:layout_marginTop=&quot;16dp&quot;
                android:layout_marginLeft=&quot;12dp&quot;
                android:layout_marginRight=&quot;12dp&quot;
                android:layout_weight=&quot;0.5&quot;
                android:visibility=&quot;invisible&quot;
                
             
       style=&quot;@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox&quot;&gt;
                    &lt;com.google.android.material.textfield.TextInputEditText
                    android:layout_width=&quot;match_parent&quot;
                    android:layout_height=&quot;wrap_content&quot;
                    android:textSize=&quot;12sp&quot;
                    android:imeOptions=&quot;actionNext&quot;
                    android:inputType=&quot;text&quot;/&gt;
                &lt;/com.google.android.material.textfield.TextInputLayout&gt;
                &lt;com.google.android.material.textfield.TextInputLayout
                android:id=&quot;@+id/QuantityText3&quot;
                android:layout_width=&quot;0dp&quot;
                android:layout_height=&quot;match_parent&quot;
                android:hint=&quot;Quantity&quot;
                android:layout_marginTop=&quot;16dp&quot;
                android:layout_marginLeft=&quot;9dp&quot;
                android:layout_marginRight=&quot;12dp&quot;
                android:layout_weight=&quot;0.5&quot;
                android:visibility=&quot;invisible&quot;
                
            style=&quot;@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox&quot;&gt;
                    &lt;com.google.android.material.textfield.TextInputEditText
                    android:layout_width=&quot;match_parent&quot;
                    android:layout_height=&quot;wrap_content&quot;
                    android:textSize=&quot;12sp&quot;
                    android:inputType=&quot;text&quot;/&gt;
                &lt;/com.google.android.material.textfield.TextInputLayout&gt;
            &lt;/LinearLayout&gt;
            &lt;LinearLayout
            android:layout_width=&quot;match_parent&quot;
            android:layout_height=&quot;wrap_content&quot;
            android:orientation=&quot;horizontal&quot;&gt;
                &lt;com.google.android.material.textfield.TextInputLayout
                android:id=&quot;@+id/VehicleNoText4&quot;
                android:layout_width=&quot;0dp&quot;
                android:layout_height=&quot;match_parent&quot;
                android:hint=&quot;Vehicle No&quot;
                android:layout_marginTop=&quot;16dp&quot;
                android:layout_marginLeft=&quot;12dp&quot;
                android:layout_marginRight=&quot;12dp&quot;
                android:layout_weight=&quot;0.5&quot;
                android:visibility=&quot;invisible&quot;
                
      style=&quot;@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox&quot;&gt;
                    &lt;com.google.android.material.textfield.TextInputEditText
                    android:layout_width=&quot;match_parent&quot;
                    android:layout_height=&quot;wrap_content&quot;
                    android:textSize=&quot;12sp&quot;
                    android:imeOptions=&quot;actionNext&quot;
                    android:inputType=&quot;text&quot;/&gt;
                &lt;/com.google.android.material.textfield.TextInputLayout&gt;
                &lt;com.google.android.material.textfield.TextInputLayout
                android:id=&quot;@+id/QuantityText4&quot;
                android:layout_width=&quot;0dp&quot;
                android:layout_height=&quot;match_parent&quot;
                android:hint=&quot;Quantity&quot;
                android:layout_marginTop=&quot;16dp&quot;
                android:layout_marginLeft=&quot;9dp&quot;
                android:layout_marginRight=&quot;12dp&quot;
                android:layout_weight=&quot;0.5&quot;
                android:visibility=&quot;invisible&quot;
                
         style=&quot;@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox&quot;&gt;
                    &lt;com.google.android.material.textfield.TextInputEditText
                    android:layout_width=&quot;match_parent&quot;
                    android:layout_height=&quot;wrap_content&quot;
                    android:textSize=&quot;12sp&quot;
                    android:inputType=&quot;text&quot;/&gt;
                &lt;/com.google.android.material.textfield.TextInputLayout&gt;
            &lt;/LinearLayout&gt;
        &lt;/LinearLayout&gt;
    &lt;/ScrollView&gt;
&lt;/androidx.coordinatorlayout.widget.CoordinatorLayout&gt;

My Fragment class

List &lt; String &gt; categories = new ArrayList &lt; &gt; ();
categories.add(&quot;1&quot;);
categories.add(&quot;2&quot;);
categories.add(&quot;3&quot;);
categories.add(&quot;4&quot;);

ArrayAdapter &lt; String &gt; dataAdapter = new ArrayAdapter &lt; &gt; (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 &lt;&lt; ? &gt; adapterView, View view, int i, long l) {

        String selectedItem = adapterView.getItemAtPosition(i).toString();
        Log.i(&quot;Values&quot;, selectedItem);
        switch (selectedItem) {

            case &quot;1&quot;:
                c13.getEditText().setVisibility(View.VISIBLE);

                Objects.requireNonNull(c14.getEditText()).setVisibility(View.VISIBLE);
                break;

            case &quot;2&quot;:
                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 &quot;3&quot;:
                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 &quot;4&quot;:
                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 &lt;&lt; ? &gt; adapterView) {

    }
});

答案1

得分: 1

初始化您的TextInputEditText变量,可以这样进行:

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

然后执行:

myEditText.setVisibility(View.VISIBLE)

而不是:

myTextInputLayout.getEditText().setVisibility(View.VISIBLE)
(这是您正在做的,对一个textInputLayout变量调用getEditText())。

请注意,您需要指定位于textInputLayout内部的TextInputEditText的ID。

英文:

Initialize your TextInputEditText variable like this:

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

and then perform

myEditText.setVisibility(View.VISIBLE)

instead of

myTextInputLayout.getEditText().setVisibility(View.VISIBLE). ( This is what you are doing, calling getEditText() on a textInputLayout variable ).

Note that you need to specify the id to the TextInputEditText that lives inside the textInputLayout.

huangapple
  • 本文由 发表于 2020年9月9日 23:47:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/63815177.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定