在這公司上班也是醉了,1個(gè)產(chǎn)品公司不大利于程序員的發(fā)展,最主要的是公司不關(guān)心員工的成長(zhǎng),每天就知道在公司優(yōu)化代碼和換下公司的界面等1些繁瑣的事情,完全是在浪費(fèi)時(shí)間,倒不如學(xué)1些新的東西,今天學(xué)ios的時(shí)候發(fā)現(xiàn)了qq5.0版的那個(gè)退出程序時(shí)的上彈提示菜單欄,之前也就是用popwindow來實(shí)現(xiàn)的,今天看ios的代碼實(shí)現(xiàn)起來確切是如此的簡(jiǎn)單,也就是已封裝好的1個(gè)控件UIActionSheet,想起安卓實(shí)現(xiàn)這功能是如此的操蛋,不能不覺得iphoe真的是有很大優(yōu)勢(shì)的,特別是去年6月出的swift語言,聽說是比oc簡(jiǎn)單的多,學(xué)ios也是辛苦的,今天我也來實(shí)現(xiàn)1個(gè)安卓版的actionSheet。
先看下ios的實(shí)現(xiàn)這功能的截圖和代碼:
是否是很漂亮的界面,再看代碼:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window=[[UIWindow alloc]initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor=[UIColor whiteColor];
[self.window makeKeyAndVisible];
self.button=[UIButton buttonWithType:UIButtonTypeRoundedRect];
self.button.frame=CGRectMake(50, 250, 280, 50);
[self.button setTitle:@"show actionSheet" forState:UIControlStateNormal];
[self.button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
// self.button.font = [UIFont fontWithName:@"STHeiti-Medium.ttc" size:10];
self.button.backgroundColor=[UIColor blueColor];
[self.button addTarget:self action:@selector(click) forControlEvents:UIControlEventTouchUpInside];
[self.window addSubview:self.button];
return YES;
}
-(void)click{
//底部彈出dialog
UIActionSheet *sheet=[[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"發(fā)送給好友" otherButtonTitles:@"轉(zhuǎn)載到空間相冊(cè)",@"上傳到群相冊(cè)",@"保存得手機(jī)",@"收藏",@"查看聊天圖片",nil];
[sheet showInView:self.window];
}
由因而初學(xué)者,所以用的是代碼添加控件的方式
然后下面是安卓來實(shí)現(xiàn):
actionSheet.java
package com.zy.actionsheet;
import java.util.ArrayList;
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;
/**
* 自定義的actionSheet,有3部份組成(1.title 2.item 3.cancle按鈕)
* 當(dāng)item 的條目多過8條時(shí),item就可以轉(zhuǎn)動(dòng)顯示
* */
public class ActionSheet {
private Context context;
private Dialog dialog;
private TextView txt_title;//標(biāo)題,默許是沒有標(biāo)題的
private TextView txt_cancel;//取消按鈕
private LinearLayout lLayout_content;
private ScrollView sLayout_content;
private boolean showTitle = false;
private List<SheetItem> sheetItems;
private Display display;
public ActionSheet(Context context) {
this.context = context;
// 獲得window對(duì)象
WindowManager windowManager = (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
//得到窗口的尺寸對(duì)象
display = windowManager.getDefaultDisplay();
}
// 為actionSheet構(gòu)建自定義的控件
public ActionSheet builder() {
// 獲得Dialog布局
View view = LayoutInflater.from(context).inflate(
R.layout.view_actionsheet, null);
// 設(shè)置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);
//單獨(dú)設(shè)置取消按鈕的點(diǎn)擊事件
txt_cancel.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
// 定義Dialog布局和參數(shù)
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;
}
//為actionSheet設(shè)置標(biāo)題
public ActionSheet setTitle(String title) {
showTitle = true;
txt_title.setVisibility(View.VISIBLE);
txt_title.setText(title);
return this;
}
// 設(shè)置dialog是不是能夠取消
public ActionSheet setCancelable(boolean cancel) {
dialog.setCancelable(cancel);
return this;
}
// 設(shè)置dialog在屏幕外部是不是能夠取消
public ActionSheet setCanceledOnTouchOutside(boolean cancel) {
dialog.setCanceledOnTouchOutside(cancel);
return this;
}
/**
*
* @param strItem
* 條目名稱
* @param color
* 條目字體色彩,設(shè)置null則默許藍(lán)色
* @param listener
* @return actionSheet
*/
public ActionSheet addSheetItem(String strItem, SheetItemColor color,
OnSheetItemClickListener listener) {
if (sheetItems == null) {
sheetItems = new ArrayList<SheetItem>();
}
sheetItems.add(new SheetItem(strItem, color, listener));
return this;
}
/** 設(shè)置條目布局 */
@SuppressWarnings("deprecation")
private void setSheetItems() {
if (sheetItems == null || sheetItems.size() <= 0) {
return;
}
int size = sheetItems.size();//除去title和cancle 按鈕顯示的總條目數(shù)
// 添加條目過量的時(shí)候控制高度
if (size >= 8) {//當(dāng)條目過剩8條時(shí)就能夠轉(zhuǎn)動(dòng)顯示
LinearLayout.LayoutParams params = (LayoutParams) sLayout_content
.getLayoutParams();
//我這里設(shè)置的item的課顯示總高度為屏幕高度的3/5
params.height = display.getHeight()*3 / 5;
sLayout_content.setLayoutParams(params);
}
// 循環(huán)添加條目
for (int i = 1; i <= size; i++) {
final int index = i;
SheetItem sheetItem = sheetItems.get(i - 1);
String strItem = sheetItem.name;
SheetItemColor color = sheetItem.color;
final OnSheetItemClickListener listener = (OnSheetItemClickListener) sheetItem.itemClickListener;
TextView textView = new TextView(context);
textView.setText(strItem);
textView.setTextSize(18);
textView.setGravity(Gravity.CENTER);
// 背景圖片
if (size == 1) {
if (showTitle) {
textView.setBackgroundResource(R.drawable.actionsheet_bottom_selector);
} else {
textView.setBackgroundResource(R.drawable.actionsheet_single_selector);
}
} else {
if (showTitle) {
if (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);
}
}
}
// 字體色彩
if (color == null) {
textView.setTextColor(Color.parseColor(SheetItemColor.Blue
.getName()));
} else {
textView.setTextColor(Color.parseColor(color.getName()));
}
// 高度
float scale = context.getResources().getDisplayMetrics().density;//獲得屏幕密度
int height = (int) (45 * scale + 0.5f);
textView.setLayoutParams(new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT, height));//textView設(shè)置寬高
// 點(diǎn)擊事件
textView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
listener.onClick(index);
dialog.dismiss();
}
});
lLayout_content.addView(textView);
}
}
public void show() {
setSheetItems();
dialog.show();
}
//條目的接口回調(diào)類
public interface OnSheetItemClickListener {
void onClick(int which);
}
public class SheetItem {
String name;
OnSheetItemClickListener itemClickListener;
SheetItemColor color;
public SheetItem(String name, SheetItemColor color,
OnSheetItemClickListener itemClickListener) {
this.name = name;
this.color = color;
this.itemClickListener = itemClickListener;
}
}
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;
}
}
}
mainActivity.java
package com.zy.actionsheet;
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.zy.actionsheet.ActionSheet.OnSheetItemClickListener;
import com.zy.actionsheet.ActionSheet.SheetItemColor;
public class MainActivity extends Activity implements OnSheetItemClickListener{//實(shí)現(xiàn)條目的點(diǎn)擊
private Button btn1;
private ActionSheet actionSheet;
@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(new OnClickListener() {
@Override
public void onClick(View v) {
actionSheet=new ActionSheet(MainActivity.this)
.builder()
.setCancelable(false)
.setCanceledOnTouchOutside(false)
.addSheetItem("發(fā)送給好友", SheetItemColor.Blue,
MainActivity.this)
.addSheetItem("轉(zhuǎn)載到空間相冊(cè)", SheetItemColor.Blue,
MainActivity.this)
.addSheetItem("上傳到群相冊(cè)", SheetItemColor.Blue,
MainActivity.this)
.addSheetItem("保存得手機(jī)", SheetItemColor.Blue,
MainActivity.this)
.addSheetItem("收藏", SheetItemColor.Blue,
MainActivity.this)
.addSheetItem("查看聊天圖片", SheetItemColor.Blue,
MainActivity.this);
actionSheet.show();
}
});
}
@Override
public void onClick(int which) {
Toast.makeText(MainActivity.this,"我被點(diǎn)擊了"+which, 1).show();
}
}
弄了下,很簡(jiǎn)單的代碼也就不需要介紹了,下面直接上圖看效果
我也是醉了,截屏工具出問題,手機(jī)拍照的
源碼呢,在這》》》》》》》戳actionSheet下載