自定义Diaolg与系统消息通报
自定义Diaolg与系统消息通知
自定义Dialog:
一.使用DialogActivity将一个activity变成Dailog样式显示。
Dialog有很多类型(单选,多选,进度条,简单显示文本,自定义等)
public class DialogActivity extends Activity { private final static int SIMPLE_DIALOG= 1; private final static int CUSTOM_VIEW_DIALOG=2; private final static int PROGRESS_DIALOG=3; Dialog d; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.setContentView(R.layout.dialog); findViewById(R.id.button1).setOnClickListener(new Button.OnClickListener(){ @Override public void onClick(View arg0) { DialogActivity.this.showDialog(SIMPLE_DIALOG, null); } }); findViewById(R.id.button2).setOnClickListener(new Button.OnClickListener(){ @Override public void onClick(View arg0) { DialogActivity.this.showDialog(CUSTOM_VIEW_DIALOG, null); } }); findViewById(R.id.button3).setOnClickListener(new Button.OnClickListener(){ @Override public void onClick(View arg0) { DialogActivity.this.showDialog(PROGRESS_DIALOG, null); } }); @Override protected Dialog onCreateDialog(int id, Bundle args) { //简单的Dialog ,显示标题,内容,图标 if(SIMPLE_DIALOG==id) return new AlertDialog.Builder(this) .setTitle("tille") .setIcon(android.R.drawable.ic_dialog_alert) .setMessage("thisis message") .create(); //自定义布局的Dialog else if(CUSTOM_VIEW_DIALOG == id){ LayoutInflater inflater=LayoutInflater.from(this); return new AlertDialog.Builder(this) .setView(inflater.inflate( R.layout.dialog, null) ) .setTitle("tille") .setIcon(android.R.drawable.ic_dialog_alert) .setMessage("thisis message") .setPositiveButton("OK", null) .setNegativeButton("Cancle", null) .create(); } //进度条类型Dialog else if(PROGRESS_DIALOG==id){ ProgressDialog dialog=new ProgressDialog(this); dialog.setTitle("title"); dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); //进度条类型(长条型,圆圈型等) dialog.setMax(100); dialog.incrementProgressBy(20); //自定了当前进度(一般要通过具体业务实现) return dialog; } return super.onCreateDialog(id); } }
二.创建一个Dialog再进行调用
在values文件夹下的styles文件中配置
<style name="MyDialog" parent="@android:Theme.Dialog"> <item name="android:windowFrame">@null</item> <item name="android:windowNoTitle">true</item> //设置Dialog无标题 <item name="android:windowBackground">@color/blue</item> //设置背景 <item name="android:windowIsFloating">true</item> //设置Dialog悬浮 <item name="android:windowContentOverlay">@null</item> </style>
创建自定义Dialog类
public class FriendsMsgDialog extends Dialog {
Context context;
int position;
MyApplication application;
ImageView icon;
TextView nickname;
TextView address;
TextView sex;
TextView phone;
JSONArray array;
LayoutInflater inflater;
//重载构造函数
public FriendsMsgDialog(Context context, int theme,int position,JSONArray array) {
super(context, theme);
this.context=context;
this.position=position;
this.array=array;
application=(MyApplication) context.getApplicationContext();
inflater=LayoutInflater.from(context);
}
//要实现onCreate(Bundle savedInstanceState) 方法
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); View view=inflater.inflate(R.layout.friends_msg,null); JSONObject obj; try { obj = (JSONObject) array.get(position); //获取自定义Dialog界面上的各个组件 icon=(ImageView) view.findViewById(R.id.img_dtlfriends_icon); nickname=(TextView) view.findViewById(R.id.tv_dtlfriends_nickname); address=(TextView) view.findViewById(R.id.tv_dtlfriends_address); sex=(TextView) view.findViewById(R.id.tv_dtlfriends_sex); phone=(TextView) view.findViewById(R.id.tv_dtlfriends_phone); //给各组件赋值(通过具体业务) nickname.setText(obj.getString("nick_name")); address.setText(obj.getString("address")); sex.setText(obj.getString("sex")); phone.setText(obj.getString("phone")); GetIcon.getview(obj.getString("icon"), icon); } catch (JSONException e) { e.printStackTrace(); } setContentView(view); } }
调用Diaog
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { //长按listView的某一项显示Dialog if(dialog==null){ dialog=new FriendsMsgDialog(context, R.style.MyDialog,position,array); dialog.show(); }else dialog.show(); return true;
系统消息通知:
NotificationManager nm; findViewById(R.id.button6).setOnClickListener(new Button.OnClickListener(){ @Override public void onClick(View arg0) { installNotificaiton(); } }); private void installNotificaiton() { //获取系统 通知服务 nm=(NotificationManager)getSystemService(NOTIFICATION_SERVICE); //创建一个通知 Notification nf=new Notification(); //设置状态text nf.tickerText="new messages incoming..."; //设置时间 nf.when=System.currentTimeMillis(); //设置icon nf.icon=R.drawable.ic_launcher; //设置特性 nf.flags=nf.FLAG_AUTO_CANCEL ; //点击消息跳转到MainActivity PendingIntent pi=PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class ),PendingIntent.FLAG_CANCEL_CURRENT); nf.setLatestEventInfo(this, "title", "somebody send you msg", pi); nm.notify(2014, nf); }