1.ActivityTest注册Activity
<span style="font-size:18px;"><manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.activitytest"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<!-- 注册activity -->
<activity
android:name=".FirstActivity"
android:label="this is FirstActivity">
<intent-filter>
<!-- 表示这个项目的主活动,手机上点击应用图标,首先启动的就是这个活动-->
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest></span>
2.first_layout.xml布局
<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<!--
android:id 定义一个唯一的标识符
android:layout_width 指定了当前元素的宽度,match_parent:表示让当前元素和父元素一样宽
android:layout_height 指定了当前元素的高度,wrap_content表示当前元素的高度只要能刚好包含里面的内容就行
android:text:指定了显示的内容
-->
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/buttonName1"
/>
<Button
android:id="@+id/back"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Back"
/>
</LinearLayout></span>
3.main.xml,menu设置
<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/add_item"
android:title="Add"
/>
<item
android:id="@+id/remove_item"
android:title="Remove"
/>
</menu></span>
4.string.xml值设定
<span style="font-size:18px;"><resources>
<string name="app_name">ActivityTest</string>
<string name="buttonName1">Button 1</string>
</resources></span>
5.FirstActivity实现,按钮,菜单,销毁
<span style="font-size:18px;">package com.example.activitytest;
import com.example.activitytest.R;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.Toast;
public class FirstActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//不在活动中显示标题栏,注意这句代码一定要在setContentView()之前执行。
requestWindowFeature(Window.FEATURE_NO_TITLE);
//在活动中加载这个布局
setContentView(R.layout.first_layout);
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(FirstActivity.this, "You clicked Button 1", Toast.LENGTH_SHORT).show();
}
});
Button backButton = (Button) findViewById(R.id.back);
backButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Log.d("back", "销毁");
finish();
}
});
}
/**
* R.menu.main:表示通过哪一个资源文件来创建菜单
* 第二个参数用于指定添加到哪一个menu中
* 返回true显示
*
* */
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
//获取哪一个菜单项
switch (item.getItemId()) {
case R.id.add_item:
Toast.makeText(this, "you click Add", Toast.LENGTH_SHORT).show();
break;
case R.id.remove_item:
Toast.makeText(this, "you click Remove", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
return true;
}
}</span>
源代码:http://download.csdn.net/detail/wangxuewei111/8540687