如何在Java中捕获材料下拉菜单选择的值?

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

How do I capture the value of a Material drop down menu selection in Java?

问题

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

我想要将用户从下拉列表中选择的内容保存到一个字符串中,以便我可以将其保存到Firebase,但是当我尝试记录结果时,它说选择为空。

有人知道我如何捕获用户从这种类型的下拉菜单中选择的内容吗?我之前有一个Spinner,它起作用,但是似乎不以相同的方式工作。

我的XML代码如下:

  1. <com.google.android.material.textfield.TextInputLayout
  2. android:id="@+id/cpdTypeLayout"
  3. style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.ExposedDropdownMenu"
  4. android:layout_width="match_parent"
  5. android:layout_height="wrap_content"
  6. android:layout_marginStart="16dp"
  7. android:layout_marginEnd="16dp"
  8. android:hint="Activity Type">
  9. <AutoCompleteTextView
  10. android:id="@+id/cpdType"
  11. android:layout_width="match_parent"
  12. android:layout_height="wrap_content"
  13. android:inputType="none" />
  14. </com.google.android.material.textfield.TextInputLayout>

我的AddAdactivity.java代码如下:

  1. // CPD TYPE DROPDOWN MENU
  2. cpdTypeLayout = findViewById(R.id.cpdTypeLayout);
  3. cpdType = findViewById(R.id.cpdType);
  4. String[] type = new String[]{
  5. "Formal Education Completed", "Other Completed", "Professional Activities", "Self-Directed Learning", "Work-Based Learning"
  6. };
  7. ArrayAdapter<String> adapterType = new ArrayAdapter<>(
  8. AddActivity.this,
  9. R.layout.dropdown_item,
  10. type
  11. );
  12. cpdType.setAdapter(adapterType);
  13. cpdType.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
  14. @Override
  15. public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
  16. selectedTypeResult = parent.getItemAtPosition(position).toString();
  17. }
  18. @Override
  19. public void onNothingSelected(AdapterView<?> parent) {
  20. }
  21. });
  22. Log.d("TAG", "You have selected " + selectedTypeResult);

编辑:之后尝试了以下方法,但没有成功。

  1. ((AutoCompleteTextView) cpdTypeLayout.getEditText()).setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
  2. @Override
  3. public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
  4. adapterType.getItem(position);
  5. String typeItem = ((AutoCompleteTextView) cpdTypeLayout.getEditText()).getText().toString();
  6. Log.d("TAG", "selected type is:" + typeItem);
  7. }
  8. @Override
  9. public void onNothingSelected(AdapterView<?> parent) {
  10. }
  11. });

请注意,这些代码块是用于处理下拉列表选择的,您可以根据需要进行调整。

英文:

I am using Material to design a form with drop down lists in android studio using Java.

I want to save the selection the user makes from the drop down list to a string so I can save this to Firebase, but when I try and log the result, it says the selection is null.

Does anyone know how I can capture what the user selects from this type of drop down menu? I did previously have a Spinner which worked but this doesn't seem to work in the same manner.

My XML code is as follows

  1. &lt;com.google.android.material.textfield.TextInputLayout
  2. android:id=&quot;@+id/cpdTypeLayout&quot;
  3. style=&quot;@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.ExposedDropdownMenu&quot;
  4. android:layout_width=&quot;match_parent&quot;
  5. android:layout_height=&quot;wrap_content&quot;
  6. android:layout_marginStart=&quot;16dp&quot;
  7. android:layout_marginEnd=&quot;16dp&quot;
  8. android:hint=&quot;Activity Type&quot;&gt;
  9. &lt;AutoCompleteTextView
  10. android:id=&quot;@+id/cpdType&quot;
  11. android:layout_width=&quot;match_parent&quot;
  12. android:layout_height=&quot;wrap_content&quot;
  13. android:inputType=&quot;none&quot; /&gt;
  14. &lt;/com.google.android.material.textfield.TextInputLayout&gt;

My AddAdactivity.java code is

  1. //CPD TYPE DROPDOWN MENU
  2. cpdTypeLayout = findViewById(R.id.cpdTypeLayout);
  3. cpdType = findViewById(R.id.cpdType);
  4. String[] type = new String[]{
  5. &quot;Formal Education Completed&quot;, &quot;Other Completed&quot;, &quot;Professional Activities&quot;, &quot;Self-Directed Learning&quot;, &quot;Work-Based Learning&quot;
  6. };
  7. ArrayAdapter&lt;String&gt; adapterType = new ArrayAdapter&lt;&gt;(
  8. AddActivity.this,
  9. R.layout.dropdown_item,
  10. type
  11. );
  12. cpdType.setAdapter(adapterType);
  13. cpdType.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
  14. @Override
  15. public void onItemSelected(AdapterView&lt;?&gt; parent, View view, int position, long id) {
  16. selectedTypeResult = parent.getItemAtPosition(position).toString();
  17. }
  18. @Override
  19. public void onNothingSelected(AdapterView&lt;?&gt; parent) {
  20. }
  21. });
  22. Log.d(&quot;TAG&quot;, &quot;You have selected &quot; + selectedTypeResult);

EDIT: have since tried this but with no luck.

  1. ((AutoCompleteTextView)cpdTypeLayout.getEditText()).setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
  2. @Override
  3. public void onItemSelected(AdapterView&lt;?&gt; parent, View view, int position, long id) {
  4. adapterType.getItem(position);
  5. String typeItem = ((AutoCompleteTextView)cpdTypeLayout.getEditText()).getText().toString();
  6. Log.d(&quot;TAG&quot;, &quot;selected type is:&quot; + typeItem);
  7. }
  8. @Override
  9. public void onNothingSelected(AdapterView&lt;?&gt; parent) {
  10. }
  11. });

答案1

得分: 16

获取String只需使用以下代码:

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

如果你想使用监听器,可以使用类似以下代码:

  1. ((AutoCompleteTextView) textInputLayout.getEditText()).setOnItemClickListener(new AdapterView.OnItemClickListener() {
  2. @Override
  3. public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
  4. String selectedValue = arrayAdapter.getItem(position);
  5. }
  6. });
英文:

To get the String you can just use:

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

Otherwise if you want to use a listener you can use something like:

  1. ((AutoCompleteTextView)textInputLayout.getEditText()).setOnItemClickListener(new AdapterView.OnItemClickListener() {
  2. @Override
  3. public void onItemClick(AdapterView&lt;?&gt; adapterView, View view, int position, long id) {
  4. String selectedValue = arrayAdapter.getItem(position);
  5. }
  6. });

huangapple
  • 本文由 发表于 2020年7月23日 00:52:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/63039340.html
  • android
  • android-spinner
  • android-textinputlayout
  • java
  • material-components-android
匿名

发表评论

匿名网友

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

确定