首页 > 其他分享 >Dialog&Notification&OptionsMenu练习

Dialog&Notification&OptionsMenu练习

时间:2023-04-07 10:06:05浏览次数:50  
标签:OptionsMenu DIALOG int Notification Dialog menu import android true


下面的menu没有意义,仅仅是个练习而已,看图先:



布局:

<?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);
    }
}



  • Dialog&Notification&OptionsMenu练习_android

  • 大小: 24.8 KB
  • 查看图片附件

标签:OptionsMenu,DIALOG,int,Notification,Dialog,menu,import,android,true
From: https://blog.51cto.com/u_5454003/6174587

相关文章

  • 一个典型的从下部弹上来的Dialog
    典型的看图importandroid.app.Dialog;importandroid.content.Context;importandroid.os.Bundle;importandroid.util.DisplayMetrics;importandroid.view.Gravity;importandroid.view.View;importandroid.view.Window;importandroid.view.Windo......
  • 简单实现可以多选的ProductListDialog<T>
    只是一个范例,是为了代码快速迭代而写的使用了listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);效果图importjava.util.ArrayList;importjava.util.List;importandroid.app.Dialog;importandroid.content.Context;importandroid.os.Bundle......
  • 基于WheelView自定义的DatePickerDialog
    本人利用WheelView写的一个DatePickerDialog(还有一个TimePickerDialog,本人忘了在写在哪个项目里了,等找到了也贴上来)先看图,有个直观的了解DatePickerDialog代码:importjava.util.Calendar;importcom.widget.wheel.NumericWheelAdapter;importcom.widge......
  • Notification的基本用法
    android4.0以前:privatestaticfinalintNOTIFY_ID=0;notificationManager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE); privatevoidshowNotification(Storestore){ Notificationnotification=newNotification(); ......
  • FolderBrowserDialog类实现选择打开文件
    privatevoidbutton1_Click(objectsender,EventArgse){FolderBrowserDialogdilog=newFolderBrowserDialog();dilog.Description="请选择文件夹";if(dilog.ShowDialog()==DialogResult.OK||dilog.ShowDialo......
  • mac notification
    displaynotification"message"withtitle"title"subtitle"subtitle"displaynotification"message"soundname"SoundName"Validsoundnamesarethenamesofsoundslocatedin…~/Library/Sounds......
  • Redis实践操作之—— keyspace notification(键空间通知)
    源码地址:https://github.com/Tinywan/PHP_Experience一、需求分析:设置了生存时间的Key,在过期时能不能有所提示?如果能对过期Key有个监听,如何对过期Key进行一个回调处理?如何使用Redis来实现定时任务?二、序言:    本文所说的定时任务或者说计划任务并不是很多人想象中的那样,比......
  • Prism源码分析--IDialogService
    1,首先看下接口提供了哪些方法 2,逻辑的实现类是DialogService,业务逻辑层一般使用方法如下   3,上面使用的方法实际上是IDialogService的一个扩展方法,扩展方法调用了实现类中的Show方法   4,Show方法内又调用了ShowDialogInternal   5,ShowDialogInternal......
  • c#后台执行js弹窗art.dialog,成功后跳转页面
     stringmsbox="<script>dialog({title:'修改成功',content:'修改成功',width:'180px',height:'100px',okValue:'确定',ok:function(){";          ......
  • element-plus的el-dialog对话框组件自定义样式未生效
    修改dialog组件样式必须在非scoped环境下,再加一个style标签,并给需要加的dialog一个类名eg:createDialog<stylelang="scss"scoped>...</style><stylelang="scss">.crea......