<menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/menu1" android:title="Menu 1" android:orderInCategory="100" android:showAsAction="never" /> <item android:id="@+id/menu2" android:title="Menu 2" android:orderInCategory="100" android:showAsAction="never" /> <item android:id="@+id/menu3" android:title="Menu 3" android:orderInCategory="100" android:showAsAction="never" /> </menu>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="96dp" android:text="TextView" /> </RelativeLayout>
package com.myapp; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.MenuItem; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final TextView txt = (TextView) findViewById(R.id.textView1); txt.setText("Click Button Menu to Show Menu"); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_menu, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.menu1: Toast.makeText(MainActivity.this, "Your Click Menu 1", Toast.LENGTH_SHORT).show(); return true; case R.id.menu2: Toast.makeText(MainActivity.this, "Your Click Menu 2", Toast.LENGTH_SHORT).show(); return true; case R.id.menu3: Toast.makeText(MainActivity.this, "Your Click Menu 3", Toast.LENGTH_SHORT).show(); return true; default: return super.onOptionsItemSelected(item); } } }
ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท