UpdateNameDialog.java
1 package com.example.lileme.ThreeFragment.MyselfDetile; 2 3 import android.app.Dialog; 4 import android.content.Context; 5 import android.os.Bundle; 6 import android.support.annotation.NonNull; 7 import android.text.TextUtils; 8 import android.view.Display; 9 import android.view.View; 10 import android.view.WindowManager; 11 import android.widget.EditText; 12 import android.widget.TextView; 13 14 import com.example.lileme.R; 15 16 public class UpdateNameDialog extends Dialog implements View.OnClickListener{ 17 private TextView mTvtitle,mTvconfirm,mTvcancle;//标题和确定取消 18 private EditText mEtname;//输入框 19 private String title,confirm,cancle,name; 20 private IOnCancleListener cancleListener; 21 private IOnConfirmListener confirmListener; 22 23 public String getName() { 24 name=mEtname.getText().toString(); //获取文本框内容 25 return name; 26 } 27 28 public UpdateNameDialog setTitle(String title) { 29 this.title = title; 30 return this; 31 } 32 33 public UpdateNameDialog setConfirm(String confirm,IOnConfirmListener confirmListener) { 34 this.confirm = confirm; 35 this.confirmListener = confirmListener; 36 return this; 37 } 38 39 public UpdateNameDialog setCancle(String cancle,IOnCancleListener cancleListener) { 40 this.cancle = cancle; 41 this.cancleListener = cancleListener; 42 return this; 43 } 44 45 public UpdateNameDialog(@NonNull Context context) { 46 super(context); 47 } 48 49 @Override 50 protected void onCreate(Bundle savedInstanceState) { 51 super.onCreate(savedInstanceState); 52 setContentView(R.layout.layout_update_name_dialog); 53 mTvtitle = findViewById(R.id.tv_title); 54 mEtname = findViewById(R.id.et_name); 55 mTvconfirm = findViewById(R.id.tv_confirm); 56 mTvcancle = findViewById(R.id.tv_cancle); 57 58 59 WindowManager windowManager = getWindow().getWindowManager(); 60 Display display = windowManager.getDefaultDisplay(); 61 WindowManager.LayoutParams lp = this.getWindow().getAttributes(); 62 lp.width = (int)(display.getWidth()); //设置宽度 63 this.getWindow().setAttributes(lp); 64 65 if(!TextUtils.isEmpty(title)){ //TextUtils工具类判断是否为空 66 mTvtitle.setText(title); 67 } 68 if(!TextUtils.isEmpty(cancle)){ //TextUtils工具类判断是否为空 69 mTvcancle.setText(cancle); 70 } 71 if(!TextUtils.isEmpty(confirm)){ //TextUtils工具类判断是否为空 72 mTvconfirm.setText(confirm); 73 } 74 mTvcancle.setOnClickListener(this); 75 mTvconfirm.setOnClickListener(this); 76 77 } 78 79 80 81 82 @Override 83 public void onClick(View v) { 84 switch (v.getId()){ 85 case R.id.tv_cancle: 86 if(cancleListener!=null){ 87 cancleListener.OnCancle(this); 88 } 89 //dismiss(); 90 break; 91 case R.id.tv_confirm: 92 if(confirmListener!=null){ 93 confirmListener.OnConfirm(this); 94 } 95 //dismiss(); 96 break; 97 } 98 } 99 100 //定义接口在别的类中调用 101 public interface IOnCancleListener{ 102 void OnCancle(UpdateNameDialog dialog); 103 } 104 public interface IOnConfirmListener{ 105 void OnConfirm(UpdateNameDialog dialog); 106 } 107 108 }
layout_update_name_dialog.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" android:layout_width="match_parent" 4 android:layout_height="wrap_content"> 5 <TextView 6 android:id="@+id/tv_title" 7 android:layout_width="wrap_content" 8 android:layout_height="40dp" 9 android:text="标题" 10 android:textSize="20sp" 11 android:textColor="#000" 12 android:layout_marginTop="10dp" 13 android:layout_marginLeft="20dp" 14 android:gravity="center_vertical"/> 15 <EditText 16 android:id="@+id/et_name" 17 android:layout_width="match_parent" 18 android:layout_height="wrap_content" 19 android:inputType="text" 20 android:text="" 21 android:textSize="20sp" 22 android:textColor="#000" 23 android:maxLength="4" 24 android:padding="10dp" 25 android:background="@drawable/bg_myinformation_inputname" 26 android:layout_marginLeft="10dp" 27 android:layout_marginRight="10dp"/> 28 <LinearLayout 29 android:layout_width="match_parent" 30 android:layout_height="wrap_content" 31 android:orientation="horizontal" 32 android:gravity="right" 33 android:layout_marginTop="10dp"> 34 <TextView 35 android:id="@+id/tv_cancle" 36 android:layout_width="wrap_content" 37 android:layout_height="wrap_content" 38 android:text="取消" 39 android:textSize="20sp" 40 android:textColor="#3809F7" 41 android:padding="10dp"/> 42 <TextView 43 android:id="@+id/tv_confirm" 44 android:layout_width="wrap_content" 45 android:layout_height="wrap_content" 46 android:text="确定" 47 android:textSize="20sp" 48 android:textColor="#000" 49 android:layout_marginLeft="20dp" 50 android:layout_marginRight="10dp" 51 android:padding="10dp" 52 android:paddingBottom="20dp"/> 53 </LinearLayout> 54 55 </LinearLayout>