前面实现了自定义Toast,今天来实现自定义Dialog的展示,仿照QQ删除提示对话框做的,但是美观上还需要具体调整,先来明白实现的方法。
效果图:
思路:1.继承Dialog类,重写其onCreat()方法
2.设置点击确定按钮的监听事件
3.动态控制dialog显示的大小
dialog的布局文件,其中用到drawable的shap来实现圆角
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/bg_custom_dialog">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/dialog_title"
android:gravity="center_horizontal"
android:textColor="#000000"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/dialog_content"
android:layout_marginTop="@dimen/dialog_margin_top"
android:layout_marginLeft="@dimen/dialog_margin_left"
android:textColor="#000000"/>
<TextView
android:layout_width="match_parent"
android:layout_height="2dip"
android:background="#000000"
android:layout_marginTop="@dimen/dialog_margin_top"
android:textColor="#000000"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tv_enter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:text="@string/dialog_enter"
android:gravity="center"
android:layout_marginTop="@dimen/dialog_margin_top"
android:textColor="#000000"/>
<TextView
android:layout_width="1dip"
android:layout_height="match_parent"
android:background="#000000"/>
<TextView
android:id="@+id/tv_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:text="@string/dialog_cancel"
android:layout_marginTop="@dimen/dialog_margin_top"
android:gravity="center"
android:textColor="#000000"/>
</LinearLayout>
</LinearLayout>
自定义dialog类:
public class MyDialog extends Dialog implements View.OnClickListener {
private Context mContext;
public MyDialog(Context context, int theme) {
super(context, theme);
mContext=context;
}
<span style="color:#ff0000;"> /*点击确定按钮的监听器*/</span>
public interface MySubmmitListener{
void submmit();
}
<span style="color:#ff0000;"> /*点击确定按钮的监听器*/</span>
private MySubmmitListener mySubmmitListener;
<span style="color:#ff0000;"> /*设置点击确定按钮时的监听事件*/</span>
public void setMySubmmitListener(MySubmmitListener mSubmmitListener){
this.mySubmmitListener=mSubmmitListener;
}
private TextView submmitTextView;
private TextView cancelTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.custom_my_dialog);
submmitTextView= (TextView) findViewById(R.id.tv_enter);
cancelTextView= (TextView) findViewById(R.id.tv_cancel);
submmitTextView.setOnClickListener(this);
cancelTextView.setOnClickListener(this);
<span style="color:#ff0000;"> /*控制dialog的大小*/</span>
<span style="color:#ff0000;"> WindowManager.LayoutParams lp=getWindow().getAttributes();
WindowManager wm= (WindowManager) mContext.getSystemService(mContext.WINDOW_SERVICE);
int width=wm.getDefaultDisplay().getWidth();
lp.width=width-100;
getWindow().setAttributes(lp);</span>
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.tv_enter:
<span style="color:#ff0000;">if(mySubmmitListener!=null){
mySubmmitListener.submmit();
}</span>
dismiss();
break;
case R.id.tv_cancel:
dismiss();
break;
}
}
}
MainActivity中调用,在submmit()方法中执行我们所需要的操作即可
showBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
MyDialog dialog=new MyDialog(MainActivity.this,R.style.dialog);
dialog.show();
dialog.setMySubmmitListener(new MyDialog.MySubmmitListener() {
@Override
public void submmit() {
MyToast.makeText(MainActivity.this,R.string.dialog_toast,Toast.LENGTH_SHORT).show();
}
});
}
});日后项目中再来实现具体需要的dialog
dialog常用的style配置
<!--dialog-->
<style name="MyProgressDialog" parent="Base.Theme.AppCompat.Dialog">
<item name="android:windowFrame">@null</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:backgroundDimEnabled">true</item>
</style>
638

被折叠的 条评论
为什么被折叠?



