【问题标题】:How to make classes like "Notification" class in Android?如何在Android中制作类似“通知”类的类?
【发布时间】:2017-04-28 15:56:50
【问题描述】:

最近我一直在学习如何在 Android 中构建通知,我遇到了这个:

NotificationCompat.Builder nBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.mipmap.test_image)
                .setLargeIcon(largeIcon)
                .setContentTitle("Notification Compat")
                .setContentText("Notification area --> 
                Modifiable!").setNumber(num);

无论我如何编写代码,结果都是一样的。 即例如,如果我更改顺序,在 setContentTitle() 之前调用 setContentText()。它返回相同的 Notification Builder 对象。 代码:

NotificationCompat.Builder nBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.mipmap.test_image)
                .setLargeIcon(largeIcon)
                .setContentText("Notification area --> Modifiable!") // Modification
                .setContentTitle("Notification Compat").setNumber(num);

谁能告诉我这怎么可能? 谢谢。

【问题讨论】:

    标签: android notifications


    【解决方案1】:

    Here 是一个如何创建Builder pattern 的示例。

    示例

    public class FooBar {
       private int mA, mB;
    
       private FooBar(int a, int b) {
          this.mA = a;
          this.mB = b;
       }
    
       public int getA() {
          return mA;
       }
    
       public void setA(int a) {
          this.mA = a;
       }
    
       public int getB() {
          return mB;
       }
    
       public void setB(int b) {
          this.mB = b;
       }
    
       public static class FooBarBuilder {
          private int mNestedA, mNestedB;
    
          public FooBarBuilder() {
              this.mNestedA = 0;
              this.mNestedB = 0;
          }
    
          public FooBarBuilder(int nestedA, int nestedB) {
              this.mNestedA = nestedA;
              this.mNestedB = nestedB;
          }
    
          public int getNestedA() {
              return mNestedA;
          }
    
          public FooBarBuilder setNestedA(int nestedA) {
              this.mNestedA = nestedA;
              return this;
          }
    
          public int getNestedB() {
              return mNestedB;
          }
    
          public FooBarBuilder setNestedB(int nestedB) {
              this.mNestedB = nestedB;
              return this;
          }
    
          public FooBar create() {
              return new FooBar(mNestedA, mNestedB);
          }
       }
    }
    

    【讨论】:

      【解决方案2】:

      这是一个建造者模式。每次调用 Builder 的方法时,它都会设置一些您作为参数传递的值并返回相同的 Builder 对象。所以顺序无关紧要。如果您调用 setters 方法而不返回对象,也是一样的。 Builder的示例代码:

      public class Builder {
      
          String mTitle;
          int mCount;
      
          public Builder setTitle(String title) {
              mTitle = title;
              return this;
          }
      
          public Builder setCount(int count) {
              mCount = count;
              return this;
          }
      }
      

      用法:

      Builder builder = new Builder()
          .setTitle("Some title")
          .setCount(count);
      

      【讨论】:

      • 好吧...现在,这很有意义... 那么,在使用通过“count”传入的值设置“mCount”之后会发生什么? Builder类的构造函数会被调用,所有指定的值都会传入其中?
      • 不,一旦您创建了 Builder (Builder builder = new Builder()) 的实例,就会调用空构造函数。当您调用该方法时,会设置值并返回相同的构建器对象。构造函数没有被调用。
      • 那么,“return this”是用来做这个的吗?到,返回builder对象?
      猜你喜欢
      • 2023-03-23
      • 2013-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多