Menu,CustomToast
MainActivity.java
public class MainActivity extends Activity { private Button btnDlg=null; private String []data =null; private Button btnDatePicker=null; private Button btnTimePicker=null; private Button btnContextMenu=null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btnDlg=(Button) findViewById(R.id.btn_dlg); data=getResources().getStringArray(R.array.data); btnDlg.setOnClickListener(new OnClickListener() { public void onClick(View v) { AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this); builder.setTitle("对话框"); builder.setItems(data, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { MyToast.makeText(getApplicationContext(), "这里的which表示选项位置"+which, Toast.LENGTH_SHORT).show(); } }); builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { MyToast.makeText(getApplicationContext(), "这里的which表示按钮位置"+which, Toast.LENGTH_SHORT).show(); } }); builder.setNeutralButton("酱油",new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { MyToast.makeText(getApplicationContext(), "这里的which表示按钮位置"+which, Toast.LENGTH_SHORT).show(); } }); builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { MyToast.makeText(getApplicationContext(), "这里的which表示按钮位置"+which, Toast.LENGTH_SHORT).show(); } }); builder.create().show(); } }); btnDatePicker=(Button) findViewById(R.id.btn_date_picker); btnDatePicker.setOnClickListener(new OnClickListener() { public void onClick(View v) { DatePickerDialog dpd=new DatePickerDialog(MainActivity.this, new OnDateSetListener() { public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { MyToast.makeText(getApplicationContext(), year+"-"+(monthOfYear+1)+"-"+dayOfMonth, Toast.LENGTH_SHORT).show(); } }, 2012, 11, 22); dpd.show(); } }); btnTimePicker =(Button) findViewById(R.id.btn_time_picker); btnTimePicker.setOnClickListener(new OnClickListener() { public void onClick(View v) { TimePickerDialog tpd=new TimePickerDialog(MainActivity.this, new OnTimeSetListener() { public void onTimeSet(TimePicker view, int hourOfDay, int minute) { MyToast.makeText(getApplicationContext(), hourOfDay+":"+minute, Toast.LENGTH_SHORT).show(); } }, 0, 0, true); tpd.show(); } }); btnContextMenu=(Button) findViewById(R.id.btn_context_menu); btnContextMenu.setOnClickListener(new OnClickListener() { public void onClick(View v) { MyToast.makeText(getApplicationContext(), "这是点击", Toast.LENGTH_SHORT).show(); } }); btnContextMenu.setOnLongClickListener(new OnLongClickListener() { public boolean onLongClick(View v) { MyToast.makeText(getApplicationContext(), "这是长按", Toast.LENGTH_SHORT).show(); return false; } }); registerForContextMenu(btnContextMenu); } @Override public boolean onCreateOptionsMenu(Menu menu) { menu.add(0, 0, 0, "play"); menu.add(0, 1, 1, "pause"); SubMenu submenu=menu.addSubMenu(1, 2, 2, "system"); submenu.add(1, 3, 1, "phone"); submenu.add(1, 4, 2, "exit"); return super.onCreateOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case 4: android.os.Process.killProcess(android.os.Process.myPid()); break; default: MyToast.makeText(getApplicationContext(), ""+item.getTitle(), Toast.LENGTH_SHORT).show(); break; } return super.onOptionsItemSelected(item); } @Override public boolean onContextItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.menu_exit: android.os.Process.killProcess(android.os.Process.myPid()); break; default: MyToast.makeText(getApplicationContext(), "contexMenu"+item.getTitle(), Toast.LENGTH_SHORT).show(); break; } return super.onContextItemSelected(item); } @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { getMenuInflater().inflate(R.menu.menu, menu); super.onCreateContextMenu(menu, v, menuInfo); } }
MyToast.java
public class MyToast { public static Toast makeText(Context context,String text,int duration){ LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); View view = inflater.inflate(R.layout.custom_toast_layout,null); ImageView image = (ImageView) view.findViewById(R.id.image); image.setImageResource(R.drawable.ic_launcher); TextView textView = (TextView) view.findViewById(R.id.text); textView.setText(text); Toast t = new Toast(context); t.setGravity(Gravity.CENTER_VERTICAL, 0, 0); t.setDuration(duration); t.setView(view); return t; } }
custom_toast_layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/custom_toast_layout" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#00000000" > <ImageView android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="fill_parent" /> <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="fill_parent" android:gravity="center_vertical" android:textSize="20sp" android:textColor="#000000" /> </LinearLayout>
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:id="@+id/btn_dlg" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="@string/btn_dlg" /> <Button android:id="@+id/btn_date_picker" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="@string/btn_date_picker" /> <Button android:id="@+id/btn_time_picker" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="@string/btn_time_picker" /> <Button android:id="@+id/btn_context_menu" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="@string/btn_context_menu" /> </LinearLayout>
string.xml
<resources> <string name="app_name">12-04</string> <string name="hello_world">Hello world!</string> <string name="menu_settings">Settings</string> <string name="title_activity_main">MainActivity</string> <string name="btn_dlg">对话框</string> <string-array name="data"> <item>选项零</item> <item>选项一</item> <item>选项二</item> <item>选项三</item> <item>选项四</item> <item>选项五</item> <item>选项六</item> </string-array> <string name="btn_date_picker">DatePicker</string> <string name="btn_time_picker">TimePicker</string> <string name="btn_context_menu">ContextMenu</string> </resources>
menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" > <item android:id="@+id/menu_play" android:title="play"/> <item android:id="@+id/menu_pause" android:title="pause"/> <item android:id="@+id/menu_system" android:title="system"> <menu> <item android:id="@+id/menu_sms" android:title="pause"/> <item android:id="@+id/menu_phone" android:title="phone"/> <item android:id="@+id/menu_exit" android:title="exit"/> </menu> </item> </menu>