如何捕获Java中“材质”下拉菜单选择的值?

时间:2020-07-22 16:52:00

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

我正在使用Material在Android Studio中使用Java设计带有下拉列表的表单。

我想将用户从下拉列表中所做的选择保存到字符串中,以便将其保存到Firebase,但是当我尝试记录结果时,它表示选择为空。

有人知道我如何捕获用户从此类下拉菜单中选择的内容吗?我以前确实有一个Spinner可以工作,但是似乎不能以相同的方式工作。

我的XML代码如下

 <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/cpdTypeLayout"
            style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.ExposedDropdownMenu"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:layout_marginEnd="16dp"
            android:hint="Activity Type">

            <AutoCompleteTextView
                android:id="@+id/cpdType"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:inputType="none" />
        </com.google.android.material.textfield.TextInputLayout>

我的AddAdactivity.java代码是

//CPD TYPE DROPDOWN MENU
    cpdTypeLayout = findViewById(R.id.cpdTypeLayout);
    cpdType = findViewById(R.id.cpdType);

    String[] type = new String[]{
            "Formal Education Completed", "Other Completed", "Professional Activities", "Self-Directed Learning", "Work-Based Learning"
    };

    ArrayAdapter<String> adapterType = new ArrayAdapter<>(
            AddActivity.this,
            R.layout.dropdown_item,
            type
    );

    cpdType.setAdapter(adapterType);

    cpdType.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            selectedTypeResult = parent.getItemAtPosition(position).toString();
        }

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

        }
    });

    Log.d("TAG", "You have selected " + selectedTypeResult);

编辑:此后一直尝试这样做,但没有运气。

((AutoCompleteTextView)cpdTypeLayout.getEditText()).setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            adapterType.getItem(position);
            String typeItem = ((AutoCompleteTextView)cpdTypeLayout.getEditText()).getText().toString();
            Log.d("TAG", "selected type is:" + typeItem);

        }

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

        }
    });

1 个答案:

答案 0 :(得分:3)

要获取String,您可以使用:

String selectedValue =((AutoCompleteTextView)textInputLayout.getEditText()).getText();

否则,如果要使用侦听器,则可以使用类似以下内容的

    ((AutoCompleteTextView)textInputLayout.getEditText()).setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
            String selectedValue = arrayAdapter.getItem(position);
        }
    });