本文介绍了如何为微调器捕获Android中的onClick事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在用户选择微调器值时捕获onClick事件.
I want to capture the onClick event when the user selects a spinner value.
我尝试实现OnClickListener
并使用以下代码:
I've tried implementing OnClickListener
and using the following code:
@Override
public void onClick(final View view) {
if (view == countrySpinner) {
Toast.makeText(this, "Override OK!", 3);
}
}
并绑定:
countrySpinner.setOnClickListener(this);
这可以编译,但是我得到一个RuntimeException
,建议我对AdapterView
使用OnItemClickListener
而不是OnClickListener
.
This compiles, but I get a RuntimeException
advising me to use OnItemClickListener
rather than OnClickListener
for an AdapterView
.
如何捕获该onClick事件?
How can I capture that onClick event?
推荐答案
而不是设置微调器的OnClickListener,请尝试设置OnTouchListener和OnKeyListener.
Instead of setting the spinner's OnClickListener,try setting OnTouchListener and OnKeyListener.
spinner.setOnTouchListener(spinnerOnTouch);
spinner.setOnKeyListener(spinnerOnKey);
和听众:
private View.OnTouchListener spinnerOnTouch = new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
//Your code
}
return false;
}
};
private static View.OnKeyListener spinnerOnKey = new View.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
//your code
return true;
} else {
return false;
}
}
};
这篇关于如何为微调器捕获Android中的onClick事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!