Spinner 点击无响应的解决

点击事件无响应前代码:

 mSortSpinner = new Spinner(getContext());
        // Create an ArrayAdapter using the string array and a default spinner layout
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getContext(),
                R.array.file_manage_sort, android.R.layout.simple_spinner_item);
        // Specify the layout to use when the list of choices appears
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        // Apply the adapter to the spinner
        mSortSpinner.setAdapter(adapter);
        mSortSpinner.setOnItemSelectedListener(this);

找到问题是在第一句语句,Spinner无需要新建一个对象

mSortSpinner = new Spinner(getContext());

控件Spinner的实现:

定义:Spinners提供了一种从集合中选择一个值的快速方法。在默认状态下,Spinners显示其当前选定的值。触摸Spinners会显示一个包含所有其他可用值的下拉菜单,用户可以从中选择一个新值。

1:Spinner控件的所处布局定义

可以使用spinner对象将spinner添加到布局中。
通常应该在带有<Spinner>元素的XML布局中做法如下:

<Spinner
    android:id="@+id/planets_spinner"
    android:layout_width="match_parent"
     android:spinnerMode="dropdown"
    android:layout_height="wrap_content" />

spinnerMode为dropdown时,Spinner为下拉式;spinnerMode为dialog时,Spinner为弹窗式,

要使用选项列表填充微调器,您需要在“Activity”或“fragment”源代码中指定SpinnerAdapter。

2:Spinner中的填充数据数据

为Spinner提供的选项可以来自任何来源,但必须通过SpinnerAdapter提供,例如ArrayAdapter(如果这些选项在数组中可用)或CursorAdapter(如果这些选择在数据库查询中可用)。
例如,如果Spinner的可用选项是预先确定的,则可以为它们提供在字符串资源文件中定义的字符串数组:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="planets_array">
        <item>Mercury</item>
        <item>Venus</item>
        <item>Earth</item>
        <item>Mars</item>
        <item>Jupiter</item>
        <item>Saturn</item>
        <item>Uranus</item>
        <item>Neptune</item>
    </string-array>
</resources>

对于上述的数组,您可以在“Activty”或“fragment”中使用以下代码,使用ArrayAdapter实例为Spinner提供数组:

3: 在Activity或者fragment使用Spinner的步骤如下:

Spinner spinner = (Spinner) findViewById(R.id.spinner);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
        R.array.planets_array, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinner.setAdapter(adapter);

① createFromResource()方法允许您从字符串数组中创建一个ArrayAdapter。此方法的第三个参数是一个布局资源,用于定义所选选项在Spinner控件中的显示方式。simple_spinner_item布局由平台提供,是您应该使用的默认布局,除非您想为Spinner的外观定义自己的布局。想要自定义布局就是替换该变量。

② 调用setDropDownViewResource(int)来指定适配器应用于显示Spinner选项列表的布局(simple_spinner_dropdown_item是平台定义的另一个标准布局)。

③ 调用setAdapter()将适配器应用于Spinner。

4、响应用户选择

当用户从下拉列表中选择一个项目时,Spinner对象将接收一个项目上选择的事件。
注意 : 要定义微调器的选择事件处理程序,请实现AdapterView.OnItemSelectedListener接口和相应的onItemSelected()回调方法。例如,以下是“Activity”中接口的实现:

public class SpinnerActivity extends Activity implements OnItemSelectedListener {
    
    
    ...

    public void onItemSelected(AdapterView<?> parent, View view,
            int pos, long id) {
    
    
        // An item was selected. You can retrieve the selected item using
        // parent.getItemAtPosition(pos)
    }

    public void onNothingSelected(AdapterView<?> parent) {
    
    
        // Another interface callback
    }
}

AdapterView.OnItemSelectedListener需要onItemSelect()和onNothingSelected()回调方法,通过调用

5、setOnItemSelectedListener()来指定接口实现

Spinner spinner = (Spinner) findViewById(R.id.spinner);
spinner.setOnItemSelectedListener(this);

如果使用Activity或Fragment实现AdapterView.OnItemSelectedListener接口(如上示例),则可以将其作为接口实例传递。

Android中官网说明连接:
https://developer.android.google.cn/develop/ui/views/components/spinner?hl=en

猜你喜欢

转载自blog.csdn.net/qq_51108920/article/details/129707821