androidの常用基础应用知识总结


1,canvas.drawBitmap()使图片全屏显示 有时候我们这使用canvas.drawBitmap()时,需要使图片全屏显示,这个时候我们可以这样:

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:paddingLeft="30px" 按钮上设置的内容(例如图片)离按钮左边边界30个像素

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.应用权限

<!-- 必须申明的权限 --> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> <uses-permission android:name="android.permission.RESTART_PACKAGES"/> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/> <uses-permission android:name="android.permission.GET_TASKS" /> <uses-permission android:name="android.permission.VIBRATE"/>

6.. 设置activity 一直竖屏

<activity android:name=".MainActivity" android:screenOrientation="nosensor"></activity>
7. 设置屏幕一直亮 getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
8 . 设置进入界面加载过程,避免黑屏或者白屏现象
<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>
在主配置文件中,主函数中添加 android:theme="@style/Theme.NoDisplay"
9. 检查手机应用是否安装指定apk
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>

10. 设置屏幕无title import android.view.Window; requestWindowFeature(Window.FEATURE_NO_TITLE);

11. back 键监听
public boolean onKeyDown(int keyCode, KeyEvent event) {if (keyCode == KeyEvent.KEYCODE_BACK) {exitApp();return true;}   return super.onKeyDown(keyCode, event);}

12 .弹出对话框选项
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();

13.获取设备管理,设备标示名称 Build bd = new Build(); String model = bd.MODEL; //poineer sw590 String brand=bd.BRAND; //poineer
14. 如何防止暴力点击button (可以加个计时器,1~3秒后才可以点击第二下) private Handler mHandler =new Handler();
public void onClick(View v){
v.setEnabled(false);
mHandler.postDelay(new Runable(){
public void run(){
v.setEnabled(true);
}
}),2000);
}


15.. GirdView添加单个item 选择上的效果

点击后,图片周围出现绿色边框效果。 属性: pictureGridView. setSelector(R.drawable.menuitemshape); 然后在 drawable文件下,添加: menuitemshape.xml 文件 <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" > <!-- <solid android:color="#FF0000" /> --> <stroke android:width="2dip" android:color="#00FF00" /> <corners android:radius="10dip" /> <padding android:bottom="5dp" android:left="5dp" android:right="5dp" android:top="7dp" /> </shape>
16.屏幕边框为椭圆形,边角为弧形,不是直角 首先在布局文件中:定义一个外布局,设置: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@color/black" android:orientation="vertical" > <RelativeLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_margin="3px" android:background="@drawable/round_rect_shape" android:orientation="vertical" > 然后在drawable文件下,添加 round_rect_shape.xml <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" > <!-- Customize round rect --> <stroke android:width="2dip" android:color="#FF99CC" /> <gradient android:angle="90" android:endColor="#99CCFF" android:startColor="#99CCFF" /> <!-- #474C48 --> <corners android:radius="5dip" /> </shape>
17.图库使用 startActivity(new Intent( Intent.ACTION_VIEW, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI) .putExtra("slideshow", true).addFlags( Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP));
18.home 键使用 startActivity(new Intent(Intent.ACTION_MAIN) .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP) .addCategory(Intent.CATEGORY_HOME));
19.拼音转换工具类
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;}}
20.一个应用在桌面显示两个图标 需要在主配置文件中,<application>下。对应的activity
<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>
记住两个图标,需要两个activity,中添加这个 <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter>配置

21.ScrollView 下只能有一个根布局,不能出现多个.

21. back键点击两次退出应用

@Overridepublic 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;// 更新firstTimereturn true;} else { // 两次按键小于2秒时,退出应用finish();}break;}return super.onKeyUp(keyCode, event);}
当使用 onBackpress() 方法返回时候无效果

更多相关文章

  1. Android(安卓)AlertDialog.Builder进入和退出动画效果设置
  2. android中跳转系统自带界面
  3. android sdk manager Failed to fetch URl https://dl-ssl.googl
  4. 自定义一个dialog没有标题
  5. Android(安卓)slidingmenu详解 优化侧滑
  6. Android设置“android:clickable="false"无效。点击事件依旧触发
  7. 实现 Android(安卓)通知提示功能
  8. android保持屏幕常亮
  9. android selector 的几种状态

随机推荐

  1. Android(安卓)Multiple Screens Android(
  2. Android ImageView控件的MaxWidth、MaxHe
  3. 实现Android计时与倒计时方法
  4. Android Studio 获取证书指纹SHA1
  5. 《Google+Android开发入门与实践》书中go
  6. 高级控件1
  7. Android核心库
  8. Android经典蓝牙和Ble蓝牙的对比
  9. Android(安卓)开发:第一日——明白Android
  10. Android(安卓)基础控件 TextView