英文:
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)
$
是ComponentSelector.select()
的别名。- " * " 是选择器(类似于 CSS 选择器),表示匹配所有组件。
rootContainer
是一个作为开始选择的根的Container
对象。setEnabled(false)
在找到的所有组件上将 enabled 设置为 false。
英文:
You can use the ComponentSelector to enable/disable components in one line of code.
$("*", rootContainer).setEnabled(false)
- The
$
is an alias ofComponentSelector.select()
- The "*" is a selector (similar to a CSS selector) that says to match all components.
- The
rootContainer
is aContainer
object that serves as the root to being selection on. setEnabled(false)
sets enabled=false on all Components in the found set.
答案2
得分: 1
既然您使用了对话框,只要表单中没有任何可点击的内容,它就不会产生任何交互。
如果您想进一步阻止任何内容,禁用会有问题,因为它不是递归的。只需在Form
中覆盖pointerPressed
和pointerReleased
方法,并有条件地阻止它们。这样事件就不会继续传播,您可以在不影响样式的情况下获得相同的效果。
例如:
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);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论