android 之杂七杂八

前言:单独开一篇将遇到的关于android代码中的一些小问题予以总结。


一、关闭软键盘

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. InputMethodManager inputMethodManager = (InputMethodManager)Activity.this.getSystemService(Context.INPUT_METHOD_SERVICE);    
  2. inputMethodManager.hideSoftInputFromWindow(Activity.this.getCurrentFocus().getWindowToken(), InputMethodManager.RESULT_HIDDEN);  

二、dip与pix转换

在代码中涉及到的int,比如LayoutParams.setPadding(int……)与android.view.ViewGroup.LayoutParams.height、android.view.ViewGroup.LayoutParams.width这些,全部都是指的pix(像素),而在XML中,为了设备无关性,我们一般使用DIP来代替相素,所以在代码中,我们要常常用到DIP与PIX的转换,转换公式如下:

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. public static int dip2px(Context context, float dipValue){   
  2.           final float scale = context.getResources().getDisplayMetrics().density;   
  3.           return (int)(dipValue * scale + 0.5f);   
  4. }   
  5.           
  6. public static int px2dip(Context context, float pxValue){   
  7.           final float scale = context.getResources().getDisplayMetrics().density;   
  8.           return (int)(pxValue / scale + 0.5f);   
  9. }  

相关网址:《android中dip、px相互换算》:http://www.cnblogs.com/error404/archive/2011/11/03/2234165.html

三、使textView点击变色(Selector)
首先在res目录下新建drawable文件夹,再在新建的drawable文件夹中新建mytv_view.xml,其目录结构为:res/drawable/mytv_view.xml

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">  
  3.     <item android:state_selected="true" android:color="#ff0000" />  
  4.     <item android:state_focused="true" android:color="#ff0000" />  
  5.     <item android:state_pressed="true" android:color="#ff0000" />  
  6.     <item android:color="#89683B" />  
  7. </selector>   

2.设置textview的selector

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <TextView  
  2.     android:id="@+id/Item_Text"  
  3.     android:layout_width="wrap_content"  
  4.     android:layout_height="wrap_content"  
  5.     android:layout_gravity="center"  
  6.     android:gravity="center"  
  7.     android:clickable="true"  
  8.     android:textColor="@color/textview" >  
  9. </TextView>  

相关网址:

《Android设置TextView的Selector来控制点击的颜色》:http://kaizen.blog.51cto.com/5333370/921041

《Android中的Selector的用法》:http://blog.csdn.net/shakespeare001/article/details/7788400

三、获取当前APP所在手机的IP和MAC地址:
 调用Android的API: WifiManager

权限:

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>  

获取MAC地址:

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);  
  2.    
  3. WifiInfo info = wifi.getConnectionInfo();  
  4.    
  5. return info.getMacAddress();  

获取IP地址:

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. public static String getCurIp(WifiManager wifiManager)  
  2. {  
  3.     WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);     
  4.     WifiInfo wifiInfo = wifiManager.getConnectionInfo();     
  5.     int ipAddress = wifiInfo.getIpAddress();     
  6.         
  7.         
  8.        return ( ipAddress & 0xFF) +"."+  
  9.                ((ipAddress >> 8 ) & 0xFF) + "." +     
  10.                ((ipAddress >> 16 ) & 0xFF) + "." +     
  11.                ((ipAddress >> 24 ) & 0xFF );  
  12. }  

四、设置textview行间距
XML:

android:lineSpacingExtra
android:lineSpacingMultiplier

JAVA:

setLineSpacing(float,float)

五、android程序更换图标安装后不变解决办法 
我们有些手机安装了老版本的应用后,当应用更换图标后,在安装,发现他桌面的图标还是老图标。卸载重新安装也没有用,这个问题一般是小米的机器才会发生。其实就是有缓存,怎么解决呢?下面提供两种方案:
1.进入目录 /data/system/customized_icons 下,找到你原来的旧图标,删除即可。需要root权限
2.把当前的工程换一个包名,重新安装即可。换了包名等于是一个新的应用。旧的缓存图标就失效了







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值