忙了一周了,也該抽點(diǎn)時(shí)間寫(xiě)點(diǎn)兒東西.
話不多說(shuō),書(shū)接上文(上文在哪兒?),開(kāi)發(fā)項(xiàng)目中經(jīng)常用到彈窗,比如,用戶要退出app,咱們就給他一個(gè)彈窗(實(shí)際上用戶不想看到~~~MDZZ),
代碼如下
mDialog=new CustomDialog(this, "溫馨提示", "確定要退出我們可愛(ài)的App嗎?", "確定",new View.OnClickListener() {
@Override
public void onClick(View v) {
mDialog.dismiss();
Toast.makeText(mContext,"退出了--傷心",Toast.LENGTH_LONG).show();
}
},"取消");
話不多說(shuō),開(kāi)始我們的表演(社會(huì)我雞哥,人很話不多)
1,你需要給我們的彈窗設(shè)置個(gè)樣式
多BB幾句,多跟你的設(shè)計(jì)妹子交流交流,別一會(huì)兒這個(gè)樣式,一會(huì)兒那個(gè)樣式的,確定整個(gè)app的彈窗基調(diào),這樣咱們寫(xiě)這個(gè)才有意義,用戶體驗(yàn)也更好一些.
<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>
這段代碼應(yīng)該在哪兒都應(yīng)該知道吧.......,具體意思大家自己了解一下
2,你需要一個(gè)自定義彈窗的布局
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="這個(gè)是標(biāo)題"
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="這個(gè)是標(biāo)題"
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>
其他的背景自己寫(xiě)吧 .............
3,簡(jiǎn)單的核心邏輯
/**
* 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, "溫馨提示", "確定要退出我們可愛(ài)的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()
設(shè)置屏蔽返回鍵
簡(jiǎn)而言之就是:你想實(shí)現(xiàn)啥樣式就自己定義一個(gè)構(gòu)造方法吧,根據(jù)自己的需求,比如設(shè)置字體的顏色之類(lèi)的,自己處理一下邏輯
好了就到這兒吧,早上看了會(huì)兒TS6 ,LGD大優(yōu)竟然被秘密翻盤(pán)了,真是嗶了狗