Android代码片段
StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath());
long sdAvailSize = (long)stat.getAvailableBlocks()
* (long)stat.getBlockSize();
long sdTotalSize = (long)stat.getBlockCount()
* (long)stat.getBlockSize()
格式化容量代码:
public static String formatSize(long size) {
if (size < 1024) {
return String.format("%dByte", size);
} else if (size < 1024 * 1024) {
return String.format("%.2fKb", size / 1024f);
} else if (size < 1024 * 1024 * 1024) {
return String.format("%.2fMb", size / 1024f / 1024f);
} else {
return String.format("%.2fGb", size / 1024f / 1024f / 1024f);
}
}
<!-- .2f表示的是保留三位小数的浮点数 -->
<string name="book">书名 (字符串)%1$s,作者(字符串)%2$s,编号(整数)%3$d,价格(浮点型):%4$.2f</string>
//在这个地方,应该使用
//android:textColor="@drawable/text_color"
// text_color.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="@color/click_blue_color"></item>
<item android:color="@color/blue_color"></item>
</selector>
DrawableLeft
Drawable drawable = getResources().getDrawable(R.drawable.drawable);
/// 这一步必须要做,否则不会显示.
drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
myTextview.setCompoundDrawables(drawable,null,null,null);
在values
中定义id
在values
文件夹中创建ids.xml
文件, 文件目录为:values/ids.xml
我们有时候,会在代码中定义一个组件,如TextView
,但是我们想给他设置一个Id,又不知道该设置一个
什么值比较好,就可以用上术方法来定义一个id
, 具体代码如下:
<?xml version="1.0" encoding="utf-8"?>
<values>
<item name="custom_item_id" type="id"></item>
</values>
Andorid打开或关闭软键盘
// 打开软键盘
InputMethodManager inputMethodManager =
(InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
// 关闭软键盘
InputMethodManager imm =
(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(mTextView.getWindowToken(), 0);
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
if ((System.currentTimeMillis() - mExitTime) > 2000) {
Object mHelperUtils;
Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
mExitTime = System.currentTimeMillis();
} else {
finish();
}
return true;
}
return super.onKeyDown(keyCode, event);
}
android.applicationVariants.all { variant ->
variant.outputs.each { output ->
def outputFile = output.outputFile
if (outputFile != null && outputFile.name.endsWith('.apk')) {
def fileName = outputFile.name.replace(".apk", "-${defaultConfig.versionName}-${ defaultConfig.versionCode}.apk")
output.outputFile = new File(outputFile.parent, fileName)
}
}
}
<style name="NoAnimationTheme">
<item name="android:windowAnimationStyle">@style/Animation</item>
</style>
<style name="Animation">
<item name="android:activityOpenEnterAnimation">@null</item>
<item name="android:activityOpenExitAnimation">@null</item>
<item name="android:activityCloseEnterAnimation">@null</item>
<item name="android:activityCloseExitAnimation">@null</item>
<item name="android:taskOpenEnterAnimation">@null</item>
<item name="android:taskOpenExitAnimation">@null</item>
<item name="android:taskCloseEnterAnimation">@null</item>
<item name="android:taskCloseExitAnimation">@null</item>
<item name="android:taskToFrontEnterAnimation">@null</item>
<item name="android:taskToFrontExitAnimation">@null</item>
<item name="android:taskToBackEnterAnimation">@null</item>
<item name="android:taskToBackExitAnimation">@null</item>
</style>
在使用的时候,只需将不需要动画的Activity的style
设置成这个就可以了