用Builder模式從新打造一個dialog,案例中有兩種Builder,分別是CommonBuilder和MDBuilder,若是還想實現其餘的通用dialog,繼承自FRBaseDialogBuilder便可。git
private void showCommonDialog() { final FRDialog dialog = new FRDialog.CommonBuilder(this) .setContentView(R.layout.dialog_common) .setText(R.id.dcu_tv_cancel, "否") .setText(R.id.dcu_tv_confirm, "是") .setText(R.id.dcu_tv_title, "舒適提示") .setText(R.id.dcu_tv_content, "1.文字文字我是文字文字文字我是文字文字文字我是文字!\n2.文字文字文字文字文字\n3.文字文字文字文字文字") .setDefaultAnim() .show(); dialog.setText(R.id.dcu_tv_confirm, "肯定"); dialog.setOnClickListener(R.id.dcu_tv_cancel, new FRDialogClickListener() { @Override public boolean onDialogClick(View view) { Toast.makeText(MainActivity.this, "點擊了否", Toast.LENGTH_SHORT).show(); return true; } }); dialog.setOnClickListener(R.id.dcu_tv_confirm, new FRDialogClickListener() { @Override public boolean onDialogClick(View v) { Toast.makeText(MainActivity.this, "點擊了是", Toast.LENGTH_SHORT).show(); return false; } }); }
private void showMDDialog() { FRDialog dialog = new FRDialog.MDBuilder(this) .setMessage("1.文字文字我是文字文字文字我是文字文字文字我是文字!\n2.文字文字文字文字文字\n3.文字文字文字文字文字") .setTitle("舒適提示") .setNegativeAndPositive("否", "是") .setPositiveListener(new FRDialogClickListener() { @Override public boolean onDialogClick(View view) { return true; } }).show(); }
繼承全部dialog的設置,同時還能夠自定義如下設置github
//設置寬度全屏 dialog.setFullWidth() //設置從底部彈出 dialog.setFromBottom() //設置彈出動畫 dialog.setAnimation(int anim)
將mWidth改爲mWidthOffset,不讓用戶設置一個具體的寬度,而是讓用戶去設置一個寬度比例,而後經過改變window的LayoutParams來設置dialog的寬高:ide
WindowManager.LayoutParams lp = window.getAttributes(); lp.width = (int) (baseBuilder.mContext.getResources().getDisplayMetrics().widthPixels * baseBuilder.mWidthOffset); lp.height = baseBuilder.mHeight; window.setAttributes(lp);
用法仍是和以前同樣:佈局
dialog.setWidthOffset(0——1) 默認是0.9
新增點擊dialog中非EditText區域隱藏軟鍵盤動畫
重寫dispatchTouchEvent方法進行攔截: /** * 點擊dialog中除EditText之外的區域隱藏軟鍵盤 * * @param ev * @return */ @Override public boolean dispatchTouchEvent(@NonNull MotionEvent ev) { FRInputMethodManager.autoHideSoftInput(this, ev); return super.dispatchTouchEvent(ev); } //核心方法 public static boolean isAutoHideSoftInput(View view, MotionEvent event) { if (event.getAction() != MotionEvent.ACTION_DOWN) { return false; } if (!(view instanceof EditText)) { return false; } float x = event.getX(); float y = event.getY(); int[] location = {0, 0}; view.getLocationInWindow(location); int left = location[0]; int top = location[1]; int bottom = top + view.getHeight(); int right = left + view.getWidth(); if (left <= x && x < right && top <= y && y < bottom) { // 點擊事件在EditText的區域裏 return false; } return true; }
用法不變。ui
新增recyclerview樣式的dialogBuilderthis
具體功能有:spa
用法:code
private void showRecyclerViewDialog() { List<Object> mDataList = new ArrayList<>(); mDataList.add(new TestDataBean("張三", "2018-09-11 14:00")); mDataList.add(new TestDataBean("李四", "2018-09-11 11:00")); mDataList.add(new TestDataBean("王五", "2018-09-11 12:00")); mDataList.add(new TestDataBean("李四", "2018-09-11 13:00")); mDataList.add(new TestDataBean("張三", "2018-09-11 16:00")); mDataList.add(new TestDataBean("王五", "2018-09-11 15:00")); final FRDialog dialog = new FRDialog.RecyclerViewBuilder(this) .setLayoutManager(new LinearLayoutManager(MainActivity.this)) .setAdapter(new FRBaseDialogAdapter<TestDataBean>(MainActivity.this) { @Override protected int getLayoutRes() { return R.layout.item_test; } @Override protected void convert(FRBaseDialogViewHolder holder, TestDataBean dataBean, int position, List<Object> payloads) { holder.setImageResource(R.id.it_iv_image, R.mipmap.ic_launcher_round); holder.setText(R.id.it_tv_title, dataBean.getName()); holder.setText(R.id.it_tv_time, dataBean.getTime()); } }).setDataList(mDataList) .setHeightOffset(0.5) .addRecyclerViewHeader(R.layout.layout_header) .addDialogFooter(R.layout.layout_footer) .setOnClickListener(R.id.lf_tv_cancel, view -> true) .setOnClickListener(R.id.lf_tv_confirm, view -> { Toast.makeText(MainActivity.this, "點擊了肯定", Toast.LENGTH_SHORT).show(); return false; }) .show(); }
詳細能夠參照demo中的。繼承