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解析
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中调用
<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布局文件中按钮的调用
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();
}
});
程序解析:
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)设置了Activity要显示的布局文件(\layout\main.xml)。程序中的String imei =tm.getSimSerialNumber();是读取SIM卡序列号,需要有权限,所以 在Androidmani.xml 中加入了权限
<uses-permission android:name="android.permission.READ_PHONE_STATE">
</uses-permission>
|
|