android中定制的dialog
16lz
2021-12-04
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;}@Overridepublic 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中获取控件,并绑定事件
效果:
更多相关文章
- Android判断当前的Activity
- [cocos2d-x 学习] Scene(场景)学习
- Android之SeekBar与RatingBar简单实例
- android取得当前网络连接状态
- Android(安卓)常用的SDCARD和内存操作
- Android(安卓)MediaPlayer基本知识
- Android(安卓)TextView自带padding 10dp问题
- android 根据TextView宽度 自动缩小字体大小 以适应其宽度
- android监听当前应用