方向(在 configChanges 处)更改使我的按钮在方向后停止工作

发布于 2024-12-22 03:40:47 字数 1229 浏览 2 评论 0原文

我有这个简洁的应用程序...

在 onCreate 时,它​​相应地绘制“startscreen.xml”:

        super.onCreate(savedInstanceState);

    requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
    setContentView(R.layout.startscreen);
    getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
            R.layout.window_title);

在我的应用程序中,我在清单中有“configChanges=”orientation“”。 我有,

    @Override
public void onConfigurationChanged(Configuration newConfig) {
  super.onConfigurationChanged(newConfig);
  setContentView(R.layout.startscreen);

}

startscreen.xml 既是横向布局又是纵向布局,两者是分开的。

所以对于这个问题,应用程序加载正常,按钮工作。我切换方向,按钮停止响应...

他们都使用relativelayout...

任何线索,有人吗? :S

编辑:

由于您的评论,现在尝试了此操作,但没有结果:S

    @Override
public void onConfigurationChanged(Configuration newConfig) {
  super.onConfigurationChanged(newConfig);
  setContentView(R.layout.startscreen);

    add_note.setOnClickListener(new View.OnClickListener() {
        public void onClick(final View v) {
            createNote();
        }

    });
}

您的意思是 onclickListener 还是这些:

add_note = (Button) findViewById(R.id.addnote);

? :S

I have this neat app...

At onCreate, it draws the "startscreen.xml" accordingly to :

        super.onCreate(savedInstanceState);

    requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
    setContentView(R.layout.startscreen);
    getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
            R.layout.window_title);

And in my app, i have "configChanges="orientation" " in the manifest.
And i have,

    @Override
public void onConfigurationChanged(Configuration newConfig) {
  super.onConfigurationChanged(newConfig);
  setContentView(R.layout.startscreen);

}

startscreen.xml is both a landscape and portrait-layout, both are separated.

So to the problem, the app loads fine, buttons work. I switch orientation, the button stops responding...

They are both using RelativeLayout...

Any clues, anyone? :S

EDIT:

Tried this now thanks to your comments, but no result :S

    @Override
public void onConfigurationChanged(Configuration newConfig) {
  super.onConfigurationChanged(newConfig);
  setContentView(R.layout.startscreen);

    add_note.setOnClickListener(new View.OnClickListener() {
        public void onClick(final View v) {
            createNote();
        }

    });
}

Do you mean that onclickListener or these :

add_note = (Button) findViewById(R.id.addnote);

?? :S

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吃兔兔 2024-12-29 03:40:47

如果您要在 onCreate 中向按钮添加点击侦听器,则需要向在 onConfigurationChanged 中创建的新按钮对象添加侦听器。

您可以考虑在 XML 文件中使用 android:onClick 属性,而不是在代码中设置点击侦听器。这是避免这个问题的好方法。

要从 XML 使用 android:onClick,请执行以下操作:

<Button
    android:id="@+id/addnote"
    android:onClick="addNoteMethod"
    ....
    />

然后在您的 Activity 中定义方法:

public void addNoteMethod(View view) {
    createNote();
}

android:onClick 方法的名称是完全任意的;它只需将 Activity 中的方法与正确的签名进行匹配 (void(View))。当发生点击时,将调用该方法,其方式与为 OnClickListener 调用 onClick 方法完全相同。

使用android:onClick时,您不需要调用setOnClickListener(甚至在onCreate,除非您需要它用于其他用途)。

If you are adding click listeners in onCreate to your buttons, you need to add listeners to the new button objects created in onConfigurationChanged.

You might consider using the android:onClick attribute in your XML files instead of setting click listeners in code. That's a nice way of avoiding this problem.

To use android:onClick from XML, do something like this:

<Button
    android:id="@+id/addnote"
    android:onClick="addNoteMethod"
    ....
    />

Then in your activity, define the method:

public void addNoteMethod(View view) {
    createNote();
}

The name of the android:onClick method is totally arbitrary; it just has to match a method in your activity with the proper signature (void <method_name>(View)). The method will be called when there's a click in exactly the same manner that the onClick method would be called for an OnClickListener.

When using android:onClick, you do not need to call setOnClickListener (or even retrieve a reference to the button in onCreate, unless you need it for something else).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文