gpt4 book ai didi

android - 如何在 Android 中制作类似 "Notification"类的类?

转载 作者:行者123 更新时间:2023-11-30 00:34:28 25 4
gpt4 key购买 nike

最近我一直在学习如何在 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);

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

最佳答案

Here是如何创建 Builder 模式 的示例。

示例

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);
}
}
}

关于android - 如何在 Android 中制作类似 "Notification"类的类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43667310/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com