获取activty title bar:
TextView actionTitle = (TextView) findViewById(com.android.internal.R.id.action_bar_title);
View actionTitle = getWindow().getDecorView().findViewById(getResources().getIdentifier("android:id/action_bar_title", null, null));
privatestaticintflagsDate=DateUtils.FORMAT_SHOW_DATE;
privatestaticintflagsTime=DateUtils.FORMAT_SHOW_TIME;
privatestaticintflagsWeek=DateUtils.FORMAT_SHOW_WEEKDAY;
StringdateStr=(String)DateUtils.formatDateTime(context,System.currentTimeMillis(),flagsDate);//5月11日
StringtimeStr=(String)DateUtils.formatDateTime(context,System.currentTimeMillis(),flagsTime);//16:40
StringweekStr=(String)DateUtils.formatDateTime(context,System.currentTimeMillis(),flagsWeek);//星期二
12小时格式时,获取上午还是下午:
String smPmStr = DateUtils.getAMPMString(Calendar.getInstance().get(Calendar.AM_PM));//上午(下午)
12小时时间格式时,只显示时间,不显示“上午“这样的字符串:
privatefinalstaticStringM12="h:mm";
privatefinalstaticStringM24="kk:mm";
formatTime=android.text.format.DateFormat.is24HourFormat(context)?M24:M12;
StringtimeStr=(String)DateFormat.format(formatTime,System.currentTimeMillis());
将系统当前事件,转化为所需格式:
longdateTaken=System.currentTimeMillis();
if(dateTaken!=0){
Stringdatetime=DateFormat.format("yyyy:MM:ddkk:mm:ss",dateTaken).toString();
Log.e(TAG,"datetime:"+datetime);
}
如果为今天,则显示时间,如果不是今天则显示日期
privateStringgetDate(longdateTime)
{
intflags=0;
Stringdate="";
if(DateUtils.isToday(dateTime))
{
flags=DateUtils.FORMAT_SHOW_TIME|DateUtils.FORMAT_24HOUR;
date=(String)DateUtils.formatDateTime(mContext,dateTime,flags);
}
else
{
flags=DateUtils.FORMAT_SHOW_DATE;
date=(String)DateUtils.formatDateTime(mContext,dateTime,flags);
}
returndate;
}
在源码短信息模块中MessageUtils.java中有这样一个函数,与上面的功能相同:
publicstaticStringformatTimeStampString(Contextcontext,longwhen,booleanfullFormat){
Timethen=newTime();
then.set(when);
Timenow=newTime();
now.setToNow();
//BasicsettingsforformatDateTime()wewantforallcases.
intformat_flags=DateUtils.FORMAT_NO_NOON_MIDNIGHT|
DateUtils.FORMAT_ABBREV_ALL|
DateUtils.FORMAT_CAP_AMPM;
//Ifthemessageisfromadifferentyear,showthedateandyear.
if(then.year!=now.year){
format_flags|=DateUtils.FORMAT_SHOW_YEAR|DateUtils.FORMAT_SHOW_DATE;
}elseif(then.yearDay!=now.yearDay){
//Ifitisfromadifferentdaythantoday,showonlythedate.
format_flags|=DateUtils.FORMAT_SHOW_DATE;
}else{
//Otherwise,ifthemessageisfromtoday,showthetime.
format_flags|=DateUtils.FORMAT_SHOW_TIME;
}
//Ifthecallerhasaskedforfulldetails,makesuretoshowthedate
//andtimenomatterwhatwe'vedeterminedabove(butstillmakeshowing
//theyearonlyhappenifitisadifferentyearfromtoday).
if(fullFormat){
format_flags|=(DateUtils.FORMAT_SHOW_DATE|DateUtils.FORMAT_SHOW_TIME);
}
returnDateUtils.formatDateTime(context,when,format_flags);
}
软键盘显示消失及取反
InputMethodManagerimm=(InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
Viewview=getCurrentFocus();
if(view!=null){
//imm.showSoftInput(view,0);//显示软键盘
imm.toggleSoftInput(0,InputMethodManager.HIDE_NOT_ALWAYS);
//imm.hideSoftInputFromWindow(view.getWindowToken(),0);//隐藏软键盘//InputMethodManager.HIDE_NOT_ALWAYS);
}
或者
getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
或者
publicvoidhideInputMethod(){
InputMethodManagerimm=getInputMethodManager();
if(imm!=null){
imm.hideSoftInputFromWindow(getWindowToken(),0);
}
}
publicvoidshowInputMethod(){
InputMethodManagerimm=getInputMethodManager();
if(imm!=null){
imm.showSoftInput(this,0);
}
}