android 日期时间格式转换;软键盘显示消失;获取系统title
16lz
2022-07-25
获取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));
[java] view plain copy- 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小时时间格式时,只显示时间,不显示“上午“这样的字符串:
[java] view plain copy- privatefinalstaticStringM12="h:mm";
- privatefinalstaticStringM24="kk:mm";
- formatTime=android.text.format.DateFormat.is24HourFormat(context)?M24:M12;
- StringtimeStr=(String)DateFormat.format(formatTime,System.currentTimeMillis());
将系统当前事件,转化为所需格式:
[java] view plain copy- longdateTaken=System.currentTimeMillis();
- if(dateTaken!=0){
- Stringdatetime=DateFormat.format("yyyy:MM:ddkk:mm:ss",dateTaken).toString();
- Log.e(TAG,"datetime:"+datetime);
- }
如果为今天,则显示时间,如果不是今天则显示日期
[java] view plain copy- 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中有这样一个函数,与上面的功能相同:
[java] view plain copy- 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);
- }
软键盘显示消失及取反
[java] view plain copy- 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);
或者
[java] view plain copy- publicvoidhideInputMethod(){
- InputMethodManagerimm=getInputMethodManager();
- if(imm!=null){
- imm.hideSoftInputFromWindow(getWindowToken(),0);
- }
- }
- publicvoidshowInputMethod(){
- InputMethodManagerimm=getInputMethodManager();
- if(imm!=null){
- imm.showSoftInput(this,0);
- }
- }
更多相关文章
- Android软键盘适配问题
- Android(安卓)P SystemUI之StatusBar UI布局status_bar.xml解析
- android用户界面-组件Widget-地图视图MapView
- Android中使Dialog显示时背景不变暗
- Android(安卓)屏幕设置
- scrollview 滚动条
- android 中使用TextView实现分段显示不同颜色的字符串
- ImageView的scaletype属性
- Android(安卓)EditText不弹出软键盘