Android開發的小問題

來源:互聯網
上載者:User

       一些小問題,不大!但是足夠讓你花上一天或者一個禮拜的時間去解決!

原創文章:轉載請註明作者:geolo。和文章出處:http://itmanito.com/geolo/?p=381.Android中的ListView無法點擊事件

     如果ListView子視圖中有Button等搶焦點的控制項,那麼需要在ListView的xml配置中加入android:descendantFocusability="blocksDescendants"這段話,同時,對Button等控制項需要android:focusable="false"的處理。

    如果,子視圖item是繼承LinearLayout類,用inflate來嵌套的,那麼裡面的button等設定了監聽是不能被分發出去的。做法是每個子item有個baseClass,這個baseClass有個讓getVeiw()調用的registerWidgetListening()方法,registerWidgetListening()方法是自訂的咯。  意思就是,要讓這種子item的button設定的監聽能夠分發到,那麼需要想辦法,讓子item裡的button在getview的時候去設定監聽。

2. 軟鍵盤,頂起自訂的底部菜單(RelativeLayout)。

解決方案是Mainfest配置中對應的Activity加入android:windowSoftInputMode="stateVisible|adjustPan"資訊

3. 設定軟鍵盤showSoftInput()方法,不能彈出軟鍵盤的問題。 因為有些組件會去搶EditText組件的焦點,你雖然點觸了EditText但是很快又失去焦點,就不能彈出軟鍵盤了。  也可以知道一個事實:  軟鍵盤的彈出與否取決於EditText是否有焦點。
editText.setFocusable(true);  editText.setFocusableInTouchMode(true);  editText.requestFocus();  

讓你的EditText有焦點吧!!!!4. 不按Home鍵,讓程式後台運行並退回Home介面!(通過Button類比android的Home鍵盤) 

                PackageManager pm = mActivity.getPackageManager();                  ResolveInfo homeInfo = pm.resolveActivity(new Intent(Intent.ACTION_MAIN)                .addCategory(Intent.CATEGORY_HOME), 0);                  ActivityInfo ai = homeInfo.activityInfo;                  Intent startIntent = new Intent(Intent.ACTION_MAIN);                  startIntent.addCategory(Intent.CATEGORY_LAUNCHER);                  startIntent.setComponent(new ComponentName(ai.packageName,ai.name));                startIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);                  mActivity.startActivity(startIntent);
5.ListView中不能使用Edittext的原因。

其一: android:descendantFocusability="blocksDescendants",是該屬性做的壞!該屬性還會導致layout中的Edittext不能得到焦點。

其二:listview中如果用Edittext會導致Edittext內的內容在ListView自主重新整理的時候,內容丟失。因此,如果資料量不是很多的情況下,可以採用Scroll+Layout方式

6. ListView或者ScrollView,定位到你指定的“自訂”layout的位置。
                    final int x = v.getScrollX();//擷取你自訂layout的X軸,貌似這邊一直為0.你應該擷取layout的getLeft的位置。哈哈                    final int y = v.getTop();//擷取你自訂layout的頂部的Y軸位置                    mScrollView.post(new Runnable() {//是個線程重新整理!                        @Override                        public void run() {                            mScrollView.scrollTo(x, y);//開始定位                        }                    });
7. 設定最小heap記憶體為6MB大小。

private final static int CWJ_HEAP_SIZE = 6* 1024* 1024 ; VMRuntime.getRuntime().setMinimumHeapSize(CWJ_HEAP_SIZE);

8.在自己的應用中監聽其他APK安裝完成事件 

<receiver android:name=".XXReceiver">      <intent-filter>          <action android:name="android.intent.action.PACKAGE_ADDED"></action>          <data android:scheme="package" /><!-- 注意!! 這句必須要加,否則接收不到BroadCast -->      </intent-filter>  </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廣播發送之後發送的。 

9. Android監聽程式的安裝和卸載

       在android系統中,安裝和卸載都會發送廣播,當應用安裝完成後系統會發android.intent.action.PACKAGE_ADDED廣播。可以通過intent.getDataString()獲得所安裝的包名。當卸載程式時系統發android.intent.action.PACKAGE_REMOVED廣播。同樣intent.getDataString()獲得所卸載的包名。
應用程式無法監聽自己的安裝與卸載,但覆蓋安裝可以監聽到自己的android.intent.action.PACKAGE_REMOVED廣播。

public class PackageReceiver extends BroadcastReceiver{@Override    public void onReceive(Context context, Intent intent) {                if (intent.getAction().equals("android.intent.action.PACKAGE_ADDED")) {            String packageName = intent.getDataString();            Log.i("Test","---------------" + packageName);        }                if (intent.getAction().equals("android.intent.action.PACKAGE_REMOVED")) {         String packageName = intent.getDataString();          Log.i("Test","---------------" + "PACKAGE_REMOVED" + packageName);        }}}

<?xml version="1.0" encoding="utf-8"?><manifest   xmlns:android="http://schemas.android.com/apk/res/android"    package="com.test"    android:versionCode="1"    android:versionName="1.0">    <application     android:icon="@drawable/icon"     android:label="測試">        <receiver android:name=".PackageReceiver"                  android:label="@string/app_name">            <intent-filter>              <action android:name="android.intent.action.PACKAGE_ADDED" />              <action android:name="android.intent.action.PACKAGE_REMOVED" />               <data android:scheme="package" />            </intent-filter>        </receiver>    </application>    <uses-sdk android:minSdkVersion="7" />   <uses-permission android:name="android.permission.INTERNET" />    <uses-permission android:name="android.permission.RESTART_PACKAGES"/>    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/></manifest>

10.背景圖片平鋪1. 在res/drawable的檔案夾中建立一個bg_repeat.xml檔案。2. 檔案代碼為:

<?xml version ="1.0" encoding ="utf-8" ?><bitmap xmlns:android ="http://schemas.android.com/apk/res/android"   android:src ="@drawable/bg"   android:tileMode ="repeat" />

3. 其中bg就是單個圖片檔案。

4. 調用的時候,調用這個bg_repeat.xml --->android:background="@drawable/bg_repeat"

 11. Android view中的requestLayout和invalidate方法

    requestLayout:當view確定自身已經不再適合現有的地區時,該view本身調用這個方法要求parent view重新調用他的onMeasure onLayout來對重新設定自己位置。
 特別的,當一個View需要parent view重新調用他的onMeasure onLayout來對重新設定自己位置時,特別是當你修改了view的layoutparameter,它的值還沒能應用到view上,調用這個方法就可以了,但是要注意parent view的onLayout是怎麼實現的

12. 關於SpannableString在記憶體容量小的手機上出現的記憶體、堆疊溢位的問題!
              SpannableString mSpannableString = new SpannableString("abcdefg/鬥眼");//崩潰      SpannableString mSpannableString = new SpannableString("abcdefg鬥眼");//崩潰      SpannableString mSpannableString = new SpannableString("abcdefg//");//不崩潰      SpannableString mSpannableString = new SpannableString("abcdefg/");//不崩潰      SpannableString mSpannableString = new SpannableString("abcdefg");//不崩潰

可以總結出,SpannableString建立漢字的時候需要比較大的記憶體容量,這個問題,需要深究思考!

13.PoPWindow無法自動化佈建高度寬度的問題!

 每次設定布局以後,還需要強制設定高度和寬度!

                        //mRecordPopWindow.setContentView(recordingView);//mRecordPopWindow.setWidth(50);//mRecordPopWindow.setHeight(50);

如果有背景圖,可以用

mRecordPopWindow.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.szrs_pls_talk));
14.關於zygote總結:zygote是在Android系統中建立Java世界的Apsara Distributed File System,它建立了第一個Java虛擬機器,同時它又是女媧,成功地繁殖了framework的核心systemserver進程,回顧一下zygote建立Java世界的步驟:
第一天:建立AppRuntime對象,並調用它的start。此後的活動則由AppRuntime來控制。
第二天:調用startVm建立Java虛擬機器,然後調用startReg來註冊JNI函數。
第三天:通過JNI調用com.android.internal.os.ZygoteInit類的函數,從此進入了Java世界。而在這個世界開創的時候,什麼東西都沒有。
第四天: 調用registerZygoteSocket函數,通過這個函數建立了socket,使之能響應子孫後代的請求。同時zygote調用preloadClass和preloadResources,為Java世界添磚加瓦。
第五天:zygote覺得自己的工作壓力太大,便通過調用startSystemServer分裂了一個進程——systemserver來為java世界服務。
第六天:zygote完成了Java世界的初創工作,它已經很滿足了。下一步該做的就是調用runSelectLoopMode後,便睡眠了。
以後的日子,zygote隨時守護在我們的周圍,當接收到子孫後代的請求時,它會隨時醒來,為它們工作。所以zygote可以稱之為Android世界的“Apsara Distributed File System_女媧”。 

15. 關於SSL的搭建:

android/ios上只要在socket上根據SSL的協議規劃,而服務端要特殊配置下認證就可以。

16. Android TextView 展開全文,收縮簡文的方式:轉:http://www.chengxuyuans.com/Android/55033.html
有人可能要說,萬一我想初始幾行呢?放心,一樣可以。上代碼:在xml檔案中設定了如下屬性:android:lines="4"      //我初始化的4行  Activity那邊代碼一樣,只改一點 if(flag){        flag = false;         tv.setEllipsize(null); // 展開         tv.setSingleLine(flag);        }else{          flag = true;          tv.setEllipsize(TextUtils.TruncateAt.END); // 收縮    } 設定setSingleLine的目的是為了取消lines==“4”這個固定值。

17. 動態設定全屏和隱藏狀態列

//設定全屏  WindowManager.LayoutParams attrs = getWindow().getAttributes();     attrs.flags |= WindowManager.LayoutParams.FLAG_FULLSCREEN;     getWindow().setAttributes(attrs);     getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);  //修改window的LayoutParams參數,然後加上FLAG_LAYOUT_NO_LIMITS標誌,就OK了。window會自動重新布局,呈現全屏的狀態。
//顯示狀態列,取消全屏final WindowManager.LayoutParams attrs = getWindow().getAttributes();attrs.flags &= (~WindowManager.LayoutParams.FLAG_FULLSCREEN);getWindow().setAttributes(attrs);getWindow().clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
17. WebView隱藏滑動條/捲軸

m_webView.setHorizontalScrollBarEnabled(false);
m_webView.setHorizontalScrollbarOverlay(false);
m_webView.setVerticalScrollBarEnabled(false);
m_webView.setVerticalScrollbarOverlay(false);
m_webView.setScrollbarFadingEnabled(false);
m_webView.getSettings().setBuiltInZoomControls(false);

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.