自定义的Dialog

来源:互联网 发布:adb打开软件 编辑:程序博客网 时间:2024/06/04 18:30

相信很多人都想做自定义的控件,因为原生控件真的太丑了,今天给大家介绍的是一款自定义Dialog,之前我总觉得自定义Dialog要重写什么,继承什么东西一大堆,其实自定义Dialog是蛮简单的。那就进入正题。

传统的dialog的写法


Dialog alertDialog = new AlertDialog.Builder(this).
setTitle("对话框的标题").
setMessage("对话框的内容").
setIcon(R.drawable.ic_launcher).
create();
alertDialog.show();

自定义步骤

一,新建一个xml文件,在xml文件中布局你需要的dialog的样式,这里你写的样式就是你后面显示额样式

<?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="match_parent">
<TextView
style="@style/TitleStyle"
android:background="#f00"
android:text="设置密码"/>
<EditText
android:id="@+id/ed_set_psd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="设置密码"/>
<EditText
android:id="@+id/ed_confirm_psd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="设置密码"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/bt_submit"
android:text="确认"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<Button
android:id="@+id/bt_cancel"
android:text="取消"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>

一,使用这个布局,利用setView()这个方法添加一个界面
“`
AlertDialog.Builder bulider = new AlertDialog.Builder(this);
final AlertDialog dialog = bulider.create();
final View dialogview = View.inflate(this,R.layout.dialog_set_psd,null);
dialog.setView(dialogview);

//后面你就可以对自定义Dialog里面的item控件进行操作
bt_submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//主要的功能区域
EditText ed_confirm_psd = (EditText) dialogview.findViewById(R.id.ed_confirm_psd);
EditText ed_set_psd = (EditText) dialogview.findViewById(R.id.ed_set_psd);
String psd = ed_set_psd.getText().toString().trim();
String confirm = ed_confirm_psd.getText().toString().trim();
if (!TextUtils.isEmpty(psd)&&!TextUtils.isEmpty(confirm)){
if (psd.equals(confirm)){
Intent intent = new Intent(HomeActivity.this,TestActivity.class);
startActivity(intent);
}else{
ToastUtil.show(getApplicationContext(),”两次输入的密码不一致”);
}
}else{
ToastUtil.show(getApplicationContext(),”输入不能为空”);
}
}
});
“`

其实这种自定的dialog还是蛮简单的,读者不必采用继承重写等这类方法去自定义 我们在xml文件中写出我们想要的样式即可,功能还是蛮强大.

0 0
原创粉丝点击