下面的menu没有意义,仅仅是个练习而已,看图先:
布局:
view:
布局:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:id="@+id/textViewName" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/name" android:textSize="20dp" /> <EditText android:id="@+id/editTextName" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <TextView android:id="@+id/textViewAge" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/age" android:textSize="20dp" /> <EditText android:id="@+id/editTextAge" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout>
view:
package com.dc.app; import java.util.Calendar; import android.app.Activity; import android.app.AlertDialog; import android.app.DatePickerDialog; import android.app.Dialog; import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.app.ProgressDialog; import android.app.TimePickerDialog; import android.content.DialogInterface; import android.graphics.Color; import android.os.Bundle; import android.util.Log; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.DatePicker; import android.widget.EditText; import android.widget.TextView; import android.widget.TimePicker; import android.widget.Toast; public class AppActivity extends Activity { private static final String TAG="AppActivity"; private TextView textViewName,textViewAge; private EditText editTextName,editTextAge; private final int menuIdRegiste=1; private final int menuIdBack=2; private final int menuIdLogin=3; private final int menuIdDate=4; private final int menuIdTime=5; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Log.v(TAG,"start"); textViewName=(TextView)this.findViewById(R.id.textViewName); textViewName.setTextColor(Color.BLUE); textViewName.setBackgroundColor(Color.WHITE); editTextName=(EditText)this.findViewById(R.id.editTextName); textViewAge=(TextView)this.findViewById(R.id.textViewAge); textViewAge.setTextColor(Color.BLUE); textViewAge.setBackgroundColor(Color.WHITE); editTextAge=(EditText)this.findViewById(R.id.editTextAge); } public boolean onCreateOptionsMenu(Menu menu) {//初始化Menu菜单选择项 super.onCreateOptionsMenu(menu); //添加菜单项,比如: menu.add(0, menuIdRegiste, 0, R.string.registe).setShortcut('1', 'r');//设置快捷键 menu.add(0, menuIdBack, 0, R.string.back).setShortcut('2', 'b');//设置快捷键 menu.add(0, menuIdLogin, 0, R.string.login).setShortcut('3', 'l');//设置快捷键 menu.add(0, menuIdDate, 0, R.string.date).setShortcut('4', 'd');//设置快捷键 menu.add(0, menuIdTime, 0, R.string.time).setShortcut('5', 't');//设置快捷键 //添加其他菜单项。。。。。。 return true; } public boolean onPrepareOptionsMenu(Menu menu) {// super.onPrepareOptionsMenu(menu); //这里可以事先设置菜单的可见性,如果都可见,可以不设置 menu.findItem(menuIdRegiste).setVisible(true).setIcon(android.R.drawable.ic_menu_add);//设置菜单项可见性 menu.findItem(menuIdBack).setVisible(true).setIcon(android.R.drawable.ic_menu_save);//设置菜单项可见性 menu.findItem(menuIdLogin).setVisible(true).setIcon(android.R.drawable.ic_menu_camera);//设置菜单项可见性 menu.findItem(menuIdDate).setVisible(true).setIcon(android.R.drawable.ic_menu_compass);//设置菜单项可见性 menu.findItem(menuIdTime).setVisible(true).setIcon(R.drawable.icon);//设置菜单项可见性 return true; } public boolean onOptionsItemSelected(MenuItem item) {//选择了一个菜单项的时候调用 //这里可以预先处理想要的变量 switch (item.getItemId()) { case menuIdRegiste://一项一项的处理想要做的,不用我介绍了吧 Toast.makeText(this, R.string.menuIdRegisteContent, Toast.LENGTH_LONG).show(); showDialog(DIALOG_KEY); return true; case menuIdBack: Toast.makeText(this, R.string.menuIdBackContent, Toast.LENGTH_LONG).show(); showNotify(); return true; case menuIdLogin: showDialog(DIALOG_LOGIN); return true; case menuIdDate: showDialog(DIALOG_DATE); return true; case menuIdTime: showDialog(DIALOG_TIME); return true; } return super.onOptionsItemSelected(item); } private static final int DIALOG_KEY = 0; private static final int DIALOG_LOGIN = 3; private static final int DIALOG_DATE = 4; private static final int DIALOG_TIME = 5; @Override protected Dialog onCreateDialog(int id) { switch (id) { case DIALOG_KEY: ProgressDialog progressDialog = new ProgressDialog(this); progressDialog.setTitle("通讯录"); progressDialog.setMessage("获取通讯录中...请稍候"); progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); progressDialog.setIndeterminate(true);//不明确 progressDialog.setCancelable(true);//是否可以按退回按键取消 progressDialog.setButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub dialog.cancel(); // removeDialog(DIALOG_KEY); } }); progressDialog.setCanceledOnTouchOutside(true); return progressDialog; case DIALOG_LOGIN: View dialogview=LayoutInflater.from(this).inflate(R.layout.main, null); Dialog loginDialog=new AlertDialog.Builder(this) .setTitle("登录") .setView(dialogview)//this.findViewById(R.layout.main),不可以,why? .setCancelable(true) .setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub dialog.cancel(); } }) .setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub dialog.cancel(); } }) .create(); loginDialog.setCanceledOnTouchOutside(true); return loginDialog; case DIALOG_DATE: Calendar c = Calendar.getInstance(); DatePickerDialog dateDialog=new DatePickerDialog(this,new DatePickerDialog.OnDateSetListener(){ @Override public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { // TODO Auto-generated method stub showDialog(DIALOG_KEY); } },c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DATE)); return dateDialog; case DIALOG_TIME: Calendar c2 = Calendar.getInstance(); TimePickerDialog timeDialog=new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() { @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) { showDialog(DIALOG_KEY); } }, c2.get(Calendar.HOUR), c2.get(Calendar.MINUTE), true); return timeDialog; } return null; } private void showNotify(){ Notification notice=new Notification(); notice.icon=R.drawable.icon; notice.tickerText="您有一条新的信息"; notice.defaults=Notification.DEFAULT_SOUND; notice.when=10L; // 100 毫秒延迟后,震动 250 毫秒,暂停 100 毫秒后,再震动 500 毫秒 // notice.vibrate = new long[] { 100, 250, 100, 500 }; notice.setLatestEventInfo(this, "通知", "开会啦", PendingIntent.getActivity(this, 0, null, 0)); NotificationManager manager=(NotificationManager)getSystemService(this.NOTIFICATION_SERVICE); manager.notify(0,notice); } }