Is there a way to use setOnClickListener with an Android Spinner?

后端 未结 2 1711
孤城傲影
孤城傲影 2020-12-16 23:58

The java.lang.RuntimeException is \"Don\'t call setOnClickListener for an AdapterView. You probably want setOnItemClickListener instead,\" but that is not correct. I am usin

相关标签:
2条回答
  • 2020-12-17 00:35

    You can replicate the an onclick event using ontouch events

        this.spinner=(Spinner)findViewById(R.id.spinner);
        this.spinner.setClickable(false);
        this.spinner.setOnTouchListener(new View.OnTouchListener() {
    
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                Log.v(TAG, "spinner touch");
    
                //replicating a click
                if(event.getAction() == MotionEvent.ACTION_UP){
                    v.playSoundEffect(android.view.SoundEffectConstants.CLICK);
                }
                return true;
            }
        });
    
    0 讨论(0)
  • 2020-12-17 00:41

    You will have to set the Click listener on the underlying view (normally a TextView with id: android.R.id.text1) of the spinner. To do so:

    • Create a custom Spinner
    • In the constructor (with attributes) create the spinner by supplying the layout android.R.layout.simple_spinner_item
    • Do a findViewById(android.R.id.text1) to get the TextView
    • Now set the onClickListener to the TextView
    0 讨论(0)
提交回复
热议问题