Android开发的小问题

本文详细解答了Android开发中常见的几个问题,包括ListView点击事件处理、软键盘影响自定义底部菜单、后台运行程序不按Home键退出、ListView中Edittext使用限制、定位自定义布局位置、设置最小heap内存大小、监听其他APK安装完成事件以及程序安装和卸载的广播接收。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一些小问题,不大!但是足够让你花上一天或者一个礼拜的时间去解决!

原创文章:转载请注明作者:geolo。和文章出处:http://blog.youkuaiyun.com/geolo/article/details/7058515

1.Android中的ListView无法点击事件

 如果ListView子视图中有Button等抢焦点的控件,那么需要在ListView的xml配置中加入android:descendantFocusability="blocksDescendants"这段话,同时,对Button等控件需要android:focusable="false"的处理。


2. 软键盘,顶起自定义的底部菜单(RelativeLayout)。

解决方法是Mainfest配置中对应的Activity加入android:windowSoftInputMode="stateVisible|adjustPan"信息


3. 不按Home键,让程序后台运行并退回Home界面!(通过Button模拟android的Home键盘) 

[html]  view plain copy print ?
  1.                             PackageManager pm = mActivity.getPackageManager();    
  2. ResolveInfo homeInfo = pm.resolveActivity(new Intent(Intent.ACTION_MAIN)  
  3. .addCategory(Intent.CATEGORY_HOME), 0);    
  4. ActivityInfo ai = homeInfo.activityInfo;    
  5. Intent startIntent = new Intent(Intent.ACTION_MAIN);    
  6. startIntent.addCategory(Intent.CATEGORY_LAUNCHER);    
  7. startIntent.setComponent(new ComponentName(ai.packageName,ai.name));  
[html]  view plain copy print ?
  1. startIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);    
[html]  view plain copy print ?
  1. mActivity.startActivity(startIntent);     
[html]  view plain copy print ?
  1. <pre>  

 

 

4.ListView中不能使用Edittext的原因。

其一: android:descendantFocusability="blocksDescendants",是该属性做的坏!该属性还会导致layout中的Edittext不能得到焦点。

其二:listview中如果用Edittext会导致Edittext内的内容在ListView自主刷新的时候,内容丢失。因此,如果数据量不是很多的情况下,可以采用Scroll+Layout方式

5. ListView或者ScrollView,定位到你指定的“自定义”layout的位置。

[html]  view plain copy print ?
  1. final int x = v.getScrollX();//获取你自定义layout的X轴,貌似这边一直为0.你应该获取layout的getLeft的位置。哈哈  
  2.                     final int y = v.getTop();//获取你自定义layout的顶部的Y轴位置  
  3.   
  4.                     mScrollView.post(new Runnable() {//是个线程刷新!  
  5.                         @Override  
  6.                         public void run() {  
  7.                             mScrollView.scrollTo(x, y);//开始定位  
  8.                         }  
  9.                     });  


6. 设置最小heap内存为6MB大小。

[html]  view plain copy print ?
  1. private final static int CWJ_HEAP_SIZE = 6* 1024* 1024 ;   
  2. VMRuntime.getRuntime().setMinimumHeapSize(CWJ_HEAP_SIZE);  

7。在自己的应用中监听其他APK安装完成事件 
 
[html]  view plain copy print ?
  1. <pre name="code" class="html"><receiver android:name=".XXReceiver">    
  2.     <intent-filter>    
  3.         <action android:name="android.intent.action.PACKAGE_ADDED"></action>    
  4.         <data android:scheme="package" /><!-- 注意!! 这句必须要加,否则接收不到BroadCast -->    
  5.     </intent-filter>    
  6. </receiver>    

String android.content.Intent.ACTION_PACKAGE_ADDED = "android.intent.action.PACKAGE_ADDED" 

Broadcast Action: A new application package has been installed on the device. The data contains the name of the package. Note that the newly installed package does not receive this broadcast. 

My include the following extras: 

    * EXTRA_UID containing the integer uid assigned to the new package. 
    * EXTRA_REPLACING is set to true if this is following an ACTION_PACKAGE_REMOVED broadcast for the same package. 

This is a protected intent that can only be sent by the system 

---------- 

当系统中新安装了一个软件或重新安装一个软件都会触发这个action的广播, 
并发送intent给接收者,发送的intent中包话新安装应用的包名、系统指定给这个应用的ID、标识是否这个广播是在ACTION_PACKAGE_REMOVED广播发送之后发送的。 

8. Android监听程序的安装和卸载

在android系统中,安装和卸载都会发送广播,当应用安装完成后系统会发android.intent.action.PACKAGE_ADDED广播。可以通过intent.getDataString()获得所安装的包名。当卸载程序时系统发android.intent.action.PACKAGE_REMOVED广播。同样intent.getDataString()获得所卸载的包名。
应用程序无法监听自己的安装与卸载,但覆盖安装可以监听到自己的android.intent.action.PACKAGE_REMOVED广播。

[html]  view plain copy print ?
  1. public class PackageReceiver extends BroadcastReceiver{  
  2. @Override  
  3.     public void onReceive(Context context, Intent intent) {          
  4.         if (intent.getAction().equals("android.intent.action.PACKAGE_ADDED")) {  
  5.             String packageName = intent.getDataString();  
  6.             Log.i("Test","---------------" + packageName);  
  7.         }  
  8.           
  9.         if (intent.getAction().equals("android.intent.action.PACKAGE_REMOVED")) {  
  10.          String packageName = intent.getDataString();  
  11.           Log.i("Test","---------------" + "PACKAGE_REMOVED" + packageName);  
  12.         }  
  13. }  
  14. }  

[html]  view plain copy print ?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest  
  3.    xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     package="com.test"  
  5.     android:versionCode="1"  
  6.     android:versionName="1.0">  
  7.     <application  
  8.      android:icon="@drawable/icon"  
  9.      android:label="测试">  
  10.         <receiver android:name=".PackageReceiver"  
  11.                   android:label="@string/app_name">  
  12.             <intent-filter>  
  13.               <action android:name="android.intent.action.PACKAGE_ADDED" />  
  14.               <action android:name="android.intent.action.PACKAGE_REMOVED" />  
  15.                <data android:scheme="package" />  
  16.             </intent-filter>  
  17.         </receiver>  
  18.     </application>  
  19.     <uses-sdk android:minSdkVersion="7" />  
  20.    <uses-permission android:name="android.permission.INTERNET" />  
  21.     <uses-permission android:name="android.permission.RESTART_PACKAGES"/>  
  22.     <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>  
  23. </manifest>   

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值