08-28 Dialog续篇、Notification(通知)
2015-08-28 21:57
330 查看
自定义的Dialog
Notification(通知)
//**mydialog.xml** <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/dialog_background"> <TextView android:id="@+id/textview_title" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="我是标题" android:textColor="#ff0000" android:padding="10dp" android:background="@drawable/title_background"/> <TextView android:id="@+id/textview_content" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="我是内容" android:gravity="center" android:padding="10dp" style="@style/Dialogstyle"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:id="@+id/button_cancle" android:layout_width="0dip" android:layout_weight="1" android:layout_height="match_parent" android:background="@drawable/btn_ground_left" android:text="取消"/> <Button android:id="@+id/button_ok" android:layout_width="0dip" android:layout_weight="1" android:layout_height="wrap_content" android:background="@drawable/btn_ground_right" android:text="确定"/> </LinearLayout> </LinearLayout> //**自定义style** <style parent="@android:Theme.Dialog" name="NoDialogTitle"> <item name="android:windowFrame">@null</item> <item name="android:windowNoTitle">true</item> <item name="android:windowBackground">@android:color/transparent</item> <item name="android:windowIsFloating">true</item> <item name="android:windowContentOverlay">@null</item> </style> <style name="Dialogstyle" > < item name="android:textColor">#00f</item> </style> //****drawable文件**** //**btn_normal_left.xml** <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <corners android:bottomRightRadius="10dp"/> <solid android:color="#55ff0000"/> <stroke android:color="#ff0000" android:width="2dp"/> </shape> //**btn_normal_right.xml** <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <corners android:bottomRightRadius="10dp"/> <solid android:color="#55ff0000"/> <stroke android:color="#ff0000" android:width="2dp"/> </shape> //**btn_pressed_left.xml** <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <corners android:bottomLeftRadius="10dp"/> <solid android:color="#55ff00"/> <stroke android:color="#ff0000" android:width="2dp"/> </shape> //**btn_pressed_right.xml** <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <corners android:bottomRightRadius="10dp"/> <solid android:color="#55ff00"/> <stroke android:color="#ff0000" android:width="2dp"/> </shape> //**btn_ground_left.xml** <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/btn_pressed_left" android:state_pressed="true"/> <item android:drawable="@drawable/btn_normal_left"/> </selector> //**btn_ground_right.xml** <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/btn_pressed_right" android:state_pressed="true"/> <item android:drawable="@drawable/btn_normal_right"/> </selector> //**dialog_background.xml** <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <corners android:radius="5dp"/> <solid android:color="#ffffff"/> </shape> //**title_background.xml** <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <corners android:radius="5dp"/> <solid android:color="#55ff"/> </shape> //**自定义的Dialog的方法** private void showmydialog() { dialog=new Dialog(MainActivity.this, R.style.NoDialogTitle);//diallog初始化,并去掉系统自带的标题 LayoutInflater inflater=getLayoutInflater(); final View dialogView=inflater.inflate(R.layout.mydialog,null); TextView textView_title= (TextView) dialogView.findViewById(R.id.textview_title); TextView textView_content= (TextView) dialogView.findViewById(R.id.textview_content); Button button_cancle= (Button) dialogView.findViewById(R.id.button_cancle); Button button_ok= (Button) dialogView.findViewById(R.id.button_ok); textView_title.setText("这是新设置的标题"); textView_content.setText("这是新设置的内容"); button_ok.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(getApplicationContext(), "点击的是确定按钮", Toast.LENGTH_SHORT).show(); dialog.dismiss(); } }); button_cancle.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { dialog.dismiss(); } }); // dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); dialog.setContentView(dialogView); dialog.show(); } ![这里写图片描述](http://img.blog.csdn.net/20150828213458050) //**显示日期的Dialog** private void showdatedialog() { mCalender= Calendar.getInstance(); DatePickerDialog datePickerDialog=new DatePickerDialog(MainActivity.this, new DatePickerDialog.OnDateSetListener() { @Override public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { mCalender.set(year,monthOfYear,dayOfMonth); SimpleDateFormat formatter=new SimpleDateFormat("yyyy年MM月dd日"); Toast.makeText(getApplicationContext(), "这是造型的" + formatter.format(mCalender.getTime()), Toast.LENGTH_SHORT).show(); } },mCalender.get(Calendar.YEAR),mCalender.get(Calendar.MONTH),mCalender.get(Calendar.DAY_OF_MONTH)); datePickerDialog.show(); } ![这里写图片描述](http://img.blog.csdn.net/20150828213528470) //**显示时间的Dialog** private void showtimedialog() { mCalender= Calendar.getInstance(); TimePickerDialog timePickerDialog=new TimePickerDialog(MainActivity.this, new TimePickerDialog.OnTimeSetListener() { @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) { mCalender.set(Calendar.HOUR,hourOfDay); mCalender.set(Calendar.MINUTE,minute); // mCalender.set(hourOfDay,minute); //只会显示当前时间 SimpleDateFormat formatter=new SimpleDateFormat("yyyy年MM月dd日HH:mm"); Toast.makeText(getApplicationContext(), "这是造型的" + formatter.format(mCalender.getTime()), Toast.LENGTH_SHORT).show(); } },mCalender.get(Calendar.HOUR),mCalender.get(Calendar.MINUTE),true); timePickerDialog.show(); } ![这里写图片描述](http://img.blog.csdn.net/20150828213553711) //****PopupWindow**** 点击后在按钮下方显示Dialog //**popupwindow.xml文件** <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@mipmap/peach"> <TextView android:id="@+id/textview1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="文本一" android:gravity="center" android:padding="20dp"/> <TextView android:id="@+id/textview2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="文本二" android:gravity="center" android:padding="20dp"/> <TextView android:id="@+id/textview3" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="文本三" android:gravity="center" android:padding="20dp"/> </LinearLayout> //popupwindow实现的方法 @TargetApi(Build.VERSION_CODES.CUPCAKE)//解决因版本不同带来的问题 private void showPopupWindow() { mPopupWindow=new PopupWindow(MainActivity.this); View popupwindow=getLayoutInflater().inflate(R.layout.popupwindow,null); mPopupWindow.setWidth(ViewGroup.LayoutParams.MATCH_PARENT); mPopupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT); mPopupWindow.setContentView(popupwindow); mPopupWindow.setFocusable(false); mPopupWindow.setOutsideTouchable(true); mPopupWindow.showAsDropDown(mButton2); } //设置点击后Dialog消失的方法 @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if(keyCode==KeyEvent.KEYCODE_BACK){ if(mPopupWindow!=null&&mPopupWindow.isShowing()){ mPopupWindow.dismiss(); return true; } } return super.onKeyDown(keyCode, event); } ![这里写图片描述](http://img.blog.csdn.net/20150828214442631)
Notification(通知)
//layout_notification.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <Button android:id="@+id/button1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="发出通知"/> <Button android:id="@+id/button2" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/button1" android:text="取消通知"/> <Button android:id="@+id/button3" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/button2" android:text="新发出通知"/> <Button android:id="@+id/button4" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/button3" android:text="新取消通知"/> </RelativeLayout> //**Activity文件** public class MainActivity extends Activity implements View.OnClickListener{ private Button mBtn1; private Button mBtn2; private Button mBtn3; private Button mBtn4; private NotificationManager mNotificationManager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mNotificationManager= (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);//初始化NotificationManager mBtn1= (Button) findViewById(R.id.button1); mBtn2= (Button) findViewById(R.id.button2); mBtn3= (Button) findViewById(R.id.button3); mBtn4= (Button) findViewById(R.id.button4); mBtn1.setOnClickListener(this); mBtn2.setOnClickListener(this); mBtn3.setOnClickListener(this); mBtn4.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()){ case R.id.button1: createNotification(); break; case R.id.button2: mNotificationManager.cancel(1);//取消id为1的通知 break; case R.id.button3: creatnewNotification(); break; case R.id.button4: mNotificationManager.cancel(1);//取消id为2的通知 break; default: break; } } //新式设置方法 @TargetApi(Build.VERSION_CODES.JELLY_BEAN) private void creatnewNotification() { Intent intent1=new Intent(getApplicationContext(),MainActivity.class);//设置pendingIntent事件 PendingIntent pendingIntent1=PendingIntent.getActivity(getApplicationContext(),2,intent1,PendingIntent.FLAG_ONE_SHOT);//设置pendingIntent的使用方式 Notification notification1= new Notification.Builder(MainActivity.this).setSmallIcon(R.mipmap.ic_launcher). setTicker("我是消息").setContentTitle("我是标题").setContentText("我是文本").setContentInfo("我是内容"). setContentIntent(pendingIntent1).setAutoCancel(true).setWhen(System.currentTimeMillis()).build(); mNotificationManager.notify(2,notification1); } //旧式设置方法 private void createNotification() { Notification notification=new Notification();//初始化notification notification.icon= R.mipmap.ic_launcher;//设置通知的图片 notification.tickerText="我是一个消息";//设置状态栏文本 notification.flags=Notification.FLAG_AUTO_CANCEL;//设置为可以取消 Intent intent=new Intent(getApplicationContext(),MainActivity.class);//设置pendingIntent事件 PendingIntent pendingIntent=PendingIntent.getActivity(getApplicationContext(),1,intent,PendingIntent.FLAG_ONE_SHOT);//设置pendingIntent的使用方式 notification.setLatestEventInfo(getApplicationContext(),"我是标题","我是内容",pendingIntent);//设置pendingIntent显示的内容 notification.when=System.currentTimeMillis();// Calendar.getInstance().getTimeInMillis(); //设置触发时间 mNotificationManager.notify(1,notification);//通知管理器将通知添加 } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } } ![旧式设置方法的显示](http://img.blog.csdn.net/20150828215344794) ![旧式设置方法的显示](http://img.blog.csdn.net/20150828215440582) ![新式设置方法的显示](http://img.blog.csdn.net/20150828215540210) ![新式设置方法的显示](http://img.blog.csdn.net/20150828215705036)
相关文章推荐
- mysql processlist 提示 Writing to net解决
- C语言的预处理命令
- hiho coder 并查集应用
- 循环-26. 求给定序列前N项和之四
- Box2d新系列 第八章 连接器(Joints)
- 数据分析的方法有哪些
- 分析指标
- poj 3254 Corn Fields(状态压缩dp)
- 自定义Dialog、带时间和日期的Dialog、PopupWindow、Notification
- 有舍才有得
- 数据分析概念
- 工作用到的正则及测试工具
- LVM的创建、扩展、收缩及快照功能的介绍
- 遇到的一个问题,R.drawable下发现不了文件
- 在Static Text控件中显示JPG图片
- 第51讲:Scala中链式调用风格的实现代码实战及其在Spark编程中的广泛运用学习笔记
- 网站用户行为分析在用户市场领域的应用
- 黑马程序员--对象作为方法的参数连续传递的练习
- iPhone第二节:登录、通讯录界面
- 网站流量统计名词解释