下面的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); } }
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
