diff --git a/README.md b/README.md index d4567296..405bdafb 100644 --- a/README.md +++ b/README.md @@ -115,6 +115,22 @@ A message with a custom icon: .setCustomImage(R.drawable.custom_img) .show(); +A edit text dialog: + + new SweetAlertDialog(this, SweetAlertDialog.EDIT_TEXT_TYPE) + .setTitleText("Edit text") + .setContentText("Please enter the text.") + .show(); + +A list text dialog: + + new SweetAlertDialog(this, SweetAlertDialog.LIST_TYPE) + .setTitleText("List dialog") + .setContentText("Please select list item.") + .setList(list) + .setListTextSize(20) + .show(); + Bind the listener to confirm button: new SweetAlertDialog(this, SweetAlertDialog.WARNING_TYPE) @@ -129,6 +145,35 @@ Bind the listener to confirm button: }) .show(); +Bind the listener to edit text confirm button: + + new SweetAlertDialog(this, SweetAlertDialog.EDIT_TEXT_TYPE) + .setTitleText("Are you sure?") + .setTitleText("Edit text") + .setContentText("Please enter the text.") + .setEditTextConfirmClickListener(new SweetAlertDialog.OnEditTextSweetClickListener() { + @Override + public void onClick(SweetAlertDialog sDialog, String inputText) { + sDialog.dismissWithAnimation(); + } + }) + .show(); + +Bind the listener to list: + + new SweetAlertDialog(this, SweetAlertDialog.LIST_TYPE) + .setTitleText("List dialog") + .setContentText("Please select list item.") + .setList(list) + .setListTextSize(20) + .setListClickListener(new SweetAlertDialog.OnListSweetClickListener() { + @Override + public void onItemClick(SweetAlertDialog sDialog, int position, String listText) { + sDialog.dismissWithAnimation(); + } + }) + .show(); + Show the cancel button and bind listener to it: new SweetAlertDialog(this, SweetAlertDialog.WARNING_TYPE) diff --git a/README.zh.md b/README.zh.md index d114fbf1..0043afa9 100644 --- a/README.zh.md +++ b/README.zh.md @@ -114,6 +114,22 @@ Android版的SweetAlert,清新文艺,快意灵动的甜心弹框 .setCustomImage(R.drawable.custom_img) .show(); +A edit text dialog: + + new SweetAlertDialog(this, SweetAlertDialog.EDIT_TEXT_TYPE) + .setTitleText("Edit text") + .setContentText("Please enter the text.") + .show(); + +A list text dialog: + + new SweetAlertDialog(this, SweetAlertDialog.LIST_TYPE) + .setTitleText("List dialog") + .setContentText("Please select list item.") + .setList(list) + .setListTextSize(20) + .show(); + 确认事件绑定: new SweetAlertDialog(this, SweetAlertDialog.WARNING_TYPE) @@ -128,6 +144,35 @@ Android版的SweetAlert,清新文艺,快意灵动的甜心弹框 }) .show(); +Bind the listener to edit text confirm button: + + new SweetAlertDialog(this, SweetAlertDialog.EDIT_TEXT_TYPE) + .setTitleText("Are you sure?") + .setTitleText("Edit text") + .setContentText("Please enter the text.") + .setEditTextConfirmClickListener(new SweetAlertDialog.OnEditTextSweetClickListener() { + @Override + public void onClick(SweetAlertDialog sDialog, String inputText) { + sDialog.dismissWithAnimation(); + } + }) + .show(); + +Bind the listener to list: + + new SweetAlertDialog(this, SweetAlertDialog.LIST_TYPE) + .setTitleText("List dialog") + .setContentText("Please select list item.") + .setList(list) + .setListTextSize(20) + .setListClickListener(new SweetAlertDialog.OnListSweetClickListener() { + @Override + public void onItemClick(SweetAlertDialog sDialog, int position, String listText) { + sDialog.dismissWithAnimation(); + } + }) + .show(); + 显示取消按钮及事件绑定: new SweetAlertDialog(this, SweetAlertDialog.WARNING_TYPE) diff --git a/build.gradle b/build.gradle index 88583e9d..9da56c60 100644 --- a/build.gradle +++ b/build.gradle @@ -3,13 +3,13 @@ buildscript { mavenCentral() } dependencies { - classpath 'com.android.tools.build:gradle:0.14.1' + classpath 'com.android.tools.build:gradle:2.3.1' } } ext { - compileSdkVersion = 21 - buildToolsVersion = "19.1.0" + compileSdkVersion = 25 + buildToolsVersion = "25.0.2" } allprojects { diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 8a6b0613..90de923d 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ -#Tue Dec 02 17:43:17 CET 2014 +#Tue May 02 15:42:42 JST 2017 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-2.2.1-all.zip \ No newline at end of file +distributionUrl=https\://services.gradle.org/distributions/gradle-3.3-all.zip diff --git a/library/build.gradle b/library/build.gradle index 8e4eabc6..a0dbc278 100644 --- a/library/build.gradle +++ b/library/build.gradle @@ -8,7 +8,7 @@ android { buildToolsVersion rootProject.ext.buildToolsVersion defaultConfig { - minSdkVersion 9 + minSdkVersion 21 } lintOptions { @@ -18,6 +18,8 @@ android { dependencies { compile 'com.pnikosis:materialish-progress:1.0' + compile 'com.android.support:recyclerview-v7:25.3.1' + compile 'com.android.support:cardview-v7:25.3.1' } apply from: 'https://raw.github.com/chrisbanes/gradle-mvn-push/master/gradle-mvn-push.gradle' \ No newline at end of file diff --git a/library/src/main/java/cn/pedant/SweetAlert/ListRecyclerAdapter.java b/library/src/main/java/cn/pedant/SweetAlert/ListRecyclerAdapter.java new file mode 100644 index 00000000..1f1706f4 --- /dev/null +++ b/library/src/main/java/cn/pedant/SweetAlert/ListRecyclerAdapter.java @@ -0,0 +1,61 @@ +package cn.pedant.SweetAlert; + +import android.content.Context; +import android.support.v7.widget.RecyclerView; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.TextView; + +import java.util.ArrayList; + +public class ListRecyclerAdapter extends RecyclerView.Adapter { + + private LayoutInflater mInflater; + private ArrayList mData; + private OnRecyclerListener mListener; + private int mTextSize; + + public ListRecyclerAdapter(Context context, ArrayList data, OnRecyclerListener listener, int textSize) { + mInflater = LayoutInflater.from(context); + mData = data; + mListener = listener; + mTextSize = textSize; + } + + public interface OnRecyclerListener { + void onItemClick(View v, int position); + } + + @Override + public ListRecyclerAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { + return new ViewHolder(mInflater.inflate(R.layout.list_item, parent, false)); + } + + @Override + public void onBindViewHolder(ViewHolder holder, final int position) { + holder.textView.setText(mData.get(position)); + holder.itemView.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + mListener.onItemClick(v, position); + } + }); + + } + + @Override + public int getItemCount() { + return mData.size(); + } + + class ViewHolder extends RecyclerView.ViewHolder { + TextView textView; + public ViewHolder(View v) { + super(v); + textView = (TextView) v.findViewById(R.id.list_item_text); + textView.setTextSize(mTextSize); + } + } + +} diff --git a/library/src/main/java/cn/pedant/SweetAlert/SweetAlertDialog.java b/library/src/main/java/cn/pedant/SweetAlert/SweetAlertDialog.java index 77be52dd..7f25043e 100644 --- a/library/src/main/java/cn/pedant/SweetAlert/SweetAlertDialog.java +++ b/library/src/main/java/cn/pedant/SweetAlert/SweetAlertDialog.java @@ -6,6 +6,9 @@ import android.graphics.drawable.Drawable; import android.os.Build; import android.os.Bundle; +import android.support.v7.widget.LinearLayoutManager; +import android.support.v7.widget.RecyclerView; +import android.util.Log; import android.view.View; import android.view.WindowManager; import android.view.animation.AlphaAnimation; @@ -13,15 +16,17 @@ import android.view.animation.AnimationSet; import android.view.animation.Transformation; import android.widget.Button; +import android.widget.EditText; import android.widget.FrameLayout; import android.widget.ImageView; import android.widget.TextView; import com.pnikosis.materialishprogress.ProgressWheel; +import java.util.ArrayList; import java.util.List; -public class SweetAlertDialog extends Dialog implements View.OnClickListener { +public class SweetAlertDialog extends Dialog implements View.OnClickListener, ListRecyclerAdapter.OnRecyclerListener { private View mDialogView; private AnimationSet mModalInAnim; private AnimationSet mModalOutAnim; @@ -55,6 +60,16 @@ public class SweetAlertDialog extends Dialog implements View.OnClickListener { private OnSweetClickListener mCancelClickListener; private OnSweetClickListener mConfirmClickListener; private boolean mCloseFromCancel; + private FrameLayout mEditTextFrame; + private EditText mEditText; + private Button mEditTextConfirmButton; + private OnEditTextSweetClickListener mEditTextConfirmClickListener; + private String mDefaultEditText = ""; + private FrameLayout mListViewFrame; + private ArrayList mList; + private RecyclerView mListView; + private OnListSweetClickListener mListClickListener; + private int mListTextSize = 15; public static final int NORMAL_TYPE = 0; public static final int ERROR_TYPE = 1; @@ -62,11 +77,21 @@ public class SweetAlertDialog extends Dialog implements View.OnClickListener { public static final int WARNING_TYPE = 3; public static final int CUSTOM_IMAGE_TYPE = 4; public static final int PROGRESS_TYPE = 5; + public static final int EDIT_TEXT_TYPE = 6; + public static final int LIST_TYPE = 7; public static interface OnSweetClickListener { public void onClick (SweetAlertDialog sweetAlertDialog); } + public static interface OnEditTextSweetClickListener { + public void onClick (SweetAlertDialog sweetAlertDialog, String inputText); + } + + public static interface OnListSweetClickListener { + public void onItemClick (SweetAlertDialog sweetAlertDialog, int position, String listText); + } + public SweetAlertDialog(Context context) { this(context, NORMAL_TYPE); } @@ -156,11 +181,17 @@ protected void onCreate(Bundle savedInstanceState) { mProgressHelper.setProgressWheel((ProgressWheel)findViewById(R.id.progressWheel)); mConfirmButton.setOnClickListener(this); mCancelButton.setOnClickListener(this); - + mEditTextFrame = (FrameLayout)findViewById(R.id.edit_text_frame); + mEditText = (EditText)findViewById(R.id.editText); + mEditTextConfirmButton = (Button)findViewById(R.id.edit_text_confirm_button); + mEditTextConfirmButton.setOnClickListener(this); + mListViewFrame = (FrameLayout)findViewById(R.id.list_view_frame); + mListView = (RecyclerView)findViewById(R.id.listView); setTitleText(mTitleText); setContentText(mContentText); setCancelText(mCancelText); setConfirmText(mConfirmText); + setList(mList); changeAlertType(mAlertType, true); } @@ -172,6 +203,9 @@ private void restore () { mWarningFrame.setVisibility(View.GONE); mProgressFrame.setVisibility(View.GONE); mConfirmButton.setVisibility(View.VISIBLE); + mEditTextFrame.setVisibility(View.GONE); + mEditTextConfirmButton.setVisibility(View.GONE); + mListViewFrame.setVisibility(View.GONE); mConfirmButton.setBackgroundResource(R.drawable.blue_button_background); mErrorFrame.clearAnimation(); @@ -220,6 +254,20 @@ private void changeAlertType(int alertType, boolean fromCreate) { mProgressFrame.setVisibility(View.VISIBLE); mConfirmButton.setVisibility(View.GONE); break; + case EDIT_TEXT_TYPE: + mEditTextFrame.setVisibility(View.VISIBLE); + mConfirmButton.setVisibility(View.GONE); + mEditTextConfirmButton.setVisibility(View.VISIBLE); + mEditText.setText(mDefaultEditText); + mEditText.requestFocus(); + getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE); + break; + case LIST_TYPE: + mWarningFrame.setVisibility(View.VISIBLE); + mTitleTextView.setVisibility(View.GONE); + mListViewFrame.setVisibility(View.VISIBLE); + mConfirmButton.setVisibility(View.GONE); + break; } if (!fromCreate) { playAnimation(); @@ -320,6 +368,28 @@ public SweetAlertDialog setConfirmText (String text) { if (mConfirmButton != null && mConfirmText != null) { mConfirmButton.setText(mConfirmText); } + if (mEditTextConfirmButton != null && mConfirmText != null) { + mEditTextConfirmButton.setText(mConfirmText); + } + return this; + } + + public SweetAlertDialog setList (final ArrayList list) { + mList = list; + if (mListView != null && mList != null) { + mListView.setLayoutManager(new LinearLayoutManager(getContext())); + mListView.setAdapter(new ListRecyclerAdapter(getContext(), mList, this, mListTextSize)); + } + return this; + } + + public SweetAlertDialog setListTextSize (final int listTextSize) { + mListTextSize = listTextSize; + return this; + } + + public SweetAlertDialog setDefaultEditText (String defaultText) { + mDefaultEditText = defaultText; return this; } @@ -333,6 +403,16 @@ public SweetAlertDialog setConfirmClickListener (OnSweetClickListener listener) return this; } + public SweetAlertDialog setEditTextConfirmClickListener (OnEditTextSweetClickListener listener) { + mEditTextConfirmClickListener = listener; + return this; + } + + public SweetAlertDialog setListClickListener (OnListSweetClickListener listener) { + mListClickListener = listener; + return this; + } + protected void onStart() { mDialogView.startAnimation(mModalInAnim); playAnimation(); @@ -356,6 +436,7 @@ public void dismissWithAnimation() { private void dismissWithAnimation(boolean fromCancel) { mCloseFromCancel = fromCancel; mConfirmButton.startAnimation(mOverlayOutAnim); + mEditTextConfirmButton.startAnimation(mOverlayOutAnim); mDialogView.startAnimation(mModalOutAnim); } @@ -373,6 +454,21 @@ public void onClick(View v) { } else { dismissWithAnimation(); } + } else if (v.getId() == R.id.edit_text_confirm_button) { + if (mEditTextConfirmClickListener != null) { + mEditTextConfirmClickListener.onClick(SweetAlertDialog.this, mEditText.getText().toString()); + } else { + dismissWithAnimation(); + } + } + } + + @Override + public void onItemClick(View v, int position) { + if (mListClickListener != null) { + mListClickListener.onItemClick(SweetAlertDialog.this, position, mList.get(position)); + } else { + dismissWithAnimation(); } } diff --git a/library/src/main/res/drawable-v21/card_foreground.xml b/library/src/main/res/drawable-v21/card_foreground.xml new file mode 100644 index 00000000..70a4240c --- /dev/null +++ b/library/src/main/res/drawable-v21/card_foreground.xml @@ -0,0 +1,3 @@ + + \ No newline at end of file diff --git a/library/src/main/res/drawable-v21/card_foreground_selector.xml b/library/src/main/res/drawable-v21/card_foreground_selector.xml new file mode 100644 index 00000000..a4399eaf --- /dev/null +++ b/library/src/main/res/drawable-v21/card_foreground_selector.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/library/src/main/res/drawable/card_foreground.xml b/library/src/main/res/drawable/card_foreground.xml new file mode 100644 index 00000000..e068235a --- /dev/null +++ b/library/src/main/res/drawable/card_foreground.xml @@ -0,0 +1,8 @@ + + \ No newline at end of file diff --git a/library/src/main/res/drawable/card_foreground_selector.xml b/library/src/main/res/drawable/card_foreground_selector.xml new file mode 100644 index 00000000..baaecd57 --- /dev/null +++ b/library/src/main/res/drawable/card_foreground_selector.xml @@ -0,0 +1,15 @@ + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/library/src/main/res/layout/alert_dialog.xml b/library/src/main/res/layout/alert_dialog.xml index 7972157f..cfc8415f 100644 --- a/library/src/main/res/layout/alert_dialog.xml +++ b/library/src/main/res/layout/alert_dialog.xml @@ -138,6 +138,33 @@ android:textColor="#797979" android:visibility="gone" /> + + + + + + + + + + +