安卓版actionSheet的實現,安卓actionsheet

來源:互聯網
上載者:User

安卓版actionSheet的實現,安卓actionsheet
               actionSheet實現華麗的popWindow效果

            在這公司上班也是醉了,一個產品公司不大利於程式員的發展,最主要的是公司不關心員工的成長,每天就知道在公司最佳化代碼和換下公司的介面等一些繁瑣的事情,完全是在浪費時間,倒不如學一些新的東西,今天學ios的時候發現了qq5.0版的那個退出程式時的上彈提示功能表列,以前也就是用popwindow來實現的,今天看ios的代碼實現起來確實是如此的簡單,也就是已經封裝好的一個控制項UIActionSheet,想起安卓實現這功能是如此的操蛋,不得不覺得iphoe真的是有很大優勢的,特別是去年6月出的swift語言,據說是比oc簡單的多,學ios也是辛苦的,今天我也來實現一個安卓版的actionSheet。

先看下ios的實現這功能的和代碼:

                                    

是不是很漂亮的介面,再看代碼:

- (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:@"發送給好友" otherButtonTitles:@"轉載到空間相簿",@"上傳到群相簿",@"儲存到手機",@"收藏",@"查看聊天圖片",nil];
    [sheet showInView:self.window];
}

由於是初學者,所以用的是代碼添加控制項的方式

然後下面是安卓來實現:

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,有三部分組成(1.title   2.item  3.cancle按鈕)
 * 當item 的條目多過8條時,item就能滾動顯示
 * */
public class ActionSheet {
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<SheetItem> sheetItems;
private Display display;


public ActionSheet(Context context) {
this.context = context;
// 取得window對象
WindowManager windowManager = (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
//得到視窗的尺寸對象
display = windowManager.getDefaultDisplay();
}


// 為actionSheet構建自訂的控制項
public ActionSheet 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;
}
    //為actionSheet設定標題
public ActionSheet setTitle(String title) {
showTitle = true;
txt_title.setVisibility(View.VISIBLE);
txt_title.setText(title);
return this;
}
// 設定dialog是否能夠取消
public ActionSheet setCancelable(boolean cancel) {
dialog.setCancelable(cancel);
return this;
}


// 設定dialog在螢幕外部是否能夠取消
public ActionSheet setCanceledOnTouchOutside(boolean cancel) {
dialog.setCanceledOnTouchOutside(cancel);
return this;
}


/**

* @param strItem
*            條目名稱
* @param color
*            條目字型顏色,設定null則預設藍色
* @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;
}


/** 設定條目布局 */
@SuppressWarnings("deprecation")
private void setSheetItems() {
if (sheetItems == null || sheetItems.size() <= 0) {
return;
}
int size = sheetItems.size();//除去title和cancle 按鈕顯示的總條目數
// 添加條目過多的時候控制高度
if (size >= 8) {//當條目多餘八條時就可以滾動顯示
LinearLayout.LayoutParams params = (LayoutParams) sLayout_content
.getLayoutParams();
//我這裡設定的item的課顯示總高度為螢幕高度的3/5
params.height = display.getHeight()*3 / 5;
sLayout_content.setLayoutParams(params);
}


// 迴圈添加條目
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設定寬高


// 點擊事件
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();
}

        //條目的介面回調類
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{//實現條目的點擊
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("發送給好友", SheetItemColor.Blue,
MainActivity.this)
.addSheetItem("轉載到空間相簿", SheetItemColor.Blue,
MainActivity.this)
.addSheetItem("上傳到群相簿", SheetItemColor.Blue,
MainActivity.this)
.addSheetItem("儲存到手機", 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,"我被點擊了"+which, 1).show();
}
}

搞了下,很簡單的代碼也就不需要介紹了,下面直接看效果

我也是醉了,截屏工具出問題,手機拍照的

源碼呢,在這》》》》》》》戳actionSheet下載





聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.