您的位置:首页 > 其它

实现一个简单的自定义Dialog弹窗。

2016-12-29 15:20 274 查看
自己看了一些资料之后,这个是自己算是第一个比较有点用的自定义控件了吧。
谢了这个之后,对接口回调的理解也有一定的帮助。

不说了,贴代码。比较懒,偷懒了很多。

//这里注意,oncreate方法在dialog.show()之后才会运行,所以要动态设置一些属性的话,必须放在oncreate中,不然会报空。

public class MyDialog extends Dialog {
private TextView msg;
private String mContent;
private TextView mYes;
private TextView mNo;
//回调
private MyOkCallBack myOkCallBack;
private MyNoCallBack myNoCallBack;

//回调
public interface MyOkCallBack {
void doSomeOkThing(View view);
}

public interface MyNoCallBack {
void doSomeNoThing(View view);

}

public MyDialog(Context context) {
//调用父类构造,使用style。
super(context, R.style.mydialog);
}

public MyDialog(Context context, int themeResId) {
super(context, themeResId);
}

protected MyDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
super(context, cancelable, cancelListener);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//设置自定义dialog布局。
setContentView(R.layout.my_custom_dialog_layout);
initView();
// 外面穿进来的参数必须在这里设置,因为dialog.show()之后才会调用Oncreate方法。
initData();
initEvent();

}

private void initData() {
if (mContent != null)
this.msg.setText(mContent);
}

private void initEvent() {
mYes.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
myOkCallBack.doSomeOkThing(v);
}
});

mNo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
myNoCallBack.doSomeNoThing(v);
}
});

}

public void setMsg(String msg) {
this.mContent = msg;
}

private void initView() {
mYes = (TextView) findViewById(R.id.tv_yes);
mNo = (TextView) findViewById(R.id.tv_no);
msg = (TextView) findViewById(R.id.msg);

}

public void setYesListener(MyOkCallBack ok) {
myOkCallBack = ok;
}

public void setNoListener(MyNoCallBack no) {
myNoCallBack = no;
}

}


自定义样式

<style name="mydialog" parent="android:style/Theme.Dialog">
<!--背景透明-->
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowNoTitle">true</item>
<!--没有边框-->
<item name="android:windowFrame">@null</item>
<!--窗口浮动-->
<item name="android:windowIsFloating">true</item>
<!--背景变暗-->
<item name="android:backgroundDimEnabled">true</item>

</style>


自定义弹窗布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
android:layout_centerInParent="true"
android:layout_width="300dp"
android:layout_height="200dp"
android:orientation="vertical"
android:background="@drawable/shape_bg"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="自定义Dialog"
/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#000000"/>

<TextView
android:id="@+id/msg"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center"
/>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
>
<TextView
android:id="@+id/tv_yes"
android:layout_width="0dp"
android:layout_weight="0.5"
android:layout_height="wrap_content"
android:text="确定"
android:gravity="center"
/>
<TextView
android:id="@+id/tv_no"
android:layout_width="0dp"
android:layout_weight="0.5"
android:layout_height="wrap_content"
android:gravity="center"
android:text="取消"
/>

</LinearLayout>

</LinearLayout>

</RelativeLayout>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐