Android - How to tell is someone has pressed a SPINNER but not changed the visible item in it
I have a spinner, which mostly works. If a user selects one of the items in it, the 'onItemSelected' routine catches it just fine.
But if a user clicks the same spinner, but does not change from the already visible item that it's currently displaying, the 'onItemSelected' routine just ignores it, and the logs show:-
WARN/InputManagerService(577): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@437948b0
I there anyway to capture someone doing this? The idea is that my spinner contains a list of names, and when a user selects one from th开发者_JS百科e spinner, it gets added to a listview.
I could just add another button to get the name from the spinner, but, screen-space is already lacking and I'd rather not add anymore content.
This problem could be solved with overriding Spinner.onClick()
, but this is buggy from android 3.0 and above (Spinner.onClick()
is called on 2.2 device, not called on 3.0 device).
So the best solution is the AlertDialog.Builder
as @adamp said.
AlertDialog.Builder builder = new Builder(this);
// maybe get from resources
CharSequence titles[] = getResources().getTextArray(R.array.titles);
builder.setItems(titles, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// add your code here
}
});
builder.create().show();
It sounds like you don't actually want a Spinner, you're just using it for its popup dialog. You can create your own simple popup dialog like Spinner uses (as the result of an 'Add' button click in your UI perhaps?) by using AlertDialog.Builder
. Spinner uses setSingleChoiceItems
followed by show
on an AlertDialog.Builder
to present its choices to the user.
Never used the spinner but wouldn't it be possible to reset the spinners position in the onItemSelected routine? To some kind of null value that says "Please choose" or something.
Have you looked at setOnItemClickListener? If you implement your own AdapterView.OnItemClickListener
and pass it in as the argument to mySpinner.setOnItemClickListener
method you'll be able to get ahold of the position of the selected item inside the onItemClick
method
精彩评论