java 有没有办法将 setOnClickListener 与 Android Spinner 一起使用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3523078/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 02:14:36  来源:igfitidea点击:

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

javaandroidspinner

提问by Hymannad

The java.lang.RuntimeException is "Don't call setOnClickListener for an AdapterView. You probably want setOnItemClickListener instead," but that is not correct. I am using setOnItemClickListener to do some stuff based on the new selection, but I also need to do some stuff before the user changes the selection. Specifically, I am collecting data for each selection that needs to be saved to a file before moving to another selection, since the other selection is associated with different set of data. Is there a way to use setOnClickListener with an Android Spinner?

java.lang.RuntimeException 是“不要为 AdapterView 调用 setOnClickListener。您可能需要 setOnItemClickListener”,但这是不正确的。我正在使用 setOnItemClickListener 根据新选择做一些事情,但我还需要在用户更改选择之前做一些事情。具体来说,我正在为需要在移动到另一个选择之前保存到文件的每个选择收集数据,因为另一个选择与不同的数据集相关联。有没有办法将 setOnClickListener 与 Android Spinner 一起使用?

spinner.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        // Do some stuff before the user changes the selection
        ...

spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> parent,
            View view, int pos, long id) {
            // Do some stuff based onItemSelected
            ...

采纳答案by Sameer Segal

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:

您必须在微调器的底层视图(通常是一个 ID 为 android.R.id.text1 的 TextView)上设置 Click 侦听器。这样做:

  • 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
  • 创建自定义微调器
  • 在构造函数(带有属性)中,通过提供布局 android.R.layout.simple_spinner_item 创建微调器
  • 做一个 findViewById(android.R.id.text1) 来获取 TextView
  • 现在将 onClickListener 设置为 TextView

回答by dcart1234

You can replicate the an onclick event using ontouch events

您可以使用 ontouch 事件复制 onclick 事件

    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;
        }
    });