【问题标题】:Need widget button example to display a dialog box or Toast需要小部件按钮示例来显示对话框或 Toast
【发布时间】:2013-07-20 19:12:02
【问题描述】:

我想创建一个带有按钮的小部件。当它被点击时,如何使它执行显示对话框或Toast之类的功能?

【问题讨论】:

  • 您必须付出更多的努力。答案肯定可以在 SO 上找到 - 挖掘答案(不仅仅是要求它)是乐趣的一部分。享受吧!

标签: android android-widget


【解决方案1】:

小部件与发送意图的remote views 一起使用。
你需要设置一个PendingIntent 看下一个example

在您的上下文(活动、服务...)收到意图后,您可以制作 Toast 或 Dialog。

【讨论】:

    【解决方案2】:

    代码

    Button button= (Button) findViewById(R.id.button1); // id which is defined inside your XML file below
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // Your Toast or dialog code goes here
        }
    });
    

    XML

    XML 文件中,将按钮定义为:

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

    记住

    用户定义的id应该用@+id定义,比如android:id="@+id/button1",而不是@id,比如android:id="@id/button1"

    【讨论】:

      猜你喜欢
      • 2012-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-06
      • 2019-10-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多