【问题标题】:How do you attach a function to a button in Eclipse?如何在 Eclipse 中将函数附加到按钮?
【发布时间】:2014-06-15 19:44:59
【问题描述】:

我是编程新手。我正在制作一个短信应用程序。我不知道如何将功能附加到按钮。这是活动和xml。

    public void sendSMS() {
    Log.d("TAG", "button is now clicked");
    String phoneNumber = "443*******";
    String message = "Hello! How are you?";

    SmsManager; SmsManager = SmsManager.getDefault();
    SmsManager.sendTextMessage(phoneNumber, null, message, null, null);
}

          <Button
            android:id="@+id/button1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Send Message" />

您应该如何将功能附加到按钮?

谢谢,感谢您的宝贵时间。

【问题讨论】:

标签: android eclipse button


【解决方案1】:

最简单的方法是使用 onClick 属性。

将此属性添加到您的 Button 元素:

android:onClick="sendSMS"

Android 将尝试在您的活动中调用 sendSMS 方法。它将 View 作为参数(将设置为您单击的视图),因此您需要将方法更改为:

public void sendSMS(View view)

这在此处的文档中都有介绍: http://developer.android.com/guide/topics/ui/controls/button.html

【讨论】:

    猜你喜欢
    • 2016-03-04
    • 2021-11-14
    • 1970-01-01
    • 1970-01-01
    • 2022-07-06
    • 1970-01-01
    • 1970-01-01
    • 2016-01-12
    • 1970-01-01
    相关资源
    最近更新 更多