androidの常用基础应用知识总结
DisplayMetrics displaymetrics = new DisplayMetrics(); height = getResources().getDisplayMetrics().heightPixels; width = getResources().getDisplayMetrics().widthPixels; Rectf rectF = new RectF(0, 0, width, height ); //屏幕宽和高 canvas.drawBitmap(bitmap, null, rectF, null);
2. 返回键与Home键区别
back键默认行为是finish处于前台的Activity的即Activity的状态为Destroy状态为止,再次启动该Activity是从onCreate开始的(不会调用onSaveInstanceState方法)。Home键默认是stop前台的Activity即状态为onStop为止而不是Destroy,若再次启动它,会调用onSaveInstanceState方法,保持上次Activity的状态则是从OnRestart开始的---->onStart()--->onResume()。
3. android:paddingLeft与android:layout_marginLeft的区别
当按钮分别设置以上两个属性时,得到的效果是不一样的。android:layout_marginLeft="30px" 整个按钮离左边设置的内容30个像素
4 判断当前网络状态
<span style="font-size:18px;">import android.net.ConnectivityManager; import android.net.NetworkInfo; public boolean isNetworkConnected() { ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo ni = cm.getActiveNetworkInfo(); return ni != null && ni.isConnectedOrConnecting(); }</span>
5. 应用权限
6.. 设置activity 一直竖屏
<style name="Theme.NoDisplay" parent="@android:Theme"> <item name="android:windowBackground">@null</item> <item name="android:windowContentOverlay">@null</item> <item name="android:windowIsTranslucent">false</item> <item name="android:windowAnimationStyle">@null</item> <item name="android:windowDisablePreview">true</item> <item name="android:windowNoDisplay">false</item> </style>
boolean d() { String packageName = "com.iflytek.speechcloud"; List<PackageInfo> packages = getPackageManager().getInstalledPackages(0); for (int i = 0; i < packages.size(); i++) { PackageInfo packageInfo = packages.get(i); if (packageInfo.packageName.equals(packageName)) { return true; } else { continue; } } return false;<span style="background-color: inherit; font-family: 微软雅黑;">}</span>
public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { exitApp(); return true; } return super.onKeyDown(keyCode, event); }
new AlertDialog.Builder(IndexActivity.this) .setTitle(getString(R.string.exittitle)) .setMessage(getString(R.string.exit)) .setIcon(R.drawable.iconapp) .setPositiveButton(getString(R.string.ok), new DialogInterface.OnClickListener() { public void onClick( DialogInterface paramDialogInterface, int paramInt) { } }) .setNegativeButton(getString(R.string.cancel), new DialogInterface.OnClickListener() { public void onClick( DialogInterface paramDialogInterface, int paramInt) { finish(); } }).show();
15.. GirdView 添加单个item 选择上的效果
import java.util.ArrayList; import java.util.Iterator; import java.util.List; import net.sourceforge.pinyin4j.PinyinHelper; import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType; import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat; import net.sourceforge.pinyin4j.format.HanyuPinyinToneType; import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType; import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination; public class PinyinUtil { /** * @param list * @return 将集合中元素换成拼音 */ public static List<String> getPinyinList(List<String> list) { List<String> pinyinList = new ArrayList<String>(); for (Iterator<String> i = list.iterator(); i.hasNext();) { String str = (String) i.next(); try { String pinyin = getPinYin(str); pinyinList.add(pinyin); } catch (BadHanyuPinyinOutputFormatCombination e) { e.printStackTrace(); } } return pinyinList; } public static String getPinYin(String zhongwen) throws BadHanyuPinyinOutputFormatCombination { String zhongWenPinYin = ""; // 将中文变成字符数组 char[] chars = zhongwen.toCharArray(); for (int i = 0; i < chars.length; i++) { String[] pinYin = PinyinHelper.toHanyuPinyinStringArray(chars[i], getDefaultOutputFormat()); if (pinYin != null) { zhongWenPinYin += pinYin[0]; } else { zhongWenPinYin += chars[i]; } } return zhongWenPinYin; } private static HanyuPinyinOutputFormat getDefaultOutputFormat() { HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat(); format.setCaseType(HanyuPinyinCaseType.UPPERCASE);// format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);// format.setVCharType(HanyuPinyinVCharType.WITH_U_AND_COLON);// return format; } }
<activity android:name="a.h.AActivity" android:label="@string/app_name" android:theme="@style/Theme.NoDisplay" android:icon="@drawable/icon"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>
21. ScrollView 下只能有一个根布局,不能出现多个.
21. back键点击两次退出应用
@Override public boolean onKeyDown(int keyCode, KeyEvent event) { switch (keyCode) { case KeyEvent.KEYCODE_BACK: long secondTime = System.currentTimeMillis(); if (secondTime - firsttime > 2000) { // 如果两次按键时间间隔大于2秒,则不退出 toast(getResources().getString(R.string.exit)); firsttime = secondTime;// 更新firstTime return true; } else { // 两次按键小于2秒时,退出应用 finish(); } break; } return super.onKeyUp(keyCode, event); }当使用 onBackpress() 方法返回时候无效果