【问题标题】:Hide a loading spinner when you click outside of it in Android在 Android 中单击加载微调器外部时隐藏加载微调器
【发布时间】:2012-10-28 11:34:08
【问题描述】:

我正在使用带有一组项目的加载微调器动画。如果您单击它的外部,那么它应该会消失。有人知道怎么做吗?

我已经试过了。它正在使用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;
}

提前致谢。

【问题讨论】:

  • 我已经检查了这个链接。但它可以用于对话框,但不能用于微调器。
  • 您必须检查您的活动窗口的触摸(在 Spinner 之外)。我在那个答案中提到过。
  • 这不是默认行为吗? :\ 我几个小时前刚试过。
  • @SerdarS。是的会有返回按钮,但是每次点击返回按钮都不是很好的方法。我们应该通过提供这种类型的服务来简化应用程序给最终用户。

标签: android spinner


【解决方案1】:

我个人所做的是用setSingleChoiceItems() 创建一个自定义AlertDialog 来做同样的事情。然后我用setCanceledOnTouchOutside()

【讨论】:

  • 感谢您的回答。我在以前的应用程序中尝试过,也成功了。但这里我的要求是微调。不是警报对话框。如果你知道请帮助我
  • 您可以抓住Spinner 操作onClick() 并运行AlertDialog 我看不出有什么困难。
【解决方案2】:

Dialoglink有解决方案

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

当用户单击单选按钮时,调用dialog.dismiss() 并执行必要的代码

【讨论】:

  • 感谢您的回答。我已经检查了您之前提供的链接。
  • 重点在于Dialog 的样式化为Spinner,而不是在链接中。尝试使用Dialog,使其类似于Spinner
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-18
  • 2014-08-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多