package com.example.menu;
import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;
import android.view.Menu;
public class MainActivity extends Activity {
TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TableLayout tl = new TableLayout(this);
setContentView(tl);
tv = new TextView(this);
tv.setText("歡迎光臨。");
tl.addView(tv);
}
public boolean onCreateOptionsMenu(Menu menu)
{
menu.add(Menu.NONE, 0, 0, "選項一");
menu.add(Menu.NONE, 1, 1, "選項二");
menu.add(Menu.NONE, 2, 2, "選項三");
menu.add(Menu.NONE, 3, 3, "選項四");
return super.onCreateOptionsMenu(menu);
}
public boolean onOptionsItemSelected(MenuItem mi)
{
String str = null;
switch(mi.getItemId())
{
case 0:
str = "選項一";
break;
case 1:
str = "選項二";
break;
case 2:
str = "選項三";
break;
case 3:
str = "選項四";
break;
}
tv.setText("是" + str + "對吧。");
return true;
}
}
轉自Android程式設計實例入門
留言列表