?
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; } } }?
? ?效果圖:
?
?
?
?
第二種情況:
?
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中獲取控件,并綁定事件
? 效果:
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
