Spinner Items Not Clickable

2019-09-14 20:56发布

I have a spinner that i fill with 4 elements. During startup onItemSelected method gets executed correctly and the toast message is displayed. However when i open the spinner and try to click any item, no event is called and the spinner popup will not close unless i press the spinner arrow. In other words I can not interact with spinner items.

Below is the activity code for the spinner

 <Spinner
        android:id="@+id/spinner"
        android:popupBackground="@color/red"
        android:clickable="true"
        style="@style/spinner250"/>

Below is the style for the spinner

    <style name="spinner250">
    <item name="android:layout_width"> 250dp </item>
    <item name="android:layout_height"> match_parent </item>
    <item name="android:textColor"> @color/blue </item>
    <item name="android:textSize"> 12sp </item>
    </style>

And this is my java code for interacting with the spinner package com.example.android.gjobat;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;
import org.w3c.dom.Text;
import static android.R.attr.country;
import static android.widget.ArrayAdapter.createFromResource;

public class MainActivity extends AppCompatActivity implements     OnItemSelectedListener {

public Spinner spinner;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final String[] items = {"Item 1", "Item 2", "Item 3", "Item 4"};

    // Create an ArrayAdapter using the string array and a default spinner layout
    ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, items);
    Spinner spinner = (Spinner) findViewById(R.id.spinner);
    spinner.setAdapter(adapter);
    spinner.setOnItemSelectedListener(this);

}

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
    {
        String item = parent.getItemAtPosition(position).toString();
        // User selected item
        Toast.makeText(getApplicationContext(), item + " selected!", Toast.LENGTH_SHORT).show();
    }

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

    }
}

4条回答
虎瘦雄心在
2楼-- · 2019-09-14 21:25

You can use onItemClickListener like below

spinner.setOnItemClickListener(new AdapterView.OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

Toast.makeText(getApplicationContext(), "Item number: " + position, Toast.LENGTH_LONG).show();

}

});
查看更多
啃猪蹄的小仙女
3楼-- · 2019-09-14 21:27

After spending entirely one day with this i figured out that the problem was caused by

 <item name="android:inputType">textCapCharacters</item>

that was in my styles.xml file. I have no idea why this line of code caused the error but at least it works now after i removed that line. Below a full copy of my styles.xml file prior to removing the faulty line

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:textColor">@color/white</item>
<item name="android:inputType">textCapCharacters</item>
<item name="colorControlActivated">@color/white</item>
<item name="colorControlHighlight">@color/white</item>
<item name="colorControlNormal">@color/white</item>
</style>

I hope this helps someone in the future

查看更多
该账号已被封号
4楼-- · 2019-09-14 21:28

Try removing the xml attribute android:clickable="true" from the Spinner widget. It could be that the entire spinner is registering the click event rather than the individual spinner items.

Possible duplicate of Spinner functionality issues in Android 6.0 Marshmallow

查看更多
霸刀☆藐视天下
5楼-- · 2019-09-14 21:30

SOMETIMES THE ISSUE MAY ALSO BE BECAUSE YOU HAVE PLACED YOUR SPINNER OUTSIDE THE LAYOUT YOU ARE CALLING . THIS WILL NOT SHOW ANY ERROR BUT YOU WILL NOT BE ABLE TO INTERACT WITH THE SPINNER VIEW

查看更多
登录 后发表回答