对话框

来源:互联网 发布:开源证券交易软件 编辑:程序博客网 时间:2024/06/14 17:29

对话框

定制Toast
AlertDialog
PopupWindow
Menu
actionbar Menu


定制Toast
设置位置
toast.setGravity(Gravity.BOTTOM,offsetX,offsetY);
设置外观(利用Layout)
toast.setView();
toast.show();//显示
private void toast_deal(){
Toast toast=null;
toast=Toast.makeText(this, "_", Toast.LENGTH_LONG);
LinearLayout ll=(LinearLayout) LayoutInflater.from(this)
.inflate(R.layout.toast_demo, null);//toast的布局文件
tvText=(TextView) ll.findViewById(R.id.tvText);
tvText.setText("收藏成功");
toast.setView(ll);
int offsetX=0;
int offsetY=0;
toast.setGravity(Gravity.CENTER, offsetX, offsetY);//
toast.show();
}

AlertDialog
简单的一种对话框,主要的目的是为用户显示一条警告信息
通过AlertDialog.Builder产生AlertDialog
如何实现定制AlertDialog
setView()方式
getWindow().setContenView()方式

private void showClearDialog(){Drawable icon=super.getResources().getDrawable(R.drawable.ic_launcher);Dialog dialog=new AlertDialog.Builder(this).setTitle("确认信息").setMessage("确认清除缓存吗?").setIcon(icon).setPositiveButton("确定", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface arg0, int arg1) {//清除缓存arg0.dismiss();//关闭对话框}}).setNegativeButton("取消", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface arg0, int arg1) {}}).create();dialog.show();}
private void showSelectColor(){Dialog dialog=new AlertDialog.Builder(this).setTitle("选择颜色").setItems(R.array.select_color,new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface arg0, int arg1) {setTxtColor(arg1);arg0.dismiss();}}).create();dialog.show();}private void setTxtColor(int idx){int colorVal=0;switch(idx){case 0:colorVal=Color.RED;break;case 1:colorVal=Color.GREEN;break;case 2:colorVal=Color.BLUE;break;case 3:colorVal=Color.MAGENTA;break;case 4:colorVal=Color.LTGRAY;break;}btSelectColor.setTextColor(colorVal);}
private void showExitDialog(){final Dialog dialog=new AlertDialog.Builder(this).create();dialog.show();Window win=dialog.getWindow();win.setContentView(R.layout.exit_layout);//对话框的布局win.findViewById(R.id.button2).setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {dialog.dismiss();finish();}});}

PopupWindow

http://blog.csdn.net/chengtaoyan/article/details/49247835


Menu

http://blog.csdn.net/chengtaoyan/article/details/49249137


1 0