活动的基本用法;
AndroidManifest.xml文件中注册活动。
隐藏标题栏
在活动中使用Menu创建菜单
销毁一个活动
package com.example.activitytest; import android.app.Activity; import android.os.Bundle; 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) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE);//隐藏标题栏 setContentView(R.layout.first_layout);//加载布局 Button button1 = (Button)findViewById(R.id.button_1); button1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub //Toast.makeText(FirstActivity.this, "You clicked Button 1", Toast.LENGTH_SHORT).show(); finish();//销毁一个活动, finish和按下back键效果一样 } }); } @Override public boolean onCreateOptionsMenu(Menu menua) { // TODO Auto-generated method stub getMenuInflater().inflate(R.menu.main, menua);//菜单默认是不会显示出来的,只用按下了menu键,菜单才会在底部显示出来。 return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // TODO Auto-generated method stub switch (item.getItemId()) { case R.id.add_item: Toast.makeText(this, "You clicked Add", Toast.LENGTH_LONG).show(); break; case R.id.remove_item: Toast.makeText(this, "You clicked remove_item", Toast.LENGTH_SHORT).show(); break; default: break; } return super.onOptionsItemSelected(item); } }
<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="14" android:targetSdkVersion="18" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.activitytest.FirstActivity" android:label="This is FirstActivity01"> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity> </application> </manifest>
<?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>
时间: 2024-11-14 00:31:50