1.Android开发:重写onKeyDown方法,监控返回键、菜单键和Home键
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_BACK) {
// 监控返回键
new Builder(TestActivity.this).setTitle("提示")
.setIconAttribute(android.R.attr.alertDialogIcon)
.setMessage("确定要退出吗?")
.setPositiveButton("确认", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
TestActivity.this.finish();
}})
.setNegativeButton("取消", null)
.create().show();
return false;
} else if(keyCode == KeyEvent.KEYCODE_MENU) {
// 监控菜单键
Toast.makeText(TestActivity.this, "Menu", Toast.LENGTH_SHORT).show();
return false;
}
return super.onKeyDown(keyCode, event);
}
2.EditText获取焦点,弹出键盘:
receiver.requestFocus();
InputMethodManager imm = (InputMethodManager) edit.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(0, InputMethodManager.SHOW_FORCED);
//关闭软键盘
imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
3.finished with non-zero exit value 2
Error:Execution failed for task ‘:app:dexDebug’. > com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process ‘command ‘C:\Program Files\Java\jdk1.8.0_60\bin\java.exe” finished with non-zero exit value 2
解决办法:
Adding multiDexEnabled true in gradle solved the issue for me.
defaultConfig {
applicationId "com.xyz.foo"
minSdkVersion 16
targetSdkVersion 23
versionCode 17
versionName "1.0"
// Enabling multidex support.
multiDexEnabled true
}
...
dependencies {
compile 'com.android.support:multidex:1.0.0'
}
其中 multiDexEnabled true 是关键
4.整理一下Android中的ListView
5.Android 控件的显示隐藏上下左右移动动画
其中关键是:
TranslateAnimation mHiddenAction = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f,
Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
0.0f, Animation.RELATIVE_TO_SELF, 1.0f);
TranslateAnimation这个函数是库函数,查看源码你会发现 FromXValue, ToXValue以及FromXStyle等等参数,在测试后发现这篇博客的示例代码是有效果的,左右滑动的动画昨天没有调通,精髓就是改变0.0f以及1.0f这几个From、To value的值
6.android 的getResource().getDrawable(id)已经弃用了,需要改用以下方式:
ContextCompat.getDrawable(context, R.drawable.***)
因为涉及到theme,所以如果你一定要用这个方法的话,尝试下面的代码吧:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
return resources.getDrawable(id, context.getTheme());
} else {
return resources.getDrawable(id);
}
涉及到类似的一些方法时你可以类推一下:
像
getResource().getColor(id)
//may cause exception:
// java.lang.NoSuchMethodError: No virtual method getColor
改为:
ContextCompat.getColor(context, R.color.your_color);
等等
转载署源-By-KyleCe: http://blog.csdn.net/kyleceshen/article/details/48802387