【问题标题】:How do i reference the objects created in the XML from the main .java program?如何从主 .java 程序引用在 XML 中创建的对象?
【发布时间】:2011-08-22 18:58:25
【问题描述】:

我正在使用 Eclipse 创建一个 android 应用程序。这是我第一个使用 eclipse 或为 android 编写的程序。我在我的 XML 中创建了几个对象(4 个按钮和一个文本视图)。当按下其中一个按钮时,该程序应该在文本视图中显示随机结果。确定随机结果的代码没有问题,但我找不到如何引用对象以编辑文本视图或将按钮连接到我制作的功能。如何从主 .java 程序引用 XML 中创建的对象?

【问题讨论】:

    标签: android motorola-droid


    【解决方案1】:

    例如:

    R.layout.main   // layout with "main" id
    R.string.hello  // string with "hello" id
    R.id.button     // button with "button" id
    

    如果你想获得一个视图,例如一个按钮:

    Button launch = (Button) findViewById(R.id.button_id);
    

    【讨论】:

      猜你喜欢
      • 2011-11-28
      • 1970-01-01
      • 2012-07-13
      • 2021-09-08
      • 1970-01-01
      • 1970-01-01
      • 2019-09-17
      • 1970-01-01
      • 2021-12-31
      相关资源
      最近更新 更多