Android,Java中的Integer类,字符串处理及格式化

Integer类

Integer是Number类的一个子类,有两个构造方法。

1.Integer(int number)

2.Integer(String str)


Integer类常用构造方法

方法  byteValue()  返回值byte  以byte类型返回该Integer对象。

方法  ompareTo(Integer anotherOnteger)  返回值int  比较两个Integer,值相等返回0,小于another返回负值,大于则返回正值。

方法  equals(Object integerObject)  返回值为boolean  用法不必多说。

方法  intValue()  返回值int  转为in型返回

方法  shortValue()  类似intValue,一样的道理。

方法  toString()  转为String并返回

方法  valueOf(String str) String转为Integer并返回

方法  pareseInt(String str)  返回包含在String中数字的等价int值。

由源码看出以上方法均为static。转化一个String数字可以用 Integer.valueOf(str).intvalue()。


字符串处理及格式化

获取长度str.length()。

查找indexOf()或者lastindexOf()分别查找第一次和最后一次出现的位置。

获取指定位置字符用charAt()。

获取子字符串str.substring(int beginindex) 或者substring(int beginindex , int endIndex),前者获取beginindex之后字符串,后者获取beginindex到endindex之间字符串。

去除字符串空格用str.trim()。

替换用str.replace(char oldChar, char newChar),注意替换将会全部替换。

判断开始结尾用str.tartsWith(String str)和str.endsWith(String str)。

equals()不说。

equalsIgnoreCase(),忽略大小写的equals比较。

compareTo(str)按字典顺序比较,equals(str)相等才返回0,如果在str之前返回负数,否则相反。

大小写转换str.toLowerCase()和str.toUpperCase()。

字符串分割str.split(str)如str.split(","),可以定义多个分隔符需要使用“|”,如str.split(",|=") 。


格式化字符串

日期格式化

Date date = new Date();

String str = String.format("%te",date)

//%te为各种转换符之一。使用format可以格式化日期,时间,等等。格式化时间还可以用SimpleDateFormat

Date date = new Date();

SimpleDateFormat sdf = new SimpleDateFprmat("HH-MM-SS");//hh-mm-ss为格式化为12小时

String str = sdt.format(date);


其他类型格式化以及正则表达式

String str = String.format("%d",400/2);格式化为十进制,其他类似。

正则表达式,用于判断字符串类型是否满足某一格式。“.”为正则表达式,若在正则表达式使用,应为"\."。


Android 中按back键不退出程序方法。

重写onBackPressed().

@Override

public void onBackPressed(){

Intent intent = new Intent(Intent.ACTION_MAIN);

intent.setFlags(FLAG_ACTIVITY_NEW_TASK);

intent.addCategory();

startActivity(intent);


关于notification


public void showNotification(){

    NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
    builder.setContentText(tq.getText().toString()+""+qw1.getText().toString());
    builder.setContentTitle(cityName.getText().toString()+"天气");
    builder.setPriority(Notification.PRIORITY_MIN);
    builder.setTicker("天气预报");
    builder.setAutoCancel(true);
    Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.forecast);
    builder.setSmallIcon(R.drawable.tian);
    builder.setLargeIcon(bitmap);
    Intent intent = new Intent(this,WeatherActivity.class);
    PendingIntent pi = PendingIntent.getActivity(this,0,intent,0);
    builder.setContentIntent(pi);
    Notification notification = builder.build();
    notification.flags |=Notification.FLAG_NO_CLEAR;
    notification.flags |=Notification.FLAG_AUTO_CANCEL;
    manager.notify(1,notification);


}
先用getSystemService(NOTIFICATION——SERVICE)来为NotificationManager获取 实例,再用NotificaCompat.Build来获得一个对象,通过build设置各种通知属性。使用PendingIntent启动活动。再获取Notification实例并设置Flags属性。






  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值