Android -- 经验分享(三)

目录                                                                                   

  1. 获取系统版本号
  2. 获取系统信息
  3. 获取安装路径和已安装程序列表
  4. 获取图片、应用名、包名
  5. 解决listview上Item上有按钮时item本身不能点击的问题
  6. 不让文本框输入中文
  7. 获取屏幕宽高
  8. 将TabWidget显示在屏幕下方
  9. 禁止软键盘弹出
  10. 调用Android其他Context的Activity
  11. 程序中安装apk
  12. 退出程序时清除通知中信息
  13. 阴影字体设置
  14. 输入框不挤压activity布局

获取系统版本号                                                                      

PackageInfo info = this.getPackageManager().getPackageInfo(this.getPackageName(), 0);  
int versionCode=nfo.versionCode  
string versionName=info.versionNam

获取系统信息                                                                         

String archiveFilePath="sdcard/download/Law.apk";//安装包路径  
PackageManager pm = getPackageManager();   
PackageInfo info = pm.getPackageArchiveInfo(archiveFilePath, PackageManager.GET_ACTIVITIES);   
if(info != null){   
ApplicationInfo appInfo = info.applicationInfo;   
String appName = pm.getApplicationLabel(appInfo).toString();   
String packageName = appInfo.packageName; //得到安装包名称  
String version=info.versionName; //得到版本信息   
Toast.makeText(test4.this, "packageName:"+packageName+";version:"+version, Toast.LENGTH_LONG).show();  
Drawable icon = pm.getApplicationIcon(appInfo);//得到图标信息  
TextView tv = (TextView)findViewById(R.id.tv); //显示图标  
tv.setBackgroundDrawable(icon);

获取安装路径和已安装程序列表                                                     

//(1)android中获取当前程序路径  
getApplicationContext().getFilesDir().getAbsolutePath()  
//(2)android取已安装的程序列表  
List<PackageInfo> packageInfoList = getPackageManager().getInstalledPackages(0);

获取图片、应用名、包名                                                             

PackageManager pManager = MessageSendActivity.this.getPackageManager();   
List<PackageInfo> appList = Utils.getAllApps(MessageSendActivity.this);   
     for(int i=0;i<appList.size();i++) {   
         PackageInfo pinfo = appList.get(i);   
         ShareItemInfo shareItem = new ShareItemInfo();   
         //set Icon   
         shareItem.setIcon(pManager.getApplicationIcon(pinfo.applicationInfo));   
         //set Application Name shareItem.setLabel(pManager.getApplicationLabel(pinfo.applicationInfo).toString());   
        //set Package Name shareItem.setPackageName(pinfo.applicationInfo.packageName);   
}

解决listview上Item上有按钮时item本身不能点击的问题                           

//1. 在item试图上面添加代码:
android:descendantFocusability="blocksDescendants"  
//2.在listview里添加代码 
android:focusable="true"

不让文本框输入中文                                                                  

android:digits="1234567890qwertyuiopasdfghjklzxcvbnm`-=[]\;,./~!@#$%^*()_+}{:?&<>"'"  
<!--这样就不会输入中文了-->

获取屏幕宽高                                                                         

DisplayMetrics displayMetrics = new DisplayMetrics();   
this.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);   
int height = displayMetrics.heightPixels;   
int width = displayMetrics.widthPixels;

将TabWidget显示在屏幕下方                                                        

<!---设置TabWidget的属性-->
android:layout_alignParentBottom="true"

为了让tabHost显示在下方,要将RadioGroup的layout_gravity设置为bottom,再将FrameLayout的 layout_weight设置为1,这样就可以将RadioGroup撑到最下方。style="@style/main_tab_bottom"里面定义了样式文件

禁止软键盘弹出                                                                       

EditText有焦点(focusable为true)阻止输入法弹出

editText.setInputType(InputType.TYPE_NULL); // 关闭软键盘 当EidtText无焦点(focusable=false)时阻止输入法弹出
InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);   
imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);

在xml文件中EditText标签有一个属性android:editable="false"和android:numeric="integer"
android:numeric="integer"表示只允许输入数字,此属性可以限制用户只能输入数字内容。
android:editable表示是否可以输入内容TRUE表示可以输入,false表示不允许输入内容;
当为android:editable="false"时,点击输入框,虚拟键盘是显示不出来的,不过当设置了android:editable=""属性时,不管是false还是true,在其后加入android:numeric="integer"属性时,是可以输入数字内容了

调用Android其他Context的Activity                                                 

Context c = createPackageContext("chroya.demo", Context.CONTEXT_INCLUDE_CODE | Context.CONTEXT_IGNORE_SECURITY);  
//载入这个类  
Class clazz = c.getClassLoader().loadClass("chroya.demo.Main");  
//新建一个实例  
Object owner = clazz.newInstance();  
//获取print方法,传入参数并执行  
Object obj = clazz.getMethod("print", String.class).invoke(owner, "Hello");

这个方法有两个参数:
1、packageName  包名,要得到Context的包名
2、flags  标志位,有CONTEXT_INCLUDE_CODE和CONTEXT_IGNORE_SECURITY两个选项。 CONTEXT_INCLUDE_CODE的意思是包括代码,也就是说可以执行这个包里面的代码。CONTEXT_IGNORE_SECURITY的意思 是忽略安全警告,如果不加这个标志的话,有些功能是用不了的,会出现安全警告。

程序中安装apk                                                                       

Intent intent = new Intent();             
       intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
    intent.setAction(android.content.Intent.ACTION_VIEW);  
    intent.setDataAndType(Uri.fromFile(“APK”),"application/vnd.android.package-archive");  
    startActivity(intent);

退出程序时清除通知中信息                                                          

NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);  
nm.cancelAll();

阴影字体设置                                                                         

<TextView  android:id="@+id/tvText1"   
 android:layout_width="wrap_content"   
 android:layout_height="wrap_content"   
 android:text="text1"   
 android:textSize="30sp"   
 android:textStyle="bold"   
 android:textColor="#FFFFFF"   
 android:shadowColor="#ff0000ff"  
 android:shadowDx="5"  
 android:shadowDy="5"       
 android:shadowRadius="10"/>

android:shadowColor 阴影颜色

android:shadowDx 阴影的水平偏移量

android:shadowDy 阴影的垂直偏移量

android:shadowRadius 阴影的范围

输入框不挤压activity布局                                                            

在manifest文件activity下加:

android:windowSoftInputMode="adjustPan"

我是天王盖地虎的分割线                                                             

posted @ 2014-08-26 23:59  我爱物联网  阅读(870)  评论(0编辑  收藏  举报
AmazingCounters.com