Android 选择和取消选择微调器

Android 选择和取消选择微调器,android,android-layout,android-spinner,Android,Android Layout,Android Spinner,第一个图像显示未选中微调器时的微调器 选择微调器后,我希望更改其样式,使其看起来如下图所示 我想没有这样直接的方法可以做到以上一点。但你们可以采取布局,设置图像的背景。在该布局上获取imageview并单击该布局的侦听器更改箭头图像,同时在该布局上单击显示listview并双击使listview不可见 见下文: 布局(具有背景图像)->Imageview(箭头)->弹出列表视图->使其可见/不可见您需要在布局XML文件上使用android:spinnerSelector,请选中 ! 很久以前,

第一个图像显示未选中微调器时的微调器

选择微调器后,我希望更改其样式,使其看起来如下图所示


我想没有这样直接的方法可以做到以上一点。但你们可以采取布局,设置图像的背景。在该布局上获取imageview并单击该布局的侦听器更改箭头图像,同时在该布局上单击显示listview并双击使listview不可见

见下文:


布局(具有背景图像)->Imageview(箭头)->弹出列表视图->使其可见/不可见

您需要在布局XML文件上使用android:spinnerSelector,请选中
!

很久以前,您曾经能够指定android:spinnerSelector,但不幸的是,该属性已被删除,不再起作用

不幸的是,唯一的其他方法(AFAIK)是扩展
微调器
类。这是因为,为了以您想要的方式更改背景,您需要确定微调器是打开还是关闭的。没有内置的方法来检测这种情况,因此需要将其构建到自定义微调器类中

在自定义微调器类中,您将通过取消对话框或选择项目来检测何时按下并打开微调器,以及何时关闭对话框

我在写这个答案时提到了大量的问题(复制粘贴了很多代码,然后修改它以供您使用)。也请参考一下。我改变了答案,取消了监听器界面的使用,因为你不在乎什么时候点击,你只想改变背景。如果您希望在活动中获得该信息,请重新添加侦听器

首先,在新文件
CustomSpinner.java

public class CustomSpinner extends Spinner {

    private boolean mOpenInitiated = false;

    // the Spinner constructors
    // add all the others too, I'm just putting one here for simplicity sake
    public CustomSpinner(Context context){
        super(context);
        setBackgroundResource(R.drawable.CLOSE_SPINNER_DRAWABLE);
    }

    @Override
    public boolean performClick() {
        // register that the Spinner was opened so we have a status
        // indicator for the activity(which may lose focus for some other
        // reasons)
        mOpenInitiated = true;
        setBackgroundResource(R.drawable.OPENED_SPINNER_DRAWABLE);
        return super.performClick();
    }

    /**
     * Propagate the closed Spinner event to the listener from outside.
     */
    public void performClosedEvent() {
        mOpenInitiated = false;
        setBackgroundResource(R.drawable.CLOSED_SPINNER_DRAWABLE);
    }

    /**
     * A boolean flag indicating that the Spinner triggered an open event.
     * 
     * @return true for opened Spinner 
     */
    public boolean hasBeenOpened() {
        return mOpenInitiated;
    }

}
在“活动”中,创建自定义微调器(可能在onCreate中),并覆盖onWindowFocusChanged,以告知微调器取消,并在关闭事件打开时通知您

CustomSpinner mSpin;

@Override
public void onCreate(Bundle savedInstanceState){
    // Do all your initialization stuff
    mSpin = (CustomSpinner) findViewById(R.id.myCustomSpinner);

    // shouldn't need this if you are setting initial background in the constructors
    mSpin.setBackgroundResource(R.drawable.CLOSED_SPINNER_DRAWABLE); 
}

@Override
public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);
    // mSpin is our custom Spinner
    if (mSpin.hasBeenOpened() && hasFocus) {
        mSpin.performClosedEvent();
    }
}
这个微调器的xml资源如下所示(注意:我只向这个xml文件添加了几个参数,它肯定不完整,因为没有宽度、高度或对齐参数)



希望这有帮助,但很抱歉,事情太复杂了。我可能已经找到了一个更简单的解决方案,但我搜索了很多,没有找到任何正确的解决方案。

有没有参考链接我刚才把答案说得更清楚了。检查一下。我认为上述解释足以说明这一点。
<com.packagename.CustomSpinner 
    android:id="@+id/myCustomSpinner"
/>