`

android 日期时间格式转换;软键盘显示消失;获取系统title

 
阅读更多

获取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 plaincopy
 
  1. private static int flagsDate = DateUtils.FORMAT_SHOW_DATE;  
  2. private static int flagsTime = DateUtils.FORMAT_SHOW_TIME ;  
  3. private static int flagsWeek = DateUtils.FORMAT_SHOW_WEEKDAY;  
  4. String dateStr = (String)DateUtils.formatDateTime(context, System.currentTimeMillis(), flagsDate);//5月11日  
  5. String timeStr = (String)DateUtils.formatDateTime(context, System.currentTimeMillis(), flagsTime);//16:40  
  6. String weekStr = (String)DateUtils.formatDateTime(context, System.currentTimeMillis(), flagsWeek);//星期二  

 

 

12小时格式时,获取上午还是下午:

String smPmStr = DateUtils.getAMPMString(Calendar.getInstance().get(Calendar.AM_PM));//上午(下午)

 

12小时时间格式时,只显示时间,不显示“上午“这样的字符串:

 

[java] view plaincopy
 
  1. private final static String M12 = "h:mm";  
  2. private final static String M24 = "kk:mm";  
  3. formatTime = android.text.format.DateFormat.is24HourFormat(context)? M24 : M12;  
  4. String timeStr = (String) DateFormat.format(formatTime,System.currentTimeMillis());  

 

 

将系统当前事件,转化为所需格式:

 

[java] view plaincopy
 
  1. long dateTaken = System.currentTimeMillis();  
  2.             if (dateTaken != 0) {  
  3.                 String datetime = DateFormat.format("yyyy:MM:dd kk:mm:ss", dateTaken).toString();  
  4.                 Log.e(TAG,"datetime : " +  datetime);  
  5.                   
  6.             }  

 

 

如果为今天,则显示时间,如果不是今天则显示日期

 

[java] view plaincopy
 
  1. private String getDate(long dateTime)  
  2.         {  
  3.             int flags = 0;  
  4.             String date = "";  
  5.               
  6.             if (DateUtils.isToday(dateTime))  
  7.             {  
  8.                 flags = DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_24HOUR;  
  9.                 date = (String)DateUtils.formatDateTime(mContext, dateTime, flags);  
  10.             }   
  11.             else  
  12.             {  
  13.                 flags = DateUtils.FORMAT_SHOW_DATE;  
  14.                 date = (String)DateUtils.formatDateTime(mContext, dateTime, flags);  
  15.             }  
  16.             return date;  
  17.         }  

 

 

在源码短信息模块中MessageUtils.java中有这样一个函数,与上面的功能相同:

 

[java] view plaincopy
 
  1. public static String formatTimeStampString(Context context, long when, boolean fullFormat) {  
  2.         Time then = new Time();  
  3.         then.set(when);  
  4.         Time now = new Time();  
  5.         now.setToNow();  
  6.         // Basic settings for formatDateTime() we want for all cases.  
  7.         int format_flags = DateUtils.FORMAT_NO_NOON_MIDNIGHT |  
  8.                            DateUtils.FORMAT_ABBREV_ALL |  
  9.                            DateUtils.FORMAT_CAP_AMPM;  
  10.         // If the message is from a different year, show the date and year.  
  11.         if (then.year != now.year) {  
  12.             format_flags |= DateUtils.FORMAT_SHOW_YEAR | DateUtils.FORMAT_SHOW_DATE;  
  13.         } else if (then.yearDay != now.yearDay) {  
  14.             // If it is from a different day than today, show only the date.  
  15.             format_flags |= DateUtils.FORMAT_SHOW_DATE;  
  16.         } else {  
  17.             // Otherwise, if the message is from today, show the time.  
  18.             format_flags |= DateUtils.FORMAT_SHOW_TIME;  
  19.         }  
  20.         // If the caller has asked for full details, make sure to show the date  
  21.         // and time no matter what we've determined above (but still make showing  
  22.         // the year only happen if it is a different year from today).  
  23.         if (fullFormat) {  
  24.             format_flags |= (DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_TIME);  
  25.         }  
  26.         return DateUtils.formatDateTime(context, when, format_flags);  
  27.     }  

 

 

软键盘显示消失及取反

 

[java] view plaincopy
 
  1. InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);    
  2.              View view = getCurrentFocus();    
  3.              if (view != null){    
  4.                 // imm.showSoftInput(view, 0); //显示软键盘    
  5.                  imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);  
  6.                 // imm.hideSoftInputFromWindow(view.getWindowToken(), 0);//隐藏软键盘  // InputMethodManager.HIDE_NOT_ALWAYS);  
  7.              }  

 

 

或者

getWindow().setSoftInputMode(
               WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);

 

或者

 

[java] view plaincopy
 
  1. public void hideInputMethod() {  
  2.         InputMethodManager imm = getInputMethodManager();  
  3.         if (imm != null) {  
  4.             imm.hideSoftInputFromWindow(getWindowToken(), 0);  
  5.         }  
  6.     }  

 

 

 

[java] view plaincopy
 
  1. public void showInputMethod() {  
  2.         InputMethodManager imm = getInputMethodManager();  
  3.         if (imm != null) {  
  4.             imm.showSoftInput(this0);  
  5.         }  
  6.     }  
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics