【问题标题】:Disable All Touch Screen Interactions While Animation动画时禁用所有触摸屏交互
【发布时间】:2012-06-14 19:15:36
【问题描述】:

我希望在显示动画时禁用所有触摸屏交互。 我不希望在动画开始或结束的按钮上使用setClickable() 方法,因为有大量按钮。有什么建议吗?

【问题讨论】:

    标签: android android-widget clickable


    【解决方案1】:

    在您的 Activity 中,您可以覆盖 onTouchEvent 并始终覆盖 return true; 以指示您正在处理触摸事件。

    您可以找到该函数的文档there

    编辑这是一种禁用整个屏幕触摸而不是逐个处理每个视图的方法...首先像这样更改当前布局:

    <FrameLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >
    
        < .... put your current layout here ... />
    
        <TouchBlackHoleView
            android:id="@+id/black_hole"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />
    </FrameLayout>
    

    然后用类似这样的方式定义您的自定义视图:

    public class TouchBlackHoleView extends View {
        private boolean touch_disabled=true;
        @Override
        public boolean onTouchEvent(MotionEvent e) {
            return touch_disabled;
        }
        public disable_touch(boolean b) {
            touch_disabled=b;
        }
    }
    

    然后,在活动中,您可以禁用触摸

    (TouchBlackHoleView) black_hole = findViewById(R.id.black_hole);
    black_hole.disable_touch(true);
    

    然后启用它

    black_hole.disable_touch(false);
    

    【讨论】:

    • View view = findViewById(R.id.whole_view); view.setOnTouchListener(new OnTouchListener() { public boolean onTouch(View v, MotionEvent event) { // TODO Auto-generated method stub return true; } }); 我在动画开始时写了这个,但它不起作用请告诉我哪里错了
    • 这里的 whole_view 是我的主要布局的 id
    • 它是什么样的视图,您要禁用什么?是按钮吗?一个滚动视图? ... ?
    • 我的布局中到处都是按钮。我想禁用它们
    • 你有什么样的布局?也许您可以使用 FrameLayout 并在其上覆盖一个 View ,您可以在其中启用/禁用触摸...让我在答案中编写一些伪代码...
    【解决方案2】:

    实现的简单方法是在其上添加透明布局(将其添加到您的 xml 填充父级高度和宽度中)。

    在动画开始:transaparentlayout.setClickable(true);

    在动画结尾:transaparentlayout.setClickable(false);

    【讨论】:

      【解决方案3】:

      回答这个问题

      for (int i = 1; i < layout.getChildCount(); i++) { TableRow row = (TableRow) layout.getChildAt(i); row.setClickable(false);

      选择表格布局中包含所有视图的所有行并禁用它们

      【讨论】:

        【解决方案4】:

        最终,我将@Matthieu 的基本答案作为基本答案,并使其以这种方式工作。我决定发布我的答案,因为我可能需要 30 分钟才能理解为什么会出错。

        XML

        <...your path to this view and in the end --> .TouchBlackHoleView
            android:id="@+id/blackHole"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
        

        公共类 TouchBlackHoleView 扩展视图 { private boolean touchDisable = false;

        public TouchBlackHoleView(Context context) {
            super(context);
        }
        
        public TouchBlackHoleView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
        
        public TouchBlackHoleView(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
        }
        
        @Override
        public boolean onTouchEvent(MotionEvent event) {
            return touchDisable;
        }
        
        public void disableTouch(boolean value){
            touchDisable = value;
        }
        }
        

        使用

        blackHole = (TouchBlackHoleView) findViewById(R.id.blackHole);
        blackHole.disableTouch(true);
        

        享受

        【讨论】:

          猜你喜欢
          • 2019-11-26
          • 1970-01-01
          • 2012-03-18
          • 1970-01-01
          • 1970-01-01
          • 2018-11-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多