android實現 仿iphoneDialog實現類似於iphone對話框樣式

續: 話說道哥跟發哥在上海柯炫做Android開發,看來跟發哥差距很大。總結瞭下發哥的技術拿來這裡裝逼用用。

參考這裡: https://blog.csdn.net/xiechengfa/article/details/40373331

package com.zf.iosdialog;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

import com.zf.iosdialog.bean.SheetItem;
import com.zf.iosdialog.widget.IosDialog;

public class MainActivity extends Activity implements OnClickListener, OnSheetMyItemClickListner {
	private Button btn1;
	private Button btn2;
	private Button btn3;
	private Button btn4;
	private Button btn5;

	/***
	 * weixinNumber: lixiaodaoaaa weibo:www.weibo.com/lixiaodaoaaa thanks
	 * :xiechengfa
	 */

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initView();
	}

	private void initView() {
		btn1 = (Button) findViewById(R.id.btn1);
		btn1.setOnClickListener(this);
		btn2 = (Button) findViewById(R.id.btn2);
		btn2.setOnClickListener(this);
		btn3 = (Button) findViewById(R.id.btn3);
		btn3.setOnClickListener(this);
		btn4 = (Button) findViewById(R.id.btn4);
		btn4.setOnClickListener(this);
		btn5 = (Button) findViewById(R.id.btn5);
		btn5.setOnClickListener(this);
	}
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.btn5 :
				IosDialog iosDialog = new IosDialog(MainActivity.this);
				ArrayList listSheetItems = new ArrayList();
				listSheetItems.add(new SheetItem("shabi001", 1));
				listSheetItems.add(new SheetItem("shabi002", 2));
				listSheetItems.add(new SheetItem("shabi003", 3));
				listSheetItems.add(new SheetItem("shabi004", 4));
				listSheetItems.add(new SheetItem("shabi005", 5));
				listSheetItems.add(new SheetItem("shabi006", 6));
				iosDialog.setSheetItems(listSheetItems, MainActivity.this);
				iosDialog.show();
				break;
			default :
				break;
		}
	}

	@Override
	public void onClickItem(int which) {
		switch (which) {
			case 1 :
				Toast.makeText(this, "lixiaodaoaaa001", Toast.LENGTH_SHORT).show();
				break;
			case 2 :
				Toast.makeText(this, "lixiaodaoaaa002", Toast.LENGTH_SHORT).show();
				break;
			case 3 :
				Toast.makeText(this, "lixiaodaoaaa003", Toast.LENGTH_SHORT).show();
				break;
			case 4 :
				Toast.makeText(this, "lixiaodaoaaa004", Toast.LENGTH_SHORT).show();
				break;
			case 5 :
				Toast.makeText(this, "lixiaodaoaaa005", Toast.LENGTH_SHORT).show();
				break;
			case 6 :
				Toast.makeText(this, "lixiaodaoaaa006", Toast.LENGTH_SHORT).show();
				break;
			default :
				break;
		}
	}

}

IOSDialog代碼(防這個效果)

package com.zf.iosdialog.widget;

import java.util.List;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ScrollView;
import android.widget.TextView;

import com.zf.iosdialog.OnSheetMyItemClickListner;
import com.zf.iosdialog.R;
import com.zf.iosdialog.bean.SheetItem;

public class IosDialog {
	private Context context;
	private Dialog dialog;
	private TextView txt_title;
	private TextView txt_cancel;
	private LinearLayout lLayout_content;
	private ScrollView sLayout_content;
	private boolean showTitle = false;
	private List listSheetItems;
	private Display display;

	public IosDialog(Context context) {
		this.context = context;
		WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
		display = windowManager.getDefaultDisplay();
		builder();
	}

	public IosDialog builder() {
		// 獲取Dialog佈局
		View view = LayoutInflater.from(context).inflate(R.layout.view_actionsheet, null);

		// 設置Dialog最小寬度為屏幕寬度
		view.setMinimumWidth(display.getWidth());

		// 獲取自定義Dialog佈局中的控件
		sLayout_content = (ScrollView) view.findViewById(R.id.sLayout_content);
		lLayout_content = (LinearLayout) view.findViewById(R.id.lLayout_content);
		txt_title = (TextView) view.findViewById(R.id.txt_title);
		txt_cancel = (TextView) view.findViewById(R.id.txt_cancel);
		txt_cancel.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				dialog.dismiss();
			}
		});

		// 定義Dialog佈局和參數
		dialog = new Dialog(context, R.style.ActionSheetDialogStyle);
		dialog.setContentView(view);
		Window dialogWindow = dialog.getWindow();
		dialogWindow.setGravity(Gravity.LEFT | Gravity.BOTTOM);
		WindowManager.LayoutParams lp = dialogWindow.getAttributes();
		lp.x = 0;
		lp.y = 0;
		dialogWindow.setAttributes(lp);

		return this;
	}

	public IosDialog setTitle(String title) {
		showTitle = true;
		txt_title.setVisibility(View.VISIBLE);
		txt_title.setText(title);
		return this;
	}

	public IosDialog setCancelable(boolean cancel) {
		dialog.setCancelable(cancel);
		return this;
	}

	public IosDialog setCanceledOnTouchOutside(boolean cancel) {
		dialog.setCanceledOnTouchOutside(cancel);
		return this;
	}

	/** 設置條目佈局 */
	public void setSheetItems(List sheetItemList, final OnSheetMyItemClickListner itemLisenner) {

		this.listSheetItems = sheetItemList;
		if (sheetItemList == null || sheetItemList.size() = 7) {
			LinearLayout.LayoutParams params = (LayoutParams) sLayout_content.getLayoutParams();
			params.height = display.getHeight() / 2;
			sLayout_content.setLayoutParams(params);
		}

		// 循環添加條目
		for (int i = 1; i = 1 && i < size) {
						textView.setBackgroundResource(R.drawable.actionsheet_middle_selector);
					} else {
						textView.setBackgroundResource(R.drawable.actionsheet_bottom_selector);
					}
				} else {
					if (i == 1) {
						textView.setBackgroundResource(R.drawable.actionsheet_top_selector);
					} else if (i < size) {
						textView.setBackgroundResource(R.drawable.actionsheet_middle_selector);
					} else {
						textView.setBackgroundResource(R.drawable.actionsheet_bottom_selector);
					}
				}
			}

			textView.setTextColor(Color.parseColor(SheetItemColor.Blue.getName()));

			// 高度
			float scale = context.getResources().getDisplayMetrics().density;
			int height = (int) (45 * scale + 0.5f);
			textView.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, height));

			// 點擊事件
			textView.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					itemLisenner.onClickItem(sheetItem.getITEM_CODE());
					dialog.dismiss();
				}
			});

			lLayout_content.addView(textView);
		}
	}

	public void show() {
		dialog.show();
	}

	public interface OnSheetItemClickListener {
		void onClick(int which);
	}

	public enum SheetItemColor {
		Blue("#037BFF"), Red("#FD4A2E");

		private String name;

		private SheetItemColor(String name) {
			this.name = name;
		}

		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}
	}
}

此demo下載地址1:猛擊這裡點壞瞭我賠錢啊^_^

道哥整理地址2:猛擊這裡,點壞瞭我也賠錢



發佈留言

發佈留言必須填寫的電子郵件地址不會公開。