小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

Android簡單開發(fā)教程(二)

 小飛苑 2011-04-16
3.Android 應用解析
資源文件解析:
AndroidManifest.xml 樣例,代碼如下
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas./apk/res/android"
package="com.test.AndroidTMS.views"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/tmslogo" android:label="@string/app_name">
<activity android:name=".AndroidTMS"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
<uses-sdk android:minSdkVersion="5" />
</manifest>
AndroidManifest.xml解析
manifest
根節(jié)點,描述了package中所有的內(nèi)容
xmlns:android
包含命名空間的聲明。xmlns:android=http://schemas.
/apk/res/android,使得Android中各種標準屬性能在文件中使用,提供了大部分元素中的數(shù)據(jù)
Package
聲明應用程序包
application
包含packageapplication級別組件聲
明的根節(jié)點。此元素也可包application的一些全局和默認的屬性,如標簽、icon、主題、必要的權(quán)限,等等。一個manifest能包含零個或一個此元素(不能大于一個)
android:icon
應用程序圖標
android:label
應用程序名字
Activity
用來與用戶交互的主要工具。Activity是用戶打開一個應用程序的初始頁面,大部分
被使用到的其他頁面也由不同activity所實現(xiàn),并聲明在另外的activity標記中。
注意,每一個activity必須有一個<activity>標記對應,無論它給外部使用或是只用于自己的package中。如果一個activity沒有對應的標記,你將不能運行它。另外,為了支持運行時查找Activity,可包含一個或多個<intent-filter>元素來描述activity所支持的操作
android:name
這個activity對應的類別
android:label
這個activity的代號
intent-filter
聲明了指定的一組組件支持的Intent值,從而形成了IntentFilter。除了能在此元素下指定不同類型的值,屬性也能放在這里來描
述一個操作所需的唯一的標簽、icon和其他信息
action
組件支持的Intent action
category
組件支持的Intent Category。這里指定
了應用程序默認啟動的activity
uses-permission
應用需要的權(quán)限應當在此處申請,所申請的權(quán)限應當被系統(tǒng)或某個應用所定義,否則視為無效申請。同時,使用權(quán)限的申請需要遵循權(quán)限授予條件,非platform認證的應用無法申請高級權(quán)限。
uses-sdk
該應用程序所使用的sdk版本相關(guān)
Strings.xml解析
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, AndroidTMS!</string>
<string name="app_name">AndroidTMS</string>
</resources>
這個文件很簡單,就定義了兩個字符串資源,與R.java中對應的索引,
public static final class string {
public static final int app_name=0x7f040001;
public static final int hello=0x7f040000;
}
String資源的使用
Resources r = this.getContext().getResources();
String appname= ((String) r.getString(R.string.app_name));
String hello= ((String) r.getString(R.string.hello))
;
AndroidManifest.xml中調(diào)用
<activity android:name=".AndroidTMS"
android:label="@string/app_name">
項目的布局文件layout中資源文件的解析
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas./apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:orientation="vertical" android:gravity="center_vertical">
<TextView android:id="@+id/username_view"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:layout_marginLeft="60dip" android:layout_marginRight="30dip"
android:text="用戶信息" android:gravity="center_horizontal"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView android:id="@+id/dataInfo" android:layout_marginRight="10dip"
android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="30dip"></TextView>
<Button android:text="退出程序" android:id="@+id/btnExit"
android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>
R.java中對應的索引
public static final class drawable {
public static final int icon=0x7f020000;
}
public static final class id {
public static final int btnExit=0x7f050002;
public static final int dataInfo=0x7f050001;
public static final int username_view=0x7f050000;
}
public static final class layout {
public static final int main=0x7f030000;
}
layout布局文件的加載
setContentView(R.layout.user_info);
layout布局文件中按鈕的調(diào)用
Button btn=(Button)findViewById(R.id.btnExit);
按鈕的監(jiān)聽事件,當用戶點擊按鈕的時候詢問用戶是否要退出改程序,如果選擇是,退出程序,選擇否不執(zhí)行任何操作
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
AlertDialog dlg = new AlertDialog.Builder(ArtistsActivity.this)
.setTitle("Login Exit").setMessage("您要退出程序??")
.setPositiveButton("確定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
finish();
}
}) .setNegativeButton("取消", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
}).create();
dlg.show();
}
});
程序解析:
public class AndroidTMS extends Activity {
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tv=(TextView) findViewById(R.id.dataInfo);
TelephonyManager tm = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
String imei =tm.getSimSerialNumber();
tv.setText(imei+”sim卡號”);
Button btn=(Button) findViewById(R.id.btnExit);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
AlertDialog dlg = new AlertDialog.Builder(ArtistsActivity.this)
.setTitle("Login Exit").setMessage("您要退出程序??")
.setPositiveButton("確定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
finish();
}
}) .setNegativeButton("取消", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
}).create();
dlg.show();
}
});
}
}
主程序AndroidTMS類繼承自TabActivity類,重寫了void onCreate(Bundle savedInstanceState)方法。在onCreate方法中通setContentView(R.layout.main)設(shè)置了Activity要顯示的布局文件(\layout\main.xml)。程序中的String imei =tm.getSimSerialNumber();是讀取SIM卡序列號,需要有權(quán)限,所以 在Androidmani.xml 中加入了權(quán)限
<uses-permission android:name="android.permission.READ_PHONE_STATE">
</uses-permission>

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多