仿iphone actionsheet_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > 仿iphone actionsheet

仿iphone actionsheet

 2013/9/12 18:52:43  yi_17328214  程序员俱乐部  我要评论(0)
  • 摘要:publicclassActionSheetimplementsOnClickListener{privateContextcontext;privateLinearLayoutlayout;privateViewactionsheetView;privateTextViewtitle;privateButtondeleteButton,cancelButton;privateActionSheetButtonClickListeneraslistener
  • 标签:iphone
public class ActionSheet implements OnClickListener {

private Context context;
private LinearLayout layout;
private View actionsheetView;
private TextView title;
private Button deleteButton, cancelButton;
private ActionSheetButtonClickListener aslistener;

/**
* This creates a ActionSheet
* @param context
* @param layout
*/
public ActionSheet(Context context, LinearLayout layout) {
this.context = context;
this.layout = layout;
init();
}

private void init() {
LayoutInflater inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.actionsheetView = inflater.inflate(R.layout.action_sheet, null);

this.deleteButton = (Button) this.actionsheetView.findViewById(R.id.action_sheet_delete);
this.cancelButton = (Button) this.actionsheetView.findViewById(R.id.action_sheet_cancel);
this.title = (TextView) this.actionsheetView.findViewById(R.id.action_sheet_title);
}

/**
* This
* @param listener
*/
public void setOnButtonClickListener(ActionSheetButtonClickListener listener) {
this.aslistener = listener;
}

/**
* This action sheet
* @param title
*/
public void show(String title) {
this.title.setText(title);
WindowManager.LayoutParams lp = new WindowManager.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.TYPE_APPLICATION, WindowManager.LayoutParams.FLAG_FULLSCREEN | WindowManager.LayoutParams.FLAG_BLUR_BEHIND,
PixelFormat.TRANSLUCENT);

this.deleteButton.setOnClickListener(this);
this.cancelButton.setOnClickListener(this);
this.actionsheetView.setSoundEffectsEnabled(false);
this.actionsheetView.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
hide();
}
});
this.layout.addView(this.actionsheetView, lp);
Animation animIn = AnimationUtils.loadAnimation(this.context, R.anim.push_up_in);
animIn.setDuration(200);
animIn.setAnimationListener(new AnimationListener() {

@Override
public void onAnimationStart(Animation animation) {

}

@Override
public void onAnimationRepeat(Animation animation) {

}

@Override
public void onAnimationEnd(Animation animation) {
Activity parent = (Activity) ActionSheet.this.context;
// using the activity, get Window reference
// Window window = parent.getWindow();
parent.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
parent.getWindow().setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND, WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
TransitionDrawable transition = (TransitionDrawable) ActionSheet.this.actionsheetView.getBackground();
transition.startTransition(100);
}
});
this.actionsheetView.setAnimation(animIn);

this.layout.bringChildToFront(this.actionsheetView);
}

/**
* This hide action sheet
*/
public void hide() {
Animation animOut = AnimationUtils.loadAnimation(this.context, R.anim.push_down_out);
animOut.setDuration(200);
animOut.setAnimationListener(new AnimationListener() {

@Override
public void onAnimationStart(Animation animation) {
TransitionDrawable transition = (TransitionDrawable) ActionSheet.this.actionsheetView.getBackground();
transition.resetTransition();
}

@Override
public void onAnimationRepeat(Animation animation) {

}

@Override
public void onAnimationEnd(Animation animation) {
ActionSheet.this.layout.removeView(ActionSheet.this.actionsheetView);

}
});
this.actionsheetView.setAnimation(animOut);
this.actionsheetView.startAnimation(animOut);
}

/**
* {@inheritDoc}
* @see android.view.View.OnClickListener#onClick(android.view.View)
*/
@Override
public void onClick(View v) {
if (v.getId() == R.id.action_sheet_delete) {
this.aslistener.onButtonClick(this, 0);
} else {
this.aslistener.onButtonClick(this, 1);
}

}
}



public interface ActionSheetButtonClickListener {

/**
* This actionsheet button call back
* @param index
*/
public void onButtonClick(ActionSheet actionsheet, int index);

}



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="@drawable/action_sheet_background"
android:gravity="bottom"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="@drawable/action_sheet_bg"
android:gravity="center_horizontal"
android:orientation="vertical" >
<TextView
android:id="@+id/action_sheet_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:textColor="@color/white"
android:textSize="20dp" />
<Button
android:id="@+id/action_sheet_delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:background="@drawable/action_sheet_delete_button"
android:text="@string/fav_action_sheet_delete"
android:textColor="@color/black"
android:textSize="20dp" />
<Button
android:id="@+id/action_sheet_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:background="@drawable/action_sheet_cancel_button"
android:text="@string/cancel"
android:textColor="@color/white"
android:textSize="20dp" />
</LinearLayout>
</LinearLayout>


action_sheet_bg


<?xml version="1.0" encoding="utf-8"?>
<transition xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- The drawables used here can be solid colors, gradients, shapes, images, etc. -->
<item android:drawable="@android:color/transparent"/>
<item android:drawable="@color/action_sheet_background"/>
</transition>


action_sheet_cancel_button
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/action_sheet_delete_clicked" android:state_pressed="true"/> <!-- pressed -->
<item android:drawable="@drawable/action_sheet_delete_clicked" android:state_focused="true"/> <!-- focused -->
<item android:drawable="@drawable/action_sheet_delete"/> <!-- default -->
</selector>


action_sheet_delete_button
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/action_sheet_cancel_clicked" android:state_pressed="true"/> <!-- pressed -->
<item android:drawable="@drawable/action_sheet_cancel_clicked" android:state_focused="true"/> <!-- focused -->
<item android:drawable="@drawable/action_sheet_cancel"/> <!-- default -->
</selector>
发表评论
用户名: 匿名