发新帖

android一些常用代码

[复制链接]
3418 0
001.判断当前横竖屏,设置横竖屏
if(this.getResources().getConfiguration().orientation ==Configuration.ORIENTATION_PORTRAIT){
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);

}
002.获取DisplayMetrics成员,获得屏幕分辨率,dp和px转换
DisplayMetrics metrics  = new DisplayMetrics();   
WindowManager WM = (WindowManager)mcontext.getSystemService(Context.WINDOW_SERVICE);
WM.getDefaultDisplay().getMetrics(metrics);
float sPixelDensity = metrics.density;//逻辑分辨率
int width = metrics.widthPixels() ;//宽
int height = metrics.heightPixels();//高
public static float dpToPixel(float dp) {
    return sPixelDensity * dp;
}
public static int dpToPixel(int dp) {
    return Math.round(dpToPixel((float) dp));
}
public static int px2dip(Context context, float pxValue) {
    final float scale = context.getResources().getDisplayMetrics().density ;
    return (int) (pxValue / scale + 0.5f) ;
}
public static int dip2px(Context context, float dpValue) {
    final float scale = context.getResources().getDisplayMetrics().density ;
    return (int) (dpValue * scale + 0.5f) ;
}
public static int meterToPixel(float meter) {
        // 1 meter = 39.37 inches, 1 inch = 160 dp.
        return Math.round(dpToPixel(meter * 39.37f * 160));
}
003.判断是否是平板(官方用法)public static boolean isTablet(Context context) {
    return (context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_LARGE;
}
004.文字根据状态更改颜色 android:textColor <selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="#53c1bd" android:state_selected="true"/>
    <item android:color="#53c1bd" android:state_focused="true"/>
    <item android:color="#53c1bd" android:state_pressed="true"/>
    <item android:color="#777777"/>
</selector>
005.按钮根据状态更改背景图片
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- Non focused states -->
    <item android:state_focused="false" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/fake_actionbar_ic_callout_normal" />
    <item android:state_focused="false" android:state_selected="true"  android:state_pressed="false" android:drawable="@drawable/fake_actionbar_ic_callout_normal" />
    <!-- Focused states -->
    <item android:state_focused="true" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/fake_actionbar_ic_callout_normal" />
    <item android:state_focused="true" android:state_selected="true"  android:state_pressed="false" android:drawable="@drawable/fake_actionbar_ic_callout_normal" />
    <!-- Pressed -->
    <!--    Non focused states -->
    <item android:state_focused="false" android:state_selected="false" android:state_pressed="true" android:drawable="@drawable/fake_actionbar_ic_callout_press" />
    <item android:state_focused="false" android:state_selected="true"  android:state_pressed="true" android:drawable="@drawable/fake_actionbar_ic_callout_press" />
    <!--    Focused states -->
    <item android:state_focused="true" android:state_selected="false" android:state_pressed="true" android:drawable="@drawable/fake_actionbar_ic_callout_press" />
    <item android:state_focused="true" android:state_selected="true"  android:state_pressed="true" android:drawable="@drawable/fake_actionbar_ic_callout_press" />
</selector>
006.带边框图片
[size=14.399999618530273px]<?xml version="1.0" encoding="UTF-8"?>
[size=14.399999618530273px]

[size=14.399999618530273px]2 <shape xmlns:android="
[size=14.399999618530273px]http://schemas.android.com/apk/res/android
[size=14.399999618530273px]">
[size=14.399999618530273px]3     <solid android:color="#EEEEEE" />
[size=14.399999618530273px]4     <stroke android:width="3dp" android:color="#EEEEEE" />
[size=14.399999618530273px]5     <corners android:radius="0dp" />
[size=14.399999618530273px]6     <padding android:left="0dp" android:top="0dp" android:right="0dp" android:bottom="0dp" />
[size=14.399999618530273px]7 </shape>
[size=14.399999618530273px]在Imageview定义页面上使用代码:
[size=14.399999618530273px][代码]xml代码:
[size=14.399999618530273px]1 ImageView
[size=14.399999618530273px]2     android:id="@+id/iv_thumbnail"
[size=14.399999618530273px]3     android:layout_height="63dp"
[size=14.399999618530273px]4     android:layout_width="63dp"
[size=14.399999618530273px]5     android:background="@drawable/bg_border1"
[size=14.399999618530273px]6     android:padding="3dp"
[size=14.399999618530273px]7     />
007.监听SD卡插拔
BroadcastReceiver mSDCardMountEventReceiver;

private void registerExternalStorageListener() {
  if (mSDCardMountEventReceiver == null) {
   mSDCardMountEventReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
     String action = intent.getAction();
     if (action.equals(Intent.ACTION_MEDIA_EJECT)) {
     } else if (action.equals(Intent.ACTION_MEDIA_MOUNTED)) {
     }
    }
   };
   IntentFilter iFilter = new IntentFilter();
   iFilter.addAction(Intent.ACTION_MEDIA_EJECT);
   iFilter.addAction(Intent.ACTION_MEDIA_MOUNTED);
   iFilter.addDataScheme("file");
   registerReceiver(mSDCardMountEventReceiver, iFilter);
  }
}
008.fragment嵌套fragment
final FragmentTransaction ft = getChildFragmentManager().beginTransaction();
  ft.add(R.id.call_log, mCallLogFragment);
  ft.commit();
009.取消标题和状态栏
this.requestWindowFeature(Window.FEATURE_NO_TITLE);     
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,   WindowManager.LayoutParams.FLAG_FULLSCREEN);  
android:theme="@android:style/Theme.NoTitleBar"


[size=14.399999618530273px]android:theme="@android:style/Theme.NoTitleBar.Fullscreen"



[size=14.399999618530273px]010.创建对话框

[size=14.399999618530273px] public void createDialog(){   
[size=14.399999618530273px]        Builder builder = new AlertDialog.Builder(this);   [size=14.399999618530273px]        
[size=14.399999618530273px]        builder.setTitle(R.string.xxx);   
[size=14.399999618530273px]        builder.setMessage(message); [size=14.399999618530273px]        
[size=14.399999618530273px]        builder.setIcon(R.drawable.ic_launcher);
[size=14.399999618530273px]        builder.setPositiveButton("确定", new DialogInterface.OnClickListener(){   [size=14.399999618530273px]         
[size=14.399999618530273px]            @Override  
[size=14.399999618530273px]            public void onClick(DialogInterface dialog, int which){   [size=14.399999618530273px]            
[size=14.399999618530273px]            }  
[size=14.399999618530273px]        }).setNegativeButton("取消", new DialogInterface.OnClickListener(){  [size=14.399999618530273px]            
[size=14.399999618530273px]            @Override  
[size=14.399999618530273px]            public void onClick(DialogInterface dialog, int which){  [size=14.399999618530273px]           
[size=14.399999618530273px]            }  
[size=14.399999618530273px]        }).create();  [size=14.399999618530273px]      
[size=14.399999618530273px]        builder.show();  [size=14.399999618530273px]        
[size=14.399999618530273px]    }  
[size=14.399999618530273px]011.判断是否联网
private void judgeNetwork() {  
        /*
         * The primary responsibilities of this class(ConnectivityManager) are
         * to:  
         * 1.Monitor network connections (Wi-Fi, GPRS, UMTS, etc.)  
         * 2.Send broadcast intents when network connectivity changes  
         * 3.Attempt to "fail over" to another network when connectivity to a network is lost
         * 4.Provide an API that allows applications to query the coarse-grained
         * or fine-grained state of the available networks
         * 千万不要忘了在manifest里面加个权限 ,粗心的朋友一定要记住:
         * <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
         */  
        ConnectivityManager conman = (ConnectivityManager) this  
                .getSystemService(Context.CONNECTIVITY_SERVICE);  

        NetworkInfo netInfo = conman.getActiveNetworkInfo();  

        if (netInfo != null && netInfo.isAvailable()) {//[size=14.399999618530273px]第二个条件也可以用[size=14.399999618530273px]netInfo.isConnected();
            Toast.makeText(this, "OK网!", Toast.LENGTH_SHORT).show();  
        } else {  
            Toast.makeText(this, "貌似断网了!", Toast.LENGTH_SHORT).show();  
        }  
    }  

012.判断SD状态
    private final static int READ_AND_WRITE = 2;
    private final static int READ_ONLY= 1;

    private final static int INVALID=0;

    public int getSDCardState(){  
        String state = Environment.getExternalStorageState();  
        if (Environment.MEDIA_MOUNTED.equals(state)) {  
            return READ_AND_WRITE ;  
        } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {  
            return READ_ONLY;  
        } else {  
            return INVALID;  
        }        
    }  




举报 使用道具

回复
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表