MainActivity
package com.sample.t2014722;import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
if (id == R.id.action_search) {
Log.d("ACTION", "Search Pressed");
return true;
}
if(id == R.id.action_share){
Log.d("ACTION","Share Pressed");
return true;
}
return super.onOptionsItemSelected(item);
}
}
Menu
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.sample.t2014722.MainActivity" >
<item android:id="@+id/action_share"
android:icon="@drawable/ic_launcher"
android:title="@string/action_share"
android:showAsAction="ifRoom" />
<item android:id="@+id/action_search"
android:icon="@drawable/ic_launcher"
android:title="@string/action_search"
android:showAsAction="ifRoom" />
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:showAsAction="never"
android:title="@string/action_settings"/>
</menu>
String
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<string name="app_name">T2014722</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
<string name="action_search">Search</string>
<string name="action_share">Share</string>
</resources>
沒有留言:
張貼留言