一、ProgressBar
常用属性:
(1)style="?android:attr/progressBarStyleHorizontal" 把转圈的进度条改为水平样式。
(2)android:max 在水平样式的基础上,该属性表示进度条的最大值。进度条的当前值可以在java代码中用getProgress()方法获得,而setProgress()方法则用于设置当前值。
(3)android:indeterminate 如果这个属性设置为true,则不显示具体进度值,但仍旧是水平样式的进度条。
二、Notification
1、首先要创建一个NotificationManager
manager= (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
2、然后创建一个Notification
notification=new NotificationCompat.Builder(this,"channel1")
.setContentTitle("这个属性设置通知的标题")
.setContentText("这个属性设置通知的内容")
.setSmallIcon(R.drawable.ic_baseline_5g_24)//设置通知的小图标,图像不能有颜色(灰白)
.setLargeIcon("BitmapFactory.decodeResource(getResources(),R.drawable.picture1)")
//设置通知的大图标,可以有颜色,但是需要经过格式转化
.setAutoCancel(true)//设置为true之后,通知被单击后就消失
.setContentIntent(pendingIntent)//利用一个pendingIntent设置点击通知后的跳转意图
//pendingIntent具体定义为以下两行,实际代码中当然要放在Notification的定义之外。
//Intent intent=new Intent(MainActivity.this,MainActivity2.class);
//PendingIntent pendingIntent=PendingIntent.getActivity(this,0,intent ,0);
.build()
3、判断版本(有些老版本不能用),设置一个NotificationChannel
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O){
NotificationChannel channel = new NotificationChannel("channel1", "测试通知", NotificationManager.IMPORTANCE_LOW);
manager.createNotificationChannel(channel);
}
黑体部分:通知重要程度。有其他值可供选择。
4、最后在按钮内,触发通知
manager.notify(1,notification);// "1"是一个随意取的ID
public class MainActivity extends AppCompatActivity {
private Button btn1,btn2;
private NotificationManager manager;
private Notification notification;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1=findViewById(R.id.btn1);
btn2=findViewById(R.id.btn2);
Intent intent=new Intent(MainActivity.this,MainActivity2.class);
PendingIntent pendingIntent=PendingIntent.getActivity(this,0,intent ,0);
manager= (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notification=new NotificationCompat.Builder(this,"channel1")
.setContentTitle("中国移动")
.setContentText("你的话费已不足,请及时充值")
.setSmallIcon(R.drawable.ic_baseline_5g_24)
.setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.p1))
.setAutoCancel(true)
.setContentIntent(pendingIntent)
.build();
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.O){
NotificationChannel channel = new NotificationChannel("channel1", "测试通知", NotificationManager.IMPORTANCE_NONE);
manager.createNotificationChannel(channel);
}
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
manager.notify(1,notification);
}
});
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
}
}
本文介绍了Android中ProgressBar的两种样式及其属性设置,包括如何改变样式和设置最大值。同时,详细阐述了Notification的创建过程,从创建NotificationManager、Notification到设置通知内容、图标、点击行为,并讲解了在不同Android版本下创建NotificationChannel的方法。最后,通过示例展示了如何在按钮点击事件中触发通知。
427

被折叠的 条评论
为什么被折叠?



