选择另一个微调器中的选项后隐藏编辑文本或微调器

时间:2019-03-20 12:45:24

标签: java android

基本上,我想在选择另一个微调器中的特定选项时仅显示微调器或Edittext。 有一个Spinner(@ + id / selection),有两个选项,分别是标准选项和自定义选项。当用户选择标准时,Spinner(@ + id / duration)在其下方应可见。就像当用户选择custom时一样,下一步应显示Edittext(@ + id / custom_duration)。在第一个微调器中选择一个选项时,总是只有一个选项可见(@ + id / selection)。经过研究,我接近了解决方案。但是我不确定如何进一步进行。请帮忙。 这是我当前的代码:

XML文件

  <Spinner
                android:id="@+id/selection"
                android:layout_width="0dp"
                android:layout_height="45dp"
                android:layout_marginStart="25dp"
                android:layout_marginTop="8dp"
                android:layout_marginEnd="32dp"
                android:entries="@array/selection"
                android:minWidth="140dp"
                android:padding="10dp"
                android:spinnerMode="dialog"

                />


            <Spinner
                android:id="@+id/duration"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginStart="32dp"
                android:layout_marginTop="8dp"
                android:layout_marginEnd="32dp"
                android:entries="@array/session_duration_labels"
                android:minWidth="140dp"
                android:padding="10dp"
                android:spinnerMode="dialog"/>

            <EditText
                android:id="@+id/custom_duration"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginStart="32dp"
                android:layout_marginTop="8dp"
                android:layout_marginEnd="32dp"
                android:minEms="6"
                app:layout_row="0"/>

阵列列表

<string-array name="selection">
    <item>Set the Type</item>
    <item>Standard</item>
    <item>Custom</item>
    </string-array>

sessionselection.java

public class StartSessionActivity extends AppCompatActivity implements View.OnClickListener{

    private Spinner spinner_session_selection;

    private Spinner spinner_session_duration;

    private EditText edit_custom_duration;

public void onCreate(@Nullable Bundle savedInstanceState) {

     spinner_session_selection = (Spinner) findViewById(R.id.selection);
    spinner_session_duration = (Spinner) findViewById(R.id.duration);
    edit_custom_duration = (EditText) findViewById(R.id.custom_duration);
        setupSpinners();

}
 void setupSpinners(){
        spinner_session_selection.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                if (position == 1) {
                    edit_custom_duration.setVisibility(View.GONE);
                } else {
                    edit_custom_duration.setVisibility(View.VISIBLE);
                }


            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }

        });

        spinner_session_selection.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                // put your code here for weight spinner
                if (position == 2) {
                    spinner_session_duration.setVisibility(View.GONE);
                } else {
                    spinner_session_duration.setVisibility(View.VISIBLE);
                }

            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }

}

1 个答案:

答案 0 :(得分:0)

您无需为同一微调器设置两次监听器。您可以在同一侦听器中检查两个不同的条件,仅保留一个侦听器,然后删除另一个侦听器。如下更改代码。

 spinner_session_selection.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
     @Override
     public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

            if (position == 1) {
                edit_custom_duration.setVisibility(View.GONE);
            } else {
                edit_custom_duration.setVisibility(View.VISIBLE);
            }

            if (position == 2) {
                spinner_session_duration.setVisibility(View.GONE);
            } else {
                spinner_session_duration.setVisibility(View.VISIBLE);
            }


        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }

    });