android spinner获取焦点,android - Spinner - focus to first item - Stack Overflow

You can make the Spinner do what you want by extending it and overriding the two methods that are responsible for setup/showing the list of values:

public class CustomSpinnerSelection extends Spinner {

private boolean mToggleFlag = true;

public CustomSpinnerSelection(Context context, AttributeSet attrs,

int defStyle, int mode) {

super(context, attrs, defStyle, mode);

}

public CustomSpinnerSelection(Context context, AttributeSet attrs,

int defStyle) {

super(context, attrs, defStyle);

}

public CustomSpinnerSelection(Context context, AttributeSet attrs) {

super(context, attrs);

}

public CustomSpinnerSelection(Context context, int mode) {

super(context, mode);

}

public CustomSpinnerSelection(Context context) {

super(context);

}

@Override

public int getSelectedItemPosition() {

// this toggle is required because this method will get called in other

// places too, the most important being called for the

// OnItemSelectedListener

if (!mToggleFlag) {

return 0; // get us to the first element

}

return super.getSelectedItemPosition();

}

@Override

public boolean performClick() {

// this method shows the list of elements from which to select one.

// we have to make the getSelectedItemPosition to return 0 so you can

// fool the Spinner and let it think that the selected item is the first

// element

mToggleFlag = false;

boolean result = super.performClick();

mToggleFlag = true;

return result;

}

}

It should work just fine for what you want to do.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值