Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
105 views
in Technique[技术] by (71.8m points)

android - Add Hide/Show EditText

Let me try to explain the title. I have a manufacturer TextView that is populated from MYSQL. When a manufacturer is selected it populates the Model TextView.

Now I want to add OTHER in each model, and when it is selected an EditText appears. Something like this:

 -FORD
    ---Mustang
    ---Escape
    ---Other
 -BMW
    ---X5
    ---Z4
    ---Other

Now whenever Other is selected, the EditText appears. If not, then it remains hidden.

The only way I got it working is by creating a field in mysql for each model. I am hoping there is a better approach.

Here is what I have done.

enter code here

        <EditText
            android:id="@+id/fordTextView"
            android:layout_width="0dp"
            android:layout_height="130px"
            android:layout_marginStart="16dp"
            android:visibility="gone"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="16dp"
            android:background="@drawable/custom_progress_bar_horizontal"
            android:inputType="textPersonName"
            android:paddingStart="@dimen/space_12"
            android:hint="@string/item_entry__model"
            android:paddingLeft="@dimen/space_12"
            android:paddingTop="8dp"
            android:paddingEnd="@dimen/space_12"
            android:paddingRight="@dimen/space_12"
            android:paddingBottom="8dp"
            android:textAlignment="viewStart"
            android:textColor="@color/text__primary"
            android:textSize="14sp"
            app:font='@{"normal"}'
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/plateNumTextView" />
       

           <EditText
            android:id="@+id/bmwTextView"
            android:layout_width="0dp"
            android:layout_height="130px"
            android:layout_marginStart="16dp"
            android:visibility="gone"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="16dp"
            android:background="@drawable/custom_progress_bar_horizontal"
            android:inputType="textPersonName"
            android:paddingStart="@dimen/space_12"
            android:hint="@string/item_entry__model"
            android:paddingLeft="@dimen/space_12"
            android:paddingTop="8dp"
            android:paddingEnd="@dimen/space_12"
            android:paddingRight="@dimen/space_12"
            android:paddingBottom="8dp"
            android:textAlignment="viewStart"
            android:textColor="@color/text__primary"
            android:textSize="14sp"
            app:font='@{"normal"}'
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/fordTextView" />

Here is is the java

        if (itemViewModel.holder.model_id.equals(Constants.MODEL_FORD_OTHER)){
            binding.get().fordTextView.setVisibility(View.VISIBLE);
            binding.get().bmwTextView.setVisibility(View.GONE);
        }
        if (!itemViewModel.holder.model_id.equals(Constants.MODEL_FORD_OTHER)){
            binding.get().fordTextView.setVisibility(View.GONE);
            binding.get().fordTextView.setText("");
        }

        if (itemViewModel.holder.model_id.equals(Constants.MODEL_BMW_OTHER)){
            binding.get().bmwTextView.setVisibility(View.VISIBLE);
            binding.get().fordTextView.setVisibility(View.GONE);
        }
        if (!itemViewModel.holder.model_id.equals(Constants.MODEL_BMW_OTHER)){
            binding.get().bmwTextView.setVisibility(View.GONE);
            binding.get().bmwTextView.setText("");
        }

Is there a way of doing this by using only one EditText?

question from:https://stackoverflow.com/questions/65934211/add-hide-show-edittext

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can achieve this using single EditText, when ever click is registered for particular model Other button, set the model_id of that model as tag to the EditText. And while reading input from EditText, always check for tag to get the model_id

binding.get().modelTextView.setTag(""+itemViewModel.holder.model_id);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...