2012-11-08 44 views
7

我正在使用加载微调器动画和一组项目。如果你点击它外面,它应该消失。有人知道怎么做这个吗?在Android中单击它之外时隐藏加载微调器

Spinner

我已经试过这一点。它与EditText一起工作。但它不工作的提前Spinner

@Override 
public boolean dispatchTouchEvent(MotionEvent event) { 
    View view = getCurrentFocus(); 
    boolean ret = super.dispatchTouchEvent(event); 

    if (view instanceof EditText||view instanceof Spinner) { 
     View w = getCurrentFocus(); 
     int scrcoords[] = new int[2]; 
     w.getLocationOnScreen(scrcoords); 
     float x = event.getRawX() + w.getLeft() - scrcoords[0]; 
     float y = event.getRawY() + w.getTop() - scrcoords[1]; 

     if (event.getAction() == MotionEvent.ACTION_UP 
&& (x < w.getLeft() || x >= w.getRight() 
|| y < w.getTop() || y > w.getBottom())) { 
      InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); 
      imm.hideSoftInputFromWindow(getWindow().getCurrentFocus().getWindowToken(), 0); 
     } 
    } 
return ret; 
} 

感谢。

+0

看看http://stackoverflow.com/questions/8384067/how-to-dismiss-the-dialog-with-click-on-outside-of-the-dialog/8384124#8384124 – user370305

+1

我已经检查过这个链接。但是可以进行对话而不是微调。 –

+0

你必须检查你的活动窗口(Spinner之外)的触摸。我在那个答案中提到了。 – user370305

回答

0

有一个为Dialoglink

那么你为什么不尝试创建一个自定义对话框(simple example),这将看起来像一个旋转的解决方案?使用单选按钮为您的对话框创建布局文件。

当用户点击一个单选按钮,呼叫dialog.dismiss(),然后进行下一步

+0

感谢您的回答。我已经检查过您提供的链接。 –

+0

重点在于将'Dialog'设置为'Spinner',而不是链接。尝试使用'对话框',使其类似于'微调' –

1

必要的代码我本人来说却是创建一个自定义AlertDialogsetSingleChoiceItems()做同样的事情。然后我用setCanceledOnTouchOutside()

+0

感谢您的答案。我在之前的应用程序中尝试过,并获得成功。但在这里我的要求是针对微调。不适用于AlertDialog。如果你知道帮助我请 –

+0

你可以通过''Spinner' action'onClick()'并运行'AlertDialog'我看不出有什么困难。 – shkschneider