博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
系出名门Android(3) - 对话框(Dialog)和通知(Notification)
阅读量:6243 次
发布时间:2019-06-22

本文共 11233 字,大约阅读时间需要 37 分钟。





系出名门Android(3) - 对话框(Dialog)和通知(Notification)


作者:



介绍

在 Android 中种对话框及各种通知效果的应用
  • 常用对话框的使用,弹出式对话框、日期选择对话框、时间选择对话框、进度条对话框  
  • 通知(出现在通知列表)和提示性通知(Toast)的演示
1、常用对话框的演示
res/layout/main.xml
<?
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/txtMsg" 
android:layout_width
="wrap_content" 

                
android:layout_height
="wrap_content"
>
</
TextView
> 

                 

        
<
Button 
android:id
="@+id/btn1" 
android:layout_width
="wrap_content" 

                
android:layout_height
="wrap_content"
>
</
Button
> 

        
<
Button 
android:id
="@+id/btn2" 
android:layout_width
="wrap_content" 

                
android:layout_height
="wrap_content"
>
</
Button
> 

        
<
Button 
android:id
="@+id/btn3" 
android:layout_width
="wrap_content" 

                
android:layout_height
="wrap_content"
>
</
Button
> 

        
<
Button 
android:id
="@+id/btn4" 
android:layout_width
="wrap_content" 

                
android:layout_height
="wrap_content"
>
</
Button
> 

        
<
Button 
android:id
="@+id/btn5" 
android:layout_width
="wrap_content" 

                
android:layout_height
="wrap_content"
>
</
Button
> 

        
<
Button 
android:id
="@+id/btn6" 
android:layout_width
="wrap_content" 

                
android:layout_height
="wrap_content"
>
</
Button
> 

                 

</
LinearLayout
>
 
res/layout/view.xml
<?
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:text
="我是一个 View" 

                
android:layout_width
="wrap_content" 
android:layout_height
="wrap_content"
> 

        
</
TextView
> 

         

</
LinearLayout
>
 
res/values/array.xml
<?
xml 
version
="1.0" 
encoding
="utf-8"
?> 

<
resources
> 

        <!--
 
                定义一个名为 ary 的 string 类型的数组 
        
--> 

        
<
string-array 
name
="ary"
> 

                
<
item
>项目 1
</
item
> 

                
<
item
>项目 2
</
item
> 

        
</
string-array
> 

</
resources
>
 
Main.java
package com.webabcd.dialog; 


import java.util.Calendar; 


import android.app.Activity; 

import android.app.AlertDialog; 

import android.app.DatePickerDialog; 

import android.app.Dialog; 

import android.app.ProgressDialog; 

import android.app.TimePickerDialog; 

import android.app.DatePickerDialog.OnDateSetListener; 

import android.app.TimePickerDialog.OnTimeSetListener; 

import android.content.DialogInterface; 

import android.content.DialogInterface.OnClickListener; 

import android.os.Bundle; 

import android.view.View; 

import android.widget.DatePicker; 

import android.widget.TextView; 

import android.widget.TimePicker; 

import android.widget.Button; 


public 
class Main 
extends Activity { 

        
/** Called when the activity is first created. */ 

        @Override 

        
public 
void onCreate(Bundle savedInstanceState) { 

                
super.onCreate(savedInstanceState); 

                setContentView(R.layout.main); 


                
// 弹出式对话框的 Demo。先调用 Builder(),在 Create(), 需要显示对话框的是后再调用 show() 

                AlertDialog dialog = 
new AlertDialog.Builder(
this).setTitle(
"弹出对话框").create(); 

                dialog.show(); 

                 

                
// 以下是各种对话框的 Demo 

                MyButtonClickListener listener = 
new MyButtonClickListener(); 

                Button btn1 = (Button) 
this.findViewById(R.id.btn1); 

                btn1.setText(
"简单的对话框的 Demo"); 

                btn1.setOnClickListener(listener); 


                Button btn2 = (Button) 
this.findViewById(R.id.btn2); 

                btn2.setText(
"包括常用设置的对话框(数据来自 xml)"); 

                btn2.setOnClickListener(listener); 


                Button btn3 = (Button) 
this.findViewById(R.id.btn3); 

                btn3.setText(
"弹出的对话框的内容是一个 View"); 

                btn3.setOnClickListener(listener); 


                Button btn4 = (Button) 
this.findViewById(R.id.btn4); 

                btn4.setText(
"日期选择对话框"); 

                btn4.setOnClickListener(listener); 


                Button btn5 = (Button) 
this.findViewById(R.id.btn5); 

                btn5.setText(
"时间选择对话框"); 

                btn5.setOnClickListener(listener); 


                Button btn6 = (Button) 
this.findViewById(R.id.btn6); 

                btn6.setText(
"进度条对话框"); 

                btn6.setOnClickListener(listener); 

        } 


        
class MyButtonClickListener 
implements View.OnClickListener { 

                @Override 

                
public 
void onClick(View v) { 

                         

                        
// 具体的对话框的实现可以通过重写 onCreateDialog 完成 

                         

                        
switch (v.getId()) { 

                        
case R.id.btn1: 

                                Main.
this.showDialog(0); 

                                
break

                        
case R.id.btn2: 

                                Main.
this.showDialog(1); 

                                
break

                        
case R.id.btn3: 

                                Main.
this.showDialog(2); 

                                
break

                        
case R.id.btn4: 

                                Main.
this.showDialog(3); 

                                
break

                        
case R.id.btn5: 

                                Main.
this.showDialog(4); 

                                
break

                        
case R.id.btn6: 

                                Main.
this.showDialog(5); 

                                
break

                        } 

                } 

        } 


        @Override 

        
public Dialog onCreateDialog(
int id) { 

                
switch (id) { 


                
case 0: 

                        
// 一个简单的弹出对话框 

                        
return 
new AlertDialog.Builder(
this).setTitle(
"这是一个简单的弹出对话框的 Demo"

                                        .create(); 


                
case 1: 

                        
// 一个相对复杂的弹出对话框 

                        
return 
new AlertDialog.Builder(
this

                                        .setTitle(
"标题"
// 设置标题 

                                        
// .setCustomTitle(View) // 以一个 View 作为标题    

                                        .setIcon(R.drawable.icon01) 
// 设置标题图片 

                                        
// .setMessage("信息") // 需要显示的弹出内容 

                                        .setPositiveButton(
"确定"
new OnClickListener() { 
// 设置弹框的确认按钮所显示的文本,以及单击按钮后的响应行为 

                                                @Override 

                                                
public 
void onClick(DialogInterface a0, 
int a1) { 

                                                        TextView txtMsg = (TextView) Main.
this.findViewById(R.id.txtMsg); 

                                                        txtMsg.append(
"单击了对话框上的“确认”按钮\n"); 

                                                } 

                                        }) 

                                        .setItems(R.array.ary, 
new DialogInterface.OnClickListener() { 
// 弹框所显示的内容来自一个数组。数组中的数据会一行一行地依次排列 

                                                
public 
void onClick(DialogInterface dialog,        
int which) { 

                                                } 

                                        }) 

                                        
// 其他常用方法如下 

                                        
// .setMultiChoiceItems(arg0, arg1, arg2) 

                                        
// .setSingleChoiceItems(arg0, arg1, arg2) 

                                        
// .setNeutralButton(arg0, arg1) 

                                        
// .setNegativeButton(arg0, arg1) 

                                        .create(); 


                
case 2: 

                        
// 弹出对话框为指定的 View 的 Demo 

                        
return 
new AlertDialog.Builder(
this).setTitle(
"此对话框的内容是一个 View"

                                        .setView(
this.findViewById(R.layout.view)).create(); 


                
case 3: 

                        
// 弹出日期选择对话框 

                        Calendar c = Calendar.getInstance(); 

                        
return 
new DatePickerDialog(
this
new OnDateSetListener() { 

                                @Override 

                                
public 
void onDateSet(DatePicker view, 
int year, 
int monthOfYear, 
int dayOfMonth) { 

                                        TextView txtMsg = (TextView) Main.
this.findViewById(R.id.txtMsg); 

                                        txtMsg.append(
"新设置的日期为:" + String.valueOf(year) + 
"-" 

                                                        + String.valueOf(monthOfYear) + 
"-" 

                                                        + String.valueOf(dayOfMonth) + 
"\n"); 

                                } 

                        }, c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DATE)); 


                
case 4: 

                        
// 弹出时间选择对话框 

                        Calendar c2 = Calendar.getInstance(); 

                        
return 
new TimePickerDialog(
this
new OnTimeSetListener() { 

                                @Override 

                                
public 
void onTimeSet(TimePicker view, 
int hourOfDay, 
int minute) { 

                                        TextView txtMsg = (TextView) Main.
this.findViewById(R.id.txtMsg); 

                                        txtMsg.append(
"新设置的时间为:"    

                                                        + String.valueOf(hourOfDay) + 
":" 

                                                        + String.valueOf(minute) + 
"\n"); 

                                } 


                        }, c2.get(Calendar.HOUR), c2.get(Calendar.MINUTE), 
true); 


                
case 5: 

                        
// 弹出进度条对话框 

                        ProgressDialog progress = 
new ProgressDialog(
this); 

                        progress.setMessage(
"loading..."); 

                        
return progress; 


                
default

                        
return 
null

                } 

        } 

}
 
2、各种提示效果的演示
res/layout/main.xml
<?
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"
> 

         

        
<
Button 
android:id
="@+id/btn1" 
android:layout_width
="wrap_content" 

                
android:layout_height
="wrap_content"
>
</
Button
> 

        
<
Button 
android:id
="@+id/btn2" 
android:layout_width
="wrap_content" 

                
android:layout_height
="wrap_content"
>
</
Button
> 

        
<
Button 
android:id
="@+id/btn3" 
android:layout_width
="wrap_content" 

                
android:layout_height
="wrap_content"
>
</
Button
> 

        
<
Button 
android:id
="@+id/btn4" 
android:layout_width
="wrap_content" 

                
android:layout_height
="wrap_content"
>
</
Button
> 

                 

</
LinearLayout
>
 
res/layout/view.xml
<?
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/txtMsg" 
android:layout_width
="wrap_content" 

                
android:layout_height
="wrap_content"
> 

        
</
TextView
> 

                 

</
LinearLayout
>
 
Main.java
package com.webabcd.notification; 


import android.app.Activity; 

import android.app.Notification; 

import android.app.NotificationManager; 

import android.app.PendingIntent; 

import android.content.Context; 

import android.content.Intent; 

import android.os.Bundle; 

import android.util.Log; 

import android.view.LayoutInflater; 

import android.view.View; 

import android.widget.Button; 

import android.widget.TextView; 

import android.widget.Toast; 


public 
class Main 
extends Activity { 


        
/** Called when the activity is first created. */ 

        @Override 

        
public 
void onCreate(Bundle savedInstanceState) { 

                
super.onCreate(savedInstanceState); 

                setContentView(R.layout.main); 


                
// 通过 Tost.makeText().show() 来实现提示性的通知效果 

                
// 短时间的提示性通知的 Demo 

                Button btn1 = (Button) 
this.findViewById(R.id.btn1); 

                btn1.setText(
"短时间提示"); 

                btn1.setOnClickListener(
new Button.OnClickListener() { 

                        
public 
void onClick(View v) { 

                                Toast.makeText(Main.
this
"我是短时间提示", Toast.LENGTH_SHORT).show(); 

                        } 

                }); 


                
// 长时间的提示性通知的 Demo 

                Button btn2 = (Button) 
this.findViewById(R.id.btn2); 

                btn2.setText(
"长时间提示"); 

                btn2.setOnClickListener(
new Button.OnClickListener() { 

                        
public 
void onClick(View v) { 

                                Toast.makeText(Main.
this
"我是长时间提示", Toast.LENGTH_LONG).show(); 

                        } 

                }); 


                
// 以一个 View 作为提示性通知的 Demo 

                Button btn3 = (Button) 
this.findViewById(R.id.btn3); 

                btn3.setText(
"以一个 View 做提示"); 

                btn3.setOnClickListener(
new Button.OnClickListener() { 

                        
public 
void onClick(View v) { 

                                View view = inflateView(R.layout.view); 

                                TextView txtMsg = (TextView) view.findViewById(R.id.txtMsg); 

                                txtMsg.setText(
"提示内容"); 


                                Toast toast = 
new Toast(Main.
this); 

                                toast.setView(view); 

                                toast.setDuration(Toast.LENGTH_LONG); 

                                toast.show(); 

                        } 

                }); 


                 

                Button btn4 = (Button) 
this.findViewById(R.id.btn4); 

                btn4.setText(
"发出一个通知(Notification)"); 

                btn4.setOnClickListener(
new Button.OnClickListener() { 

                        
public 
void onClick(View v) { 

                                
// 实例化通知管理器 

                                NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 


                                
// 指定单击通知后所打开的详细的通知页面(单击通知后打开 NotificationView) 

                                PendingIntent contentIntent = PendingIntent.getActivity( 

                                                Main.
this, 0, 
new Intent(Main.
this,        NotificationView.
class), 0); 


                                
// 实例化一个通知,并指定其图标和标题(在提示栏上显示) 

                                Notification n = 
new Notification(R.drawable.icon01, 
"我是滚动的通知信息我是滚动的通知信息我是滚动的通知信息", System.currentTimeMillis()); 

                                 

                                
// 设置通知的发送人和通知的详细内容(打开提示栏后在通知列表中显示) 

                                n.setLatestEventInfo(Main.
this
"通知发送人"
"我是详细的通知信息我是详细的通知信息我是详细的通知信息", contentIntent); 


                                
// 100 毫秒延迟后,震动 250 毫秒,暂停 100 毫秒后,再震动 500 毫秒 

                                n.vibrate = 
new 
long[] { 100, 250, 100, 500 }; 

                                 

                                
// 发出通知(其中第一个参数为通知标识符) 

                                nm.notify(0, n); 

                        } 

                }); 

        } 


        
// 将指定的 xml 资源转换为一个 View 

        
private View inflateView(
int resource) { 

                LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

                
return vi.inflate(resource, 
null); 

        } 


        
// 打开详细通知页后此 Activity 会被 Pause,从详细通知页返回后此 Activity 会被 Resume 

        @Override 

        
protected 
void onPause() { 

                
// TODO Auto-generated method stub 

                
super.onPause(); 

                 

                Log.d(
"MyDebug"
"onPause"); 

        } 

         

        @Override 

        
protected 
void onResume() { 

                
// TODO Auto-generated method stub 

                
super.onResume(); 

                 

                Log.d(
"MyDebug"
"onResume"); 

        } 

}
 
NotificationView.java
package com.webabcd.notification; 


import android.app.Activity; 

import android.app.NotificationManager; 

import android.os.Bundle; 

import android.widget.TextView; 


// 单击通知列表的某个通知后,所打开的详细的通知页 

public 
class NotificationView 
extends Activity { 

        
protected 
void onCreate(Bundle savedInstanceState) { 

                
super.onCreate(savedInstanceState); 

                setContentView(R.layout.view); 


                TextView txtMsg = (TextView)
this.findViewById(R.id.txtMsg); 

                txtMsg.setText(
"点通知之后所链接到的 Activity"); 

                 

                NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 

                
// 取消显示在通知列表中的指定通知(参数为通知标识符) 

                nm.cancel(0); 

                 

                
// 需要关闭此 Activity 的话就 finish 它既可 

                
// this.finish(); 

        } 

}
 
 
OK
     本文转自webabcd 51CTO博客,原文链接: http://blog.51cto.com/webabcd/342014
,如需转载请自行联系原作者
你可能感兴趣的文章
本分类说明 -- django
查看>>
Android Binder IPC分析
查看>>
mysql分隔字符串,并将分隔字符串作为新列
查看>>
图学java基础篇之集合
查看>>
Tomcat源码分析------ 架构
查看>>
如何分析并策划好网站
查看>>
解决Skype一台电脑登陆多个账号的问题
查看>>
Gradle构建卡住问题解决
查看>>
linux使用cron任务定时执行数据库操作
查看>>
实验11 原始套接字
查看>>
C#配置Properties.Setting
查看>>
Tomcat:为Filter过滤器设置参数
查看>>
在线编辑、展示HTML、CSS、Javascript的网站
查看>>
java读取csv文件
查看>>
js判断是否为360浏览器
查看>>
京华科讯存储虚拟化技术
查看>>
Python模板库Mako的用法
查看>>
Spring整合shiro,使用jdbcTmplate操作SessionDAO
查看>>
Hibernate所鼓励的7大措施
查看>>
Python对进程Multiprocessing基础
查看>>