忙了一周了,也该抽点时间写点儿东西.
话不多说,书接上文(上文在哪儿?),开发项目中经常用到弹窗,比如,用户要退出app,咱们就给他一个弹窗(实际上用户不想看到~~~MDZZ),
代码如下
mDialog=new CustomDialog(this, "温馨提示", "确定要退出我们可爱的App吗?", "确定",new View.OnClickListener() {
@Override
public void onClick(View v) {
mDialog.dismiss();
Toast.makeText(mContext,"退出了--伤心",Toast.LENGTH_LONG).show();
}
},"取消");
话不多说,开始我们的表演(社会我鸡哥,人很话不多)
1,你需要给我们的弹窗设置个样式
多BB几句,多跟你的设计妹子交流交流,别一会儿这个样式,一会儿那个样式的,确定整个app的弹窗基调,这样咱们写这个才有意义,用户体验也更好一些.
<style name="Dialog" parent="android:style/Theme.Dialog">
<item name="android:background">#00000000</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">true</item>
</style>
这段代码应该在哪儿都应该知道吧.......,具体意思大家自己了解一下
2,你需要一个自定义弹窗的布局
custom_dialog_layout
<?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="match_parent"
android:gravity="center"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="@drawable/dialog_back"
android:gravity="center_horizontal"
android:orientation="vertical">
<TextView
android:id="@+id/dialog_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginLeft="40dp"
android:layout_marginRight="40dp"
android:layout_marginTop="20dp"
android:gravity="center"
android:text="这个是标题"
android:textColor="#333333"
android:textSize="16sp"/>
<TextView
android:id="@+id/dialog_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp"
android:layout_marginRight="40dp"
android:gravity="center"
android:text="这个是标题"
android:textColor="#333333"
android:textSize="15sp"/>
<View
android:layout_width="match_parent"
android:layout_height="1px"
android:layout_marginTop="20dp"
android:background="@color/gray_line_color"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:id="@+id/dialog_confirm"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/back_text_selector_left"
android:gravity="center"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:text="确定"
android:textColor="#08A8FF"
android:textSize="16sp"/>
<View
android:id="@+id/dialog_line"
android:layout_width="1px"
android:layout_height="match_parent"
android:background="@color/gray_line_color"/>
<TextView
android:id="@+id/dialog_cancel"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/back_text_selector_right"
android:gravity="center"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:text="取消"
android:textColor="#08A8FF"
android:textSize="16sp"/>
</LinearLayout>
</LinearLayout>
<!--08A8FF-->
</LinearLayout>
dialog_back
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="10dp"/>
<stroke
android:width="1px"
android:color="@color/white"/>
<solid android:color="@color/white"/>
</shape>
back_text_selector_left
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape
android:shape="rectangle"
>
<corners android:bottomLeftRadius="10dp" />
<solid android:color="@color/fadegray"/><!--#AFAFAF-->
</shape>
</item>
<item android:state_pressed="false">
<shape
android:shape="rectangle"
>
<corners android:bottomLeftRadius="10dp" />
<solid android:color="@color/white"/>
</shape>
</item>
</selector>
其他的背景自己写吧 .............
3,简单的核心逻辑
/**
* Created by wangfei
*/
public class CustomDialog extends Dialog {
private String title;
private String content;
private String buttonConfirm;
private String buttonCancel;
private View.OnClickListener confirmClickListener;
private View.OnClickListener cancelClickListener;
private static final int SHOW_ONE = 1;
private int show = 2;
public CustomDialog(Context context, String title, String content,
String buttonConfirm,
View.OnClickListener confirmClickListener) {
super(context, R.style.Dialog);
this.title = title;
this.content = content;
this.buttonConfirm = buttonConfirm;
this.confirmClickListener = confirmClickListener;
this.show = SHOW_ONE;
}
public CustomDialog(Context context, String title, String content,
String buttonConfirm,
View.OnClickListener confirmClickListener,String buttonCancel) {
super(context, R.style.Dialog);
this.title = title;
this.content = content;
this.buttonConfirm = buttonConfirm;
this.buttonCancel = buttonCancel;
this.confirmClickListener = confirmClickListener;
}
public CustomDialog(Context context, String title, String content,
View.OnClickListener confirmClickListener, String buttonConfirm, String buttonCancel) {
super(context, R.style.Dialog);
this.title = title;
this.content = content;
this.buttonConfirm = buttonConfirm;
this.buttonCancel = buttonCancel;
this.confirmClickListener = confirmClickListener;
}
public CustomDialog(Context context, String title, String content,
View.OnClickListener confirmClickListener, View.OnClickListener cancelClickListener, String
buttonConfirm, String buttonCancel) {
super(context, R.style.Dialog);
this.title = title;
this.content = content;
this.buttonConfirm = buttonConfirm;
this.buttonCancel = buttonCancel;
this.confirmClickListener = confirmClickListener;
this.cancelClickListener = cancelClickListener;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.custom_dialog_layout);
TextView dialog_title = (TextView) findViewById(R.id.dialog_title);
TextView dialog_content = (TextView) findViewById(R.id.dialog_content);
TextView dialog_confirm = (TextView) findViewById(R.id.dialog_confirm);
TextView dialog_cancel = (TextView) findViewById(R.id.dialog_cancel);
View dialog_line = findViewById(R.id.dialog_line);
if (!TextUtils.isEmpty(title))
dialog_title.setText(title);
if (!TextUtils.isEmpty(content)) {
dialog_content.setText(content);
} else {
dialog_content.setVisibility(View.GONE);
}
if (!TextUtils.isEmpty(buttonConfirm))
dialog_confirm.setText(buttonConfirm);
if (!TextUtils.isEmpty(buttonCancel))
dialog_cancel.setText(buttonCancel);
if (SHOW_ONE == show) {
dialog_line.setVisibility(View.GONE);
dialog_cancel.setVisibility(View.GONE);
if (null != confirmClickListener) {
dialog_confirm.setOnClickListener(confirmClickListener);
}
dialog_confirm.setBackgroundResource(R.drawable.back_text_selector);
} else {
if (null != confirmClickListener) {
dialog_confirm.setOnClickListener(confirmClickListener);
}
if (null != cancelClickListener) {
dialog_cancel.setOnClickListener(cancelClickListener);
} else {
dialog_cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CustomDialog.this.dismiss();
}
});
}
}
}
public void setCanotBackPress() {
this.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) {
return true;
}
return false;
}
});
}
}
4,使用方法
mDialog=new CustomDialog(this, "温馨提示", "确定要退出我们可爱的App吗?", "确定",new View.OnClickListener() {
@Override
public void onClick(View v) {
mDialog.dismiss();
Toast.makeText(LoginActivity.this,"退出了--伤心",Toast.LENGTH_LONG).show();
}
});
mDialog.setCanotBackPress();
mDialog.setCanceledOnTouchOutside(false);
mDialog.show();
这TMD不跟上面的一样吗?NONONO,too young too simple sometimes native
效果如下
setCanotBackPress()
设置屏蔽返回键
简而言之就是:你想实现啥样式就自己定义一个构造方法吧,根据自己的需求,比如设置字体的颜色之类的,自己处理一下逻辑
好了就到这儿吧,早上看了会儿TS6 ,LGD大优竟然被秘密翻盘了,真是哔了狗