android中定制的dialog

系统 1631 0

 

    public ShelfSwitchDialog(Context context) {
		super(context,android.R.style.Theme_NoTitleBar);
		setContentView(R.layout.newspaper_switch_dialog);
		this.context = context;
		Window win = getWindow();
		win.getAttributes().gravity = Gravity.TOP;
		win.setBackgroundDrawableResource(R.drawable.newspaper_blank);
		//大小
		win.setLayout(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
	}
  

    dialog的定制 :

    public class ShelfSwitchDialog extends Dialog implements OnClickListener{

    	Context context;
	ImageView yingyong,shudian,baojia;
	int selectIndex;
	String[] choiceItems = new String[]{"图书","报架"};
	
	public ShelfSwitchDialog(Context context,int paddingLeft){
		this(context);
		View v = findViewById(R.id.dialog_root_layout);
		v.setPadding(paddingLeft-50,50,v.getPaddingRight(),v.getPaddingBottom());
		yingyong = (ImageView)findViewById(R.id.yingyong);
		yingyong.setOnClickListener(this);
		shudian = (ImageView)findViewById(R.id.shudian);
		shudian.setOnClickListener(this);
		baojia = (ImageView)findViewById(R.id.baojia);
		baojia.setOnClickListener(this);
	}
	
	public ShelfSwitchDialog(Context context) {
		super(context,android.R.style.Theme_NoTitleBar);
		setContentView(R.layout.newspaper_switch_dialog);
		this.context = context;
		Window win = getWindow();
		win.getAttributes().gravity = Gravity.TOP;
		win.setBackgroundDrawableResource(R.drawable.newspaper_blank);
		//大小
		win.setLayout(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
	}
	public AlertDialog create(){
		if(context==null){return null;}
		final boolean instOfStore = context instanceof ShelfActivity||context instanceof BooksListActivity;
		final boolean instOfShelf = context instanceof NewspaperShelf;
		final Intent shelfIntent = new Intent(context,NewspaperShelf.class);
		final Intent storeIntent = new Intent(context,ShelfActivity.class);
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		AlertDialog dialog =  builder
        .setIcon(R.drawable.newspaper_ic_switch)
        .setTitle("切换到:")
        .setSingleChoiceItems(choiceItems, 0, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
            	selectIndex =  whichButton;
            }
        })
        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
        	
            public void onClick(DialogInterface dialog, int whichButton) {
//            	Log.v("","whichButton:"+selectIndex);
            	if(instOfStore){
            		switch(selectIndex){
            		case 0:
            			Toast.makeText(context,"当前已是书店",Toast.LENGTH_SHORT).show();
            			break;
            		case 1:
            			context.startActivity(shelfIntent);
            			break;
            		}
            	}
            	else if(instOfShelf){
            		switch(selectIndex){
            		case 0:
            			((ShelfActivity)context).finish();
            			break;
            		case 1:
            			Toast.makeText(context,"当前已是报架",Toast.LENGTH_SHORT).show();
            			break;
            		}
            	}
            }
        })
        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
            	
            }
        })
       .create();
		dialog.getWindow().getAttributes().gravity = Gravity.TOP;
		dialog.getWindow().setLayout(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
		return dialog;
	}

	@Override
	public void onClick(View v) {
		final boolean instOfShelf = context instanceof ShelfActivity;
		final boolean instOfNewspaperShelf = context instanceof NewspaperShelf;
		final boolean instOfStoreActivity = context instanceof StoreActivity;
		final boolean instOfBooksListActivity = context instanceof BooksListActivity;
		final Intent newspaperShelfIntent = new Intent(context,NewspaperShelf.class);
		final Intent shelfIntent = new Intent(context,ShelfActivity.class);
		switch(v.getId()){
		case R.id.yingyong:
			Intent intent = new Intent("cn.chutong.ereader.finishactivity");
			dismiss();
			context.sendBroadcast(intent);
			break;
		case R.id.shudian:
			if(instOfNewspaperShelf){
				((NewspaperShelf)context).finish();
				dismiss();
			}
			else if(instOfShelf||instOfBooksListActivity){
				Toast.makeText(context,"当前已是书店",Toast.LENGTH_SHORT).show();
				dismiss();
			}
			else if(instOfStoreActivity){
				dismiss();
				((StoreActivity)context).finish();
				context.startActivity(shelfIntent);
			}
			break;
		case R.id.baojia:
			if(instOfNewspaperShelf){
				Toast.makeText(context,"当前已是报架",Toast.LENGTH_SHORT).show();
				dismiss();
			}
			else if(instOfShelf||instOfBooksListActivity){
				context.startActivity(newspaperShelfIntent);
				dismiss();
			}
			else if(instOfStoreActivity){
				context.startActivity(newspaperShelfIntent);
				dismiss();
				((StoreActivity)context).finish();
			}
			break;
		
		}
	}
}
  
 

   效果图:

 

 


android中定制的dialog

 

 

第二种情况:

 

    addCustomDlg = new Dialog(this, R.style.dialog);
addCustomDlg.setContentView(R.layout.add);
addCustomDlg.show();
confirmBtn = (Button) addCustomDlg.findViewById(R.id.confirmBtn);
edit = (EditText) addCustomDlg.findViewById(R.id.txt);
edit.setText("");
btn_back = (ImageButton) addCustomDlg.findViewById(R.id.btn_back);
btn_back.setOnClickListener(this);
confirmBtn.setOnClickListener(this);
  

   直接从dialog中获取控件,并绑定事件

  效果:


android中定制的dialog

android中定制的dialog


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论