Android 中一些常用而又容易忘记的

来源:互联网 发布:单片机压力传感器调试 编辑:程序博客网 时间:2024/05/21 23:55

  有时候,需要一些小的功能上网找到之后会用word保存起来,但是word打开的时候死慢,而且明显不直观,然后我在想,我的的知识都是网上获取的,我应该也有责任分享知识,这样大家都能获得最有用最直接的知识,提高成本,也希望大家都在网上分享成果。


      所以就整理一下,方便自己和他人,欢迎指出错误和多种解决方法,我会及时改正。


1.解决listview上 Item上有按钮时 item本身不能点击的问题

[java] view plaincopy
  1. 1.Item布局的根布局加上android:descendantFocusability=”blocksDescendants”的属性  
  2.   
  3. beforeDescendants:viewgroup会优先其子类控件而获取到焦点  
  4. afterDescendants:viewgroup只有当其子类控件不需要获取焦点时才获取焦点  
  5. blocksDescendants:viewgroup会覆盖子类控件而直接获得焦点  
  6.   
  7. 如果不行的话再继续往下加  
  8.   
  9. 2在listview里 添加代码 android:focusable="true"//是否获取焦点  
  10.         // 获取listitem下面的view的值,item与button都能响应不同的事件,无冲突  
  11.         public View getView(int position, View convertView, ViewGroup parent) {  
  12.                 if (convertView == null) {  
  13.                         convertView = m_Inflater.inflate(resource, null);  
  14.                         convertView.setClickable(true);  
  15.                         convertView.setOnClickListener(new listener(position));  
  16.                 }  
  17.                 if (resource == R.layout.index_listview_copy) {  
  18.                         indexShareLinerlayout = (LinearLayout) convertView  
  19.                                         .findViewById(R.id.index_share_linerlayout);  
  20.                         indexShareLinerlayout.setOnClickListener(new listener(position));  
  21.                 }  
  22.                 return super.getView(position, convertView, parent);  
  23.         }  
  24.   
  25.         class listener implements OnClickListener {  
  26.                 private int position;  
  27.                 public listener(int position) {  
  28.                         this.position = position;  
  29.                 }  
  30.                 @Override  
  31.                 public void onClick(View v) {  
  32.                         switch (v.getId()) {  
  33.                         case R.id.index_share_linerlayout:  
  34.                                 Dialog dialog = new MyDialog(context, R.style.MyDialog);  
  35.                                 dialog.show();  
  36.                                 break;  
  37.                         default:  
  38.                                 Intent intentcomment = new Intent();  
  39.                                 intentcomment.setClass(context, CommentActivity.class);  
  40.                                 context.startActivity(intentcomment);  
  41.                                 break;  
  42.                         }  
  43.                 }  
  44.   
  45.         }  


2.获取屏幕宽高

[java] view plaincopy
  1. DisplayMetrics displayMetrics = new DisplayMetrics();     
  2. this.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);     
  3. int height = displayMetrics.heightPixels;     
  4. int width = displayMetrics.widthPixels;   


3.LayoutInflater和inflate()方法的用法

[java] view plaincopy
  1. LayoutInflater作用是将layout的xml布局文件实例化为View类对象。  
  2. 实现LayoutInflater的实例化共有3种方法,  
  3. (1).通过SystemService获得  
  4. LayoutInflaterinflater = (LayoutInflater)  
  5. context.getSystemServices(Context.LAYOUT_INFLATER_SERVICES);  
  6. Viewview = inflater.inflate(R.layout.main, null);  
  7. (2).从给定的context中获得  
  8. LayoutInflater inflater = LayoutInflater.from(context);  
  9. Viewview = inflater.inflate(R.layout.mian, null);</font></font>  
  10. (3).LayoutInflaterinflater =getLayoutInflater();(在Activity中可以使用,实际上是View子类下window的一个函数)  
  11. Viewlayout = inflater.inflate(R.layout.main, null);  

4.有时候按钮文字在代码中设置setTextColor无效

[html] view plaincopy
  1. ColorStateList colors = getContext().getResources().getColorStateList(R.color.radio_txt);    
  2. radioButton.setTextColor(colors);   


5.输入法挤乱布局问题

[java] view plaincopy
  1. 如果不想布局动  在这个activity中的AndroidManifest.xml中加入代码  
  2. android:windowSoftInputMode="adjustPan"  
  3.   
  4. 如果想让布局动 则加入代码  
  5. Android:windowSoftInputMode="stateVisible|adjustPan"  
  6. 还有一种方法    
  7. 在对应的layout XML的顶级元素上加一层ScrollView  
  8. <ScrollView xmlns:Android="http://schemas.android.com/apk/res/android "   
  9.         Android:layout_width="fill_parent"   
  10.          Android:layout_height="fill_parent">   
  11. </ScrollView>  
  12.   
  13. 如果输入法和输入框有点间距的话,就可以用Android:windowSoftInputMode="adjustReszie"  
  14. 如果要使adjustResize起效的话,必须要考虑好布局,  
  15. 布局必须要让最底下的widget或者layout的位置为android:layout_alignParentBottom="true"才能使之生效  


6.Android让页面默认弹出输入法和不弹出输入法的问题

[html] view plaincopy
  1. <pre name="code" class="java">看一个manifest中Activity的配置,如果这个页面有EditText,并且我们想要进入这个页面的时候默认弹出输入法,可以这样设置这个属相:  
  2. android:windowSoftInputMode=stateVisible,这样就会默认弹起输入法,当然还有别的办法。  
  3. <activity android:name=".ui.login"  
  4.                    android:configChanges="orientation|keyboardHidden|locale"  
  5.                    android:screenOrientation="portrait"  
  6.                    android:windowSoftInputMode="stateVisible|adjustPan" >  
  7.          </activity>  
  8.   
  9. Android EditText 不弹出输入法总结  
  10. 方法一:   
  11. 在AndroidMainfest.xml中选择哪个activity,设置windowSoftInputMode属性为adjustUnspecified|stateHidden  
  12.   
  13. 方法二:   
  14. 让EditText失去焦点,使用EditText的clearFocus方法   
  15. 例如:EditText edit=(EditText)findViewById(R.id.edit);   
  16. edit.clearFocus();   
  17.   
  18. 方法三:   
  19. 强制隐藏Android输入法窗口   
  20. 例如:EditText edit=(EditText)findViewById(R.id.edit);   
  21. InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);  
  22. imm.hideSoftInputFromWindow(edit.getWindowToken(),0);   
  23.   
  24. EditText始终不弹出软件键盘   
  25. 例:EditText edit=(EditText)findViewById(R.id.edit);   
  26. edit.setInputType(InputType.TYPE_NULL);  

7.获取时间差几小时几分钟前

[java] view plaincopy
  1.     private final static long minute = 60 * 1000;// 1分钟  
  2.     private final static long hour = 60 * minute;// 1小时  
  3.     private final static long day = 24 * hour;// 1天  
  4.     private final static long month = 31 * day;// 月  
  5.     private final static long year = 12 * month;// 年  
  6.   
  7.     /** 
  8.      * 返回文字描述的日期 
  9.      *  
  10.      * @param date 
  11.      * @return 
  12.      */  
  13.     public static String getTimeFormatText(Date date) {  
  14.         if (date == null) {  
  15.             return null;  
  16.         }  
  17.         long diff = new Date().getTime() - date.getTime();  
  18.         long r = 0;  
  19.         if (diff > year) {  
  20.             r = (diff / year);  
  21.             return r + "年前";  
  22.         }  
  23.         if (diff > month) {  
  24.             r = (diff / month);  
  25.             return r + "个月前";  
  26.         }  
  27.         if (diff > day) {  
  28.             r = (diff / day);  
  29.             return r + "天前";  
  30.         }  
  31.         if (diff > hour) {  
  32.             r = (diff / hour);  
  33.             return r + "个小时前";  
  34.         }  
  35.         if (diff > minute) {  
  36.             r = (diff / minute);  
  37.             return r + "分钟前";  
  38.         }  
  39.         return "刚刚";  
  40.     }  
  41.       
  42. // 如果要奖Sring转为date型需要用的到方法   
  43. //SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");                  
  44. //Date date = sdf.parse("2008-08-08 12:10:12");    


8.bitmap与String的转化,以及url转化成bitmap

[java] view plaincopy
  1. /** 
  2.  * 图片转成string 
  3.  *  
  4.  * @param bitmap 
  5.  * @return 
  6.  */  
  7. public static String convertIconToString(Bitmap bitmap) {  
  8.         ByteArrayOutputStream baos = new ByteArrayOutputStream();// outputstream  
  9.         bitmap.compress(CompressFormat.PNG, 100, baos);  
  10.         byte[] appicon = baos.toByteArray();// 转为byte数组  
  11.         return Base64.encodeToString(appicon, Base64.DEFAULT);  
  12.   
  13. }  
  14.   
  15. /** 
  16.  * string转成bitmap 
  17.  *  
  18.  * @param st 
  19.  */  
  20. public static Bitmap convertStringToIcon(String st) {  
  21.         // OutputStream out;  
  22.         Bitmap bitmap = null;  
  23.         try {  
  24.                 
  25.                 byte[] bitmapArray;  
  26.                 bitmapArray = Base64.decode(st, Base64.DEFAULT);  
  27.                 bitmap = BitmapFactory.decodeByteArray(bitmapArray, 0,  
  28.                                 bitmapArray.length);  
  29.                 return bitmap;  
  30.         } catch (Exception e) {  
  31.                 return null;  
  32.         }  
  33. }  
  34.   
  35. /** 
  36.  * 根据头像的url 获取bitmap 
  37.  */  
  38. public Bitmap getImgBitmap(String imageUri) {  
  39.         // 显示网络上的图片  
  40.         Bitmap bitmap = null;  
  41.         HttpURLConnection conn = null;  
  42.         InputStream is = null;  
  43.         try {  
  44.                 URL myFileUrl = new URL(imageUri);  
  45.                 conn = (HttpURLConnection) myFileUrl.openConnection();  
  46.                 conn.setDoInput(true);  
  47.                 conn.connect();  
  48.   
  49.                 is = conn.getInputStream();  
  50.                 bitmap = BitmapFactory.decodeStream(is);  
  51.                 is.close();  
  52.         } catch (IOException e) {  
  53.                 e.printStackTrace();  
  54.                 return null;  
  55.         } finally {  
  56.                 try {  
  57.                         conn.disconnect();  
  58.                         is.close();  
  59.                         is.reset();  
  60.                 } catch (IOException e) {  
  61.                         // TODO Auto-generated catch block  
  62.                         e.printStackTrace();  
  63.                 }  
  64.         }  
  65.         return bitmap;  

9.md5 加密
[java] view plaincopy
  1. /* md5 加密 */  
  2. static public String md5(String str) {  
  3.         MessageDigest algorithm = null;  
  4.         try {  
  5.                 algorithm = MessageDigest.getInstance("MD5");  
  6.         } catch (NoSuchAlgorithmException e) {  
  7.                 e.printStackTrace();  
  8.         }  
  9.         if (algorithm != null) {  
  10.                 algorithm.reset();  
  11.                 algorithm.update(str.getBytes());  
  12.                 byte[] bytes = algorithm.digest();  
  13.                 StringBuilder hexString = new StringBuilder();  
  14.                 for (byte b : bytes) {  
  15.                         hexString.append(Integer.toHexString(0xFF & b));  
  16.                 }  
  17.                 return hexString.toString();  
  18.         }  
  19.         return "";  
  20.   
  21.   
  22. }  
10.dip与px的转化,以及首字母大写

[java] view plaincopy
  1. public static int px2dip(Context context, float pxValue) {  
  2.         final float scale = context.getResources().getDisplayMetrics().density;  
  3.         return (int) (pxValue / scale + 0.5f);  
  4. }  
  5. public static int dip2px(Context context, float dipValue) {  
  6.         final float scale = context.getResources().getDisplayMetrics().density;  
  7.         return (int) (dipValue * scale + 0.5f);  
  8. }  
[java] view plaincopy
  1. /* 首字母大写 */  
  2.   static public String ucfirst(String str) {  
  3.           if (str != null && str != "") {  
  4.                   str = str.substring(01).toUpperCase() + str.substring(1);  
  5.           }  
  6.           return str;  
  7.   }  

0 0