Codename One 获取并禁用所有组件和子组件

huangapple go评论73阅读模式
英文:

Codename One get and disable all components and sub components

问题

我有一个包含多个容器、按钮和标签等组件的 Android 应用程序。

该应用程序使用弹出窗口在首次运行时向用户介绍应用程序及其组件的使用情况。

我希望在用户按下弹出对话框的关闭按钮之前,禁用表单和/或所有组件和子组件。

我尝试过使用 form.setEnabled(false),但没有起作用。

form.getComponentCount() 也只会获取容器和工具栏,而不是容器内部的内容,所以不足够。

现在我使用以下代码来禁用这些组件:

form.setScrollable(false);

for (Component component : containerOne) {
    component.setEnabled(false);
}

for (Component component : containerTwo) {
    component.setEnabled(false);
}

for (Component component : containerThree) {
    component.setEnabled(false);
}

buttonOne.setEnabled(false);
buttonTwo.setEnabled(false);

textFieldOne.setEnabled(false);
textFieldTwo.setEnabled(false);

但这需要大量的代码,因为我之后还需要启用这些组件,而且我还使用了多个类。

有没有一行代码或更简单的方法来实现这一点?

英文:

I have an Android application with several containers and other components like buttons and labels.

The app uses popups to inform the user on the first run about the usage of the app and its components.

I want to disable the form and/ or all the components and sub components inside, until the users presses the close button of the popup dialog.

I have tried using form.setEnabled (false), but that didn't work.

form.getComponentCount () 

also only gets the containers and the toolbar and not what's inside the containers, so it is insufficient.

Now I use the following code to disable the components :

form.setScrollable (false); 

for (Component component : containerOne ) {
		component.setEnabled(false);
}

for (Component component : containerTwo ) {
		component.setEnabled(false);
}

for (Component component : containerThree ) {
		component.setEnabled(false);
}

buttonOne.setEnabled(false);
buttonTwo.setEnabled(false);

textFieldOne.setEnabled(false);
textFieldTwo.setEnabled(false); 

but this takes a lot of code, since I need to enable the components later and I am also using several classes.

I there a one-liner or easier way to achieve this?

答案1

得分: 2

你可以使用ComponentSelector在一行代码中启用/禁用组件。

$("*", rootContainer).setEnabled(false)
  1. $ComponentSelector.select() 的别名。
  2. " * " 是选择器(类似于 CSS 选择器),表示匹配所有组件。
  3. rootContainer 是一个作为开始选择的根的 Container 对象。
  4. setEnabled(false) 在找到的所有组件上将 enabled 设置为 false。
英文:

You can use the ComponentSelector to enable/disable components in one line of code.

$("*", rootContainer).setEnabled(false)
  1. The $ is an alias of ComponentSelector.select()
  2. The "*" is a selector (similar to a CSS selector) that says to match all components.
  3. The rootContainer is a Container object that serves as the root to being selection on.
  4. setEnabled(false) sets enabled=false on all Components in the found set.

答案2

得分: 1

既然您使用了对话框,只要表单中没有任何可点击的内容,它就不会产生任何交互。

如果您想进一步阻止任何内容,禁用会有问题,因为它不是递归的。只需在Form中覆盖pointerPressedpointerReleased方法,并有条件地阻止它们。这样事件就不会继续传播,您可以在不影响样式的情况下获得相同的效果。

例如:

class MyForm extends Form {
    private boolean blockFormEvents;
    // ...

    @Override
    public void pointerReleased(int x, int y) {
        if (blockFormEvents) return;
        super.pointerReleased(x, y);
    }

    @Override
    public void pointerReleased(int[] x, int[] y) {
        if (blockFormEvents) return;
        super.pointerReleased(x, y);
    }

    @Override
    public void pointerPressed(int x, int y) {
        if (blockFormEvents) return;
        super.pointerPressed(x, y);
    }

    @Override
    public void pointerPressed(int[] x, int[] y) {
        if (blockFormEvents) return;
        super.pointerPressed(x, y);
    }
}
英文:

Since you use a Dialog as long as it shows nothing in the form is clickable.

If you want to further block anything disabling is problematic because it isn't recursive. Just override pointer pressed/released in Form and conditionally block them. They won't propagate the event onward and you can get the same effect without the problematic style change to disabled.

e.g.:

class MyForm extends Form {
    private boolean blockFormEvents;
    // ...
   
    @Override
    public void pointerReleased(int x, int y) {
        if(blockFormEvents) return;
        super.pointerReleased(x, y);
    }
   
    @Override
    public void pointerReleased(int[] x, int[] y) {
        if(blockFormEvents) return;
        super.pointerReleased(x, y);
    }

    @Override
    public void pointerPressed(int x, int y) {
        if(blockFormEvents) return;
        super.pointerPressed(x, y);
    }
   
    @Override
    public void pointerPressed(int[] x, int[] y) {
        if(blockFormEvents) return;
        super.pointerPressed(x, y);
    }
}

huangapple
  • 本文由 发表于 2020年9月21日 01:04:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/63981568.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定