Android - Spinner + setOnClickListener

前提是你 提交于 2019-12-05 05:53:32

I'm doing it like this:

public void addListenerOnSpinnerItemSelection() {
        mySpinner = (Spinner) findViewById(R.id.GPU_LAYOUT);  
        mySpinner.setOnItemSelectedListener(new myOnItemSelectedListener());
    }

public class myOnItemSelectedListener implements OnItemSelectedListener {       
@Override
public void onItemSelected(AdapterView<?> parent, View arg1, int pos,long arg3)     {           

int position=Arrays.asList(getResources().getStringArray(R.array.GPU_LAYOUT)).indexOf(fecha);           }

@Override
public void onNothingSelected(AdapterView<?> arg0) {        }       
}

Also I suggest to get the position of the group you're using and then handle it.

You can do it with setOnTouchListener in that way:

spinner.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                Toast.makeText(getBaseContext(), "CLICK", Toast.LENGTH_SHORT).show();
                return false;
            }
        });
min_max.setOnItemClickListener(new OnItemClickListener()
{
    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {

    //whatever your logic is put it here 
 }
}); 

you can't use setOnClickListener directly on a Spinner. If you want to do it here is a trick.

 @Override
public boolean onTouch(View view, MotionEvent motionEvent) {
    switch (view.getId()) {
        case R.id.sp_category:
            if (motionEvent.getAction() == MotionEvent.ACTION_UP)//replacing with onclick
            {
                //Perform any action
            }
            break;
    }
    return true;
}

In this code MotionEvent.ACTION_UP will make this touch act like onclick. Hope it will help you!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!