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

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

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

问题

我正在使用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) {

    }
});

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

英文:

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

 &lt;com.google.android.material.textfield.TextInputLayout
            android:id=&quot;@+id/cpdTypeLayout&quot;
            style=&quot;@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.ExposedDropdownMenu&quot;
            android:layout_width=&quot;match_parent&quot;
            android:layout_height=&quot;wrap_content&quot;
            android:layout_marginStart=&quot;16dp&quot;
            android:layout_marginEnd=&quot;16dp&quot;
            android:hint=&quot;Activity Type&quot;&gt;

            &lt;AutoCompleteTextView
                android:id=&quot;@+id/cpdType&quot;
                android:layout_width=&quot;match_parent&quot;
                android:layout_height=&quot;wrap_content&quot;
                android:inputType=&quot;none&quot; /&gt;
        &lt;/com.google.android.material.textfield.TextInputLayout&gt;

My AddAdactivity.java code is

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

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

    ArrayAdapter&lt;String&gt; adapterType = new ArrayAdapter&lt;&gt;(
            AddActivity.this,
            R.layout.dropdown_item,
            type
    );

    cpdType.setAdapter(adapterType);

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

        @Override
        public void onNothingSelected(AdapterView&lt;?&gt; parent) {

        }
    });

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

EDIT: have since tried this but with no luck.

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

        }

        @Override
        public void onNothingSelected(AdapterView&lt;?&gt; parent) {

        }
    });

答案1

得分: 16

获取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);
    }
});
英文:

To get the String you can just use:

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

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

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

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

发表评论

匿名网友

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

确定