Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Dialog在android中的應用(一)

Dialog在android中的應用(一)

編輯:關於Android編程

如果做一個如下圖的Dialog,首先要定義樣式:

 


定義一個基類,基類裡面有一些基本方法:

 

 

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();
    }

}


 

然後是具體實現類:

 

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);
    }

}


 

 

 

 

 

 

\

 

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved