如果你没有setCancelable方法也不setCanceledOnTouchOutside使用此功能:
public static void clickOutSideSpinner(View view)
{
// Configure touch listener for all views except edittext,Button and Spinner
if (!(view instanceof EditText)
&&!(view instanceof Button)
&&!(view instanceof Spinner))
{
view.setOnTouchListener(new OnTouchListener()
{
@Override
public boolean onTouch(View v, MotionEvent event)
{
//here you close your dialog spinner
return false;
}
});
}
//runs through all the children views .
if (view instanceof ViewGroup)
{
for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++)
{
View innerView = ((ViewGroup) view).getChildAt(i);
closeSlidingDrawerOnTouch(innerView);
}
}
}
在的onCreate调用你的函数,并通过布局:
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textviw1 = (TextView) findViewById(R.id.textviw1);
Spinner spinner1 = (Spinner) findViewById(R.id.spinner1);
Button button1 = (Button) findViewById(R.id.button1);
//change to other layout if you use other
LinearLayout layout1 = (LinearLayout) findViewById(R.id.layout1);
clickOutSideSpinner(layout1);
}
这是微调。在Spinner对象中,没有setCancelable(boolean cancelable)方法。 http://developer.android.com/reference/android/widget/Spinner.html – Andy 2014-11-25 03:50:44