一开始,我都用的Android自带的弹出框,自带弹出框的好处是简单易用,但也有欠缺。一是虽然和操作系统风格相符,但可能与你的APP的个性化风格不符;二是如果要在弹出框里加Button,textEdit等就捉襟见肘了。
于是,绕不开的就是自定义弹出框了,干脆,就做好看点吧。
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"
android:orientation="vertical"
android:padding="20.0dip" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@drawable/bg_bombbox"
android:orientation="vertical" >
<TextView
android:id="@+id/title"
style="@style/text_18_ffffff"
android:layout_width="fill_parent"
android:layout_height="40.0dip"
android:gravity="center"
android:text="购买数量"
android:visibility="visible" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:orientation="horizontal" >
<Button
android:id="@+id/decrease_bt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="-"
android:textSize="24sp"
android:textStyle="bold"
android:paddingLeft="15dp"
android:paddingRight="15dp"/>
<EditText
android:id="@+id/amount_et"
android:inputType="number"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:lineSpacingMultiplier="1.0"
android:minHeight="30.0dip"/>
<Button
android:id="@+id/increase_bt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="+"
android:textSize="24sp"
android:textStyle="bold"
android:paddingLeft="15dp"
android:paddingRight="15dp"/>
LinearLayout>
<View
android:layout_width="fill_parent"
android:layout_height="1.0px"
android:background="#ffd0d0d0" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="50.0dip"
android:layout_gravity="bottom"
android:background="@drawable/dialog_bottom_bg"
android:gravity="center"
android:orientation="horizontal" >
<Button
android:id="@+id/positiveButton"
style="@style/text_15_ffffff_sdw"
android:layout_width="114.0dip"
android:layout_height="40.0dip"
android:background="@drawable/btn_ok_selector"
android:gravity="center"
android:text="@string/ok" />
<Button
android:id="@+id/negativeButton"
style="@style/text_15_666666_sdw"
android:layout_width="114.0dip"
android:layout_height="40.0dip"
android:layout_marginLeft="20.0dip"
android:background="@drawable/btn_cancel_selector"
android:gravity="center"
android:text="@string/cancel" />
LinearLayout>
LinearLayout>
FrameLayout>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@drawable/btn_ok_pressed" />
<item android:drawable="@drawable/btn_ok_normal" />
selector>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="true" android:state_pressed="true" android:drawable="@drawable/btn_cancel_pressed" />
<item android:state_enabled="true" android:drawable="@drawable/btn_cancel" />
selector>
public class PutIntoCartDialog extends Dialog {
public PutIntoCartDialog(Context context) {
super(context);
}
public PutIntoCartDialog(Context context, int theme) {
super(context, theme);
}
public static class Builder {
private Context context;
private int goodsID;
Button decreaseButton;
Button increaseButton;
EditText amountEditText;
private DialogInterface.OnClickListener positiveButtonClickListener;
public Builder(Context context, int id) {
this.context = context;
goodsID=id;
}
// 获取数量
public int getAmount() {
if(amountEditText.getText()==null || amountEditText.length()==0 || amountEditText.getText().toString().equals("0"))
return 0;
else
return Integer.valueOf(amountEditText.getText().toString());
}
/**
* Set the positive button's listener
*
* @param listener
* @return
*/
public Builder setPositiveButtonListener(DialogInterface.OnClickListener listener) {
this.positiveButtonClickListener = listener;
return this;
}
public PutIntoCartDialog create() {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// instantiate the dialog with the custom Theme
final PutIntoCartDialog dialog = new PutIntoCartDialog(context,R.style.Dialog);
View layout = inflater.inflate(R.layout.dialog_put_into_cart_layout, null);
dialog.addContentView(layout, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
// set the spinner
decreaseButton = (Button) layout.findViewById(R.id.decrease_bt);
increaseButton = (Button) layout.findViewById(R.id.increase_bt);
amountEditText = (EditText) layout.findViewById(R.id.amount_et);
int purchaseCount = FusionField.FindGoodsByID(goodsID).getPurchaseCount();
if(purchaseCount>0)
amountEditText.setText(String.valueOf(purchaseCount));
else
amountEditText.setText("1");
// + and - button
decreaseButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
int amount = Integer.valueOf(amountEditText.getText().toString());
if(amount>0)
amountEditText.setText(String.valueOf(amount-1));
}
});
increaseButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
int amount = Integer.valueOf(amountEditText.getText().toString());
amountEditText.setText(String.valueOf(amount+1));
}
});
// set the confirm button
if (positiveButtonClickListener != null) {
((Button) layout.findViewById(R.id.positiveButton)).setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
positiveButtonClickListener.onClick(dialog, DialogInterface.BUTTON_POSITIVE);
}
});
}
// Cancel button
((Button) layout.findViewById(R.id.negativeButton)).setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.setContentView(layout);
return dialog;
}
}
}