導航:首頁 > 編程語言 > android代碼實例

android代碼實例

發布時間:2021-10-25 06:40:46

㈠ 如何在Android上編寫高效的java代碼

1、避免建立對象
世界上沒有免費的對象。雖然GC為每個線程都建立了臨時對象池,可以使創建對象的代價變得小一些,但是分配內存永遠都比不分配內存的代價大。如果在用戶界面循環中分配對象內存,就會引發周期性的垃圾回收, 除非必要,應盡量避免盡力對象的實例。
2、使用本地方法
在處理字串的時候,不要吝惜使用String.indexOf(), String.lastIndexOf()等特殊實現的方法。這些方法都是使用C/C++實現的,比起Java循環快10到100倍。
但也並非要完全使用本地方法,調用本地方法的代價要高於調用解釋方法。所以如果可以避免,就不應使用本地方法去做一些並不復雜的運算。
3、選擇虛類而不是介面

4、用靜態方法比虛方法好
如果不需要訪問一個對象的成員變數,那麼請把方法聲明成static。虛方法執行的更快,因為它可以被直接調用而不需要一個虛函數表。另外你也可以通過聲明體現出這個函數的調用不會改變對象的狀態。
5、不用getter和setter
在很多本地語言如C++中,都會使用getter(比如:i = getCount())來避免直接訪問成員變數(i = mCount)。在C++中這是一個非常好的習慣,因為編譯器能夠內聯訪問,如果你需要約束或調試變數,你可以在任何時候添加代碼。在Android上,這就不是個好主意了。虛方法的開銷比直接訪問成員變數大得多。在通用的介面定義中,可以依照OO的方式定義getters和setters,但是在一般的類中,你應該直接訪問變數。
6、將成員變數緩存到本地
訪問成員變數比訪問本地變數慢得多,下面一段代碼:
for (int i = 0; i < this.mCount; i++)mpItem(this.mItems[i]);你應該寫成:int count = this.mCount;Item[] items = this.mItems;for (int i = 0; i < count; i++)mpItems(items[i]);(顯示的使用」this」是為了表明這些是成員變數)
另一個相似的原則是:永遠不要在for的第二個條件中調用任何方法。如下面方法所示,在每次循環的時候都會調用getCount()方法,這樣做比你在一個int先把結果保存起來開銷大很多。
for (int i = 0; i < this.getCount();i++)
mpItems(this.getItem(i));同樣如果你要多次訪問一個變數,也最好先為它建立一個本地變數
7、使用常量

將一個方法或類聲明為」final」不會帶來性能的提升,但是會幫助編譯器優化代碼。舉例說,如果編譯器知道一個」getter」方法不會被重載,那麼編譯器會對其採用內聯調用。
也可以將本地變數聲明為」final」,同樣,這也不會帶來性能的提升。使用」final」只能使本地變數看起來更清晰些

㈡ 求基於Android校園開發的實例,詳細代碼及過程,謝謝了!!!

把你問的具體一點,你是想要做安卓開發嗎? 開發什麼軟體?什麼是校園開發的實例

㈢ android靜態方法和實例方法的區別

靜態方法的調用,是類名後直接加靜態方法即可。實例方法必須將類實例化後,用實例調用該實例方法。
靜態方法,只能調用類中的其他靜態屬性和靜態方法,不能調用類中的非靜態屬性和非靜態方法。
實例方法,可以調用靜態屬性和靜態方法,也可以調用實例屬性和實例方法。
靜態方法是屬於類的必須由類來調用,實例方法是屬於實例的必須實例化類後,用類的實例調用。

㈣ 有沒有什麼好的android 實例開發的源代碼網站

github.com上有很多,想找那種類型的去這個網站上搜索就行,github主要是代碼託管,好多公開的項目,都可以在上面找到源碼

㈤ 安卓開發 有個例子不是很明白

這是安卓給控制項綁定事件的基本寫法,也是最簡單的方法,如果你控制項的事件比較少,可以直接這么寫,但是如果有很多個按鈕,你全寫一遍new OnclickListener(){xxxxx};這樣很容易出現代碼冗餘,就可以讓activity實現OnclickListener介面,重新Onclick事件,在裡面做switch判斷,button1.setOnclickListener(this);就可以這樣寫了。

㈥ android 代碼布局簡單的例子

android LinearLayout 布局實例代碼,參考如下:
<?xml version="1.0" encoding="utf-8"?>
<!--
<LinearLayout>
線性版面配置,在這個標簽中,所有元件都是按由上到下的排隊排成的
-->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!-- android:orientation="vertical" 表示豎直方式對齊
android:orientation="horizontal"表示水平方式對齊
android:layout_width="fill_parent"定義當前視圖在屏幕
可以消費的寬度,fill_parent即填充整個屏幕。
android:layout_height="wrap_content":隨著文字欄位的不同
而改變這個視圖的寬度或者高度。有點自動設置框度或者高度的意思 。

-->
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
<TextView
android:text="red"
android:gravity="center_horizontal"
android:background="#aa0000"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"/>

<TextView
android:text="green"
android:gravity="center_horizontal"
android:background="#00aa00"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"/>

<TextView
android:text="blue"
android:gravity="center_horizontal"
android:background="#0000aa"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"/>

<TextView
android:text="yellow"
android:gravity="center_horizontal"
android:background="#aaaa00"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"/>

</LinearLayout>

<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="2">

<TextView
android:text="row one"
android:textSize="15pt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>

<TextView
android:text="row two"
android:textSize="15pt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>

<TextView
android:text="row three"
android:textSize="15pt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>

<TextView
android:text="row four"
android:textSize="15pt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>

</LinearLayout>

</LinearLayout>

Java代碼 Views.java
package com.cn.view;

import android.app.Activity;
import android.os.Bundle;

public class Views extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}

㈦ 如何在編寫android中的控制項 一個窗口 四個基本按鈕的 求代碼示例

在Activity中重寫下onCreateOptionsMenu方法就OK了
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
MenuItem item1=menu.add(1, 1, 1, "新建");
MenuItem item2=menu.add(1, 2, 2, "打開");
MenuItem item3=menu.add(2, 3, 3, "刪除");
MenuItem item4=menu.add(2, 4, 4, "退出");
item1.setIcon(R.drawable.btn_check_off_disable_focused);
item2.setIcon(R.drawable.btn_check_off_pressed);
item3.setIcon(R.drawable.btn_check_off_selected);
item4.setIcon(R.drawable.btn_check_on_disable_focused);
return true;
}

㈧ Android實例問題 請看 實例化了DButilImpl實現類 調用了實現類裡面的插入信息的方法

你應該給你的BtnAdd.setOnClickListener(listener);
這個寫在你的findView的下面,別寫事件裡面

然後在你的listener裡面寫

switch (v.getId()) {
case R.id.BtnAdd:
bit.insert(user);

break;

㈨ 急求android中SQLite樣例代碼

嘛~大致就是 SQLiteDatabase Mysqlite = this.openOrCreateDatabase("資料庫名.db",MODE_PRIVATE,null) 這就創建了資料庫對象
插入是 Mysqlite.execSql("insert into ....") 或者
ContentValues cv = new ContentValues();
cv.put(欄位名,欄位值);
然後 Mysqlite.insert(表名,null,cv);
delete是一樣的
查詢復雜一點,必須用游標實現 我具體給你一個我自己寫的一個封裝好的資料庫類吧,這個裡面用到了SQLiteOpenHelper這個類,你查閱下相關資料
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class SQLiteManager {
Context mContext;
DataHelper helper;
SQLiteDatabase Mysqlite;
public static String DataName = "MyData.db";
public static String t_Name = "table3";
public static String Id = "_id";
public static String v_Name = "Name";
public static int DataVersion = 1;
public static final String Table_CREATETABLE = "CREATE TABLE table3(_id INTEGER PRIMARY KEY autoincrement,Name TEXT)";

private class DataHelper extends SQLiteOpenHelper{

public DataHelper(Context context) {
super(context, DataName, null,DataVersion);
// TODO Auto-generated constructor stub
}

@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(Table_CREATETABLE);

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS notes");
onCreate(db);

}

}
public SQLiteManager(Context context){
mContext = context;
}
public void open(){
helper = new DataHelper(mContext);
Mysqlite = helper.getWritableDatabase();

}
public void close(){
helper.close();
}
public void Insert(String values){
ContentValues cv = new ContentValues();
cv.put(v_Name,values);
Mysqlite.insert(t_Name, Id,cv);
}
/* 通過Cursor查詢所有數據 */
public Cursor fetchAllData()
{
return Mysqlite.query(t_Name, new String[] { Id, v_Name}, null, null, null, null, null);

}
public Cursor fetchData(int RowId){
return Mysqlite.query(true,t_Name, new String[] { Id, v_Name}, "_id="+RowId, null, null, null,null,null);
}

㈩ 誰有android應用程序開發實例代碼或者在哪兒可以下載到完整的代碼

你要開發Android程序,必需下載SDK,而SDK裡面就自帶了大量的DEMO,你建工程的時候就可以看到,或者到SDK目錄下的samples文件裡面找,都按平台版本分好類了的,這都是官方的DEMO代碼。

閱讀全文

與android代碼實例相關的資料

熱點內容
台灣古裝三極有哪些 瀏覽:78
有迅雷鏈接怎樣找到網址 瀏覽:305
《天欲》電影未刪減版 瀏覽:818
外圓雙弧怎麼編程 瀏覽:340
根據文件內容計算sha值 瀏覽:879
李采潭10個故事的電影 瀏覽:195
什麼app競彩足球 瀏覽:954
女配寵妃升級空間 瀏覽:684
深田詠美電影孕婦日本 瀏覽:452
pythonwriteexcel文件 瀏覽:794
有奶電影 瀏覽:646
百度移動端排名工具 瀏覽:938
安卓安裝程序不見了 瀏覽:251
3d9中文精簡版找不到文件 瀏覽:839
手機能關聯哪些app 瀏覽:423
電影播放量排行榜在哪裡看 瀏覽:717
網路銷售辦公圖片素材 瀏覽:390
ps肉色代碼 瀏覽:258
快穿以肉為主 瀏覽:376

友情鏈接