Application of Dialog in android (1)

Source: Internet
Author: User

Application of Dialog in android (1)

If you want to create a Dialog, you must first define the style:

 



Define a base class. There are some basic methods in the base class:

 

 

public abstract class DropDownFilterDialog extends Dialog {    protected ArrayList
 
   mTopMenuList;    OnDropdownClickListener mDropdownClickListener;    protected LinearLayout mDropDownMenuLayout;    public Context mContext;    private int mCurrentIndex = 0;    public void setCurrentIndex(int mCurrentIndex) {        this.mCurrentIndex = mCurrentIndex;    }    public void setTopMenuItemList(ArrayList
  
    mTopMenuList) {        this.mTopMenuList = mTopMenuList;    }    public DropDownFilterDialog(Context context) {       
    super(context, R.style.DropdownFilterDialogStyle);        mContext = context;    }    public interface OnDropdownClickListener {        void onDropdownHide();        void onClickItem(int index);    }    public void setDropDownClickListener(OnDropdownClickListener dropdownClickListener) {        mDropdownClickListener = dropdownClickListener;    }    public abstract void hideDropDownFilter(boolean showAnimation);    public abstract void showDialog(boolean showAnimation, View banner);    public void init() {        mDropDownMenuLayout = (LinearLayout) findViewById(R.id.text_chat_menu_linear);        constructButton(mTopMenuList);    }    @Override    protected void onStart() {        super.onStart();        init();    }    public void constructButton(ArrayList
   
     list) {        mDropDownMenuLayout.removeAllViews();        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(                WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);//        mDropDownMenuLayout.addView(View.inflate(mContext,//                R.layout.view_top_menu_separator, null), params);        final int size = list.size();        final int lastPosition = size - 1;        for (int i = 0; i < size; i++) {            DropDownItem item = list.get(i);            View view = View.inflate(mContext, R.layout.dropdown_menu_item_layout,                    null);            view.setTag(i);            view.setOnClickListener(new View.OnClickListener() {                @Override                public void onClick(View view) {                    hideDropDownFilter(true);                    mDropdownClickListener.onClickItem((Integer) view.getTag());                }            });            TextView btn = (TextView) view                    .findViewById(R.id.top_menu_button);            btn.setText(item.getName());            btn.setTag(item.getIndex());            if (mCurrentIndex == i) {                btn.setTextColor(mContext.getResources().getColor(R.color.contact_detail_phone_related_color_pressed));            }            TextView tab_indicator_counter = (TextView) view.findViewById(R.id.indicator_counter);            if (item.getCouter() == 0) {                tab_indicator_counter.setVisibility(View.GONE);            } else {                tab_indicator_counter.setText(String.valueOf(item.getCouter()));            }            boolean showSeparator = true;            if (size == 1) {                showSeparator = false;            } else if (i == lastPosition) {                showSeparator = false;            }            view.setBackgroundResource(R.drawable.drop_down_filter_item_background_selector);            mDropDownMenuLayout.addView(view, params);            if (showSeparator) {                LinearLayout.LayoutParams paramsForLine = new LinearLayout.LayoutParams(                        WindowManager.LayoutParams.MATCH_PARENT, mContext.getResources().getDimensionPixelSize(R.dimen.top_menu_vertical_separator));                paramsForLine.leftMargin = mContext.getResources().getDimensionPixelSize(R.dimen.drop_down_menu_item_margin_left_right);                paramsForLine.rightMargin = paramsForLine.leftMargin;                mDropDownMenuLayout.addView(View.inflate(mContext,                        R.layout.drop_down_menu_divider_layout, null), paramsForLine);            }        }        mDropDownMenuLayout.invalidate();    }}
   
  
 


 

Then there is a specific implementation class:

 

public class DropDownFilterDialogForTablet extends DropDownFilterDialog {    public DropDownFilterDialogForTablet(Context context) {        super(context);        mContext = context;        Window window = getWindow();        WindowManager.LayoutParams lp = window.getAttributes();        lp.gravity = Gravity.CENTER_HORIZONTAL | Gravity.TOP;    }    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.drop_down_filter_dialog);    }    @Override    protected void onStart() {        super.onStart();        init();    }    public void hideDropDownFilter(boolean showAnimation) {        dismiss();        mDropdownClickListener.onDropdownHide();    }    public void init() {        super.init();        mDropDownMenuLayout.setBackgroundResource(R.drawable.dropdown_filters_background);    }    public void showDialog(boolean showAnimation, View banner) {        Window window = getWindow();        WindowManager.LayoutParams lp = window.getAttributes();        int height = mContext.getResources().getDimensionPixelSize(R.dimen.drop_down_menu_item_margin_top_for_tablet);        if (banner == null) {            lp.y = height;        } else {            lp.y = height + banner.getMeasuredHeight();        }        show();    }    @Override    public boolean onTouchEvent(MotionEvent event) {        if (event.getAction() == MotionEvent.ACTION_DOWN) {            hideDropDownFilter(true);            return true;        }        return false;    }    @Override    public boolean dispatchKeyEvent(KeyEvent event) {        if (event.getAction() == KeyEvent.ACTION_DOWN                && event.getKeyCode() == KeyEvent.KEYCODE_BACK) {            hideDropDownFilter(true);            return true;        }        return super.dispatchKeyEvent(event);    }}


 

 

 

 

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.