在Android中单击加载微调器外部时隐藏该微调器

在Android中单击加载微调器外部时隐藏该微调器,android,spinner,Android,Spinner,我正在使用带有一组项目的加载微调器动画。如果你在它外面点击,它就会消失。有人知道怎么做吗 我试过这个。它正在使用EditText。但它不适用于Spinner @Override public boolean dispatchTouchEvent(MotionEvent event) { View view = getCurrentFocus(); boolean ret = super.dispatchTouchEvent(event); if (view insta

我正在使用带有一组项目的加载微调器动画。如果你在它外面点击,它就会消失。有人知道怎么做吗

我试过这个。它正在使用
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;
}
@覆盖
公共布尔dispatchTouchEvent(MotionEvent){
视图=getCurrentFocus();
布尔ret=super.dispatchTouchEvent(事件);
if(查看EditText的实例| |查看微调器的实例){
视图w=getCurrentFocus();
int scrcoords[]=新int[2];
w、 getLocationOnScreen(scrcoords);
float x=event.getRawX()+w.getLeft()-scrcoords[0];
float y=event.getRawY()+w.getTop()-scrcoords[1];
如果(event.getAction()==MotionEvent.ACTION\u UP
&&(x=w.getRight()
||yw.getBottom()){
InputMethodManager imm=(InputMethodManager)getSystemService(Context.INPUT\u方法\u服务);
imm.hideSoftInputFromWindow(getWindow().getCurrentFocus().getWindowToken(),0);
}
}
返回ret;
}

提前感谢。

有一个
对话框的解决方案

那么,为什么不尝试创建一个看起来像微调器的自定义对话框()?使用单选按钮为对话框创建布局文件


当用户单击单选按钮时,调用
dialog.dismise()
并执行必要的下一步操作我个人所做的是使用
setSingleChoiceItems()
创建一个自定义
警报对话框
,以执行相同的操作。然后我使用了
setcanceledontouchout()

看看我已经检查过这个链接了。但可以使用对话框,但不能使用微调器。您必须检查活动窗口(微调器外部)的触摸。我在回答中提到过。这不是默认行为吗?:\“我几个小时前刚试过。”塞尔达斯说。是的,会有后退按钮,但每次单击后退按钮都不是很好的方法。我们应该通过提供此类服务来简化应用程序,以供最终用户使用。谢谢您的回答。我已经检查了您之前提供的链接。重点是将
对话框
样式化为
微调器
,而不是链接。尝试使用
对话框
,使其类似于
微调器
,谢谢您的回答。我在以前的申请中试过了,也成功了。但这里我的要求是纺纱机。不适用于AlertDialog。如果你知道帮助我,你可以抓住
微调器
动作
onClick()
,然后运行
警报对话框
,我看不出有什么困难。