android自定义 dialog

来源:互联网 发布:超级软件txt下载 编辑:程序博客网 时间:2024/05/18 12:40

这里以customer_dialog为例子

/** * 自定义dialog样式 *  * @author cyp * */public class CustomerDialog {Context context;Dialog dialog;Button sure, cancle;TextView phone_error, phone_sure, notification3;LinearLayout customer_dialog_warn_linear, customer_dialog_phoneofmessage;MyHandler handler;/** * init the dialog *  * @return */public CustomerDialog(Context con,final MyHandler handler) {this.context = con;this.handler = handler;dialog = new Dialog(context, R.style.PersonalInfomationDialog);dialog.setContentView(R.layout.custom_dialog_layout);phone_error = (TextView) dialog.findViewById(R.id.phone_error);phone_sure = (TextView) dialog.findViewById(R.id.phone_sure);notification3 = (TextView) dialog.findViewById(R.id.notification3);sure = (Button) dialog.findViewById(R.id.sure);cancle = (Button) dialog.findViewById(R.id.cancle);DensityUtil.setViewWHForW(context, sure, 150,90);DensityUtil.setViewWHForW(context, cancle, 150,90);customer_dialog_warn_linear = (LinearLayout) dialog.findViewById(R.id.warn_linear);customer_dialog_phoneofmessage = (LinearLayout) dialog.findViewById(R.id.phoneofmessage);sure.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {Intent intent = new Intent();//intent.setAction("show_userinfo_verification_code");//// 发出广播 打开验证码页面//context.sendBroadcast(intent); Message msg = new Message(); msg.what = 4; handler.sendMessage(msg);//////Intent intent = new Intent(context,PersonalInfomationActivity.class);////context.startActivity(intent);}});cancle.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {dismiss();}});}/** * @category Set The Content of the Button * */public void setButtonText(String buttontext) {cancle.setText(buttontext);}public void show() {dialog.show();}public void hide() {dialog.hide();}/** * 验证手机 */public void show_phone_sure() {phone_sure.setVisibility(View.VISIBLE);}public void hide_phone_sure() {phone_sure.setVisibility(View.GONE);}/** * 错误 */public void show_phone_error() {phone_error.setVisibility(View.VISIBLE);}public void hide_phone_error() {phone_error.setVisibility(View.GONE);}/** * 手机错误提示层 */public void show_warn_linear() {customer_dialog_warn_linear.setVisibility(View.VISIBLE);}public void hide_warn_linear() {customer_dialog_warn_linear.setVisibility(View.GONE);}/** * 按钮显示隐藏 */public void show_button_ok() {sure.setVisibility(View.VISIBLE);}public void hide_button_ok() {sure.setVisibility(View.GONE);}/** * 短信提示发送层 */public void show_phoneofmessage(String str) {customer_dialog_phoneofmessage.setVisibility(View.VISIBLE);notification3.setText(str);}public void hide_phoneofmessage() {customer_dialog_phoneofmessage.setVisibility(View.GONE);}public void dismiss() {dialog.dismiss();}}
在xml倒入dialog

然后在activity中创建

1

MyHandler myHandler = new MyHandler();// 接收事件public class MyHandler extends Handler {public MyHandler(Looper looper) {super(looper);}public MyHandler() {super();}@Overridepublic void handleMessage(Message msg) {Log.v("test", "1");switch (msg.what) {case 4:changeContent(VERIFICATION_CODE);myDialog.dismiss();mid_text.setText(R.string.title_text_content_mid_personalinfomation_verification_code);title_right_text_search.setVisibility(View.GONE);// myDialog.show_phone_sure();// myDialog.hide_phone_error();// myDialog.hide_warn_linear();// myDialog.show_button_ok();// myDialog.show_phoneofmessage(input_phone.getText().toString());Log.v("test", "执行这里");break;case 1:showWhichDialog(SURE_PHONUMER);break;case 2:showWhichDialog(ERROR_PAS);break;default:break;}}};

2

myDialog = new CustomerDialog(PersonalInfomation_Bind_Activity.this,myHandler);

在这里showwhichdialog就是显示dialog.可以显示不同的dialog,

这里我只贴入xml

<?xml version="1.0" encoding="utf-8"?><!-- 自定义的dialog布局 --><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:background="#ffffff"    android:orientation="vertical"    android:padding="30dp" >    <RelativeLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:gravity="center" >        <TextView            android:id="@+id/phone_error"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="手机号码错误"            android:textColor="@color/title_mid_text_color"            android:textSize="@dimen/title_mid_text_size" />        <TextView            android:id="@+id/phone_sure"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="确认手机号码"            android:textColor="@color/title_mid_text_color"            android:textSize="@dimen/title_mid_text_size"            android:visibility="gone" />    </RelativeLayout>    <RelativeLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_marginTop="20dp"        android:gravity="center" >        <LinearLayout            android:id="@+id/warn_linear"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:orientation="vertical" >            <TextView                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="您输入的是一个无效的手机号码"                android:textColor="@color/menu_text_loginmode_color" />        </LinearLayout>        <LinearLayout            android:id="@+id/phoneofmessage"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:gravity="center"            android:orientation="vertical"            android:visibility="gone" >            <TextView                android:id="@+id/notification2"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="我们将发送验证短信到这个号码" />            <TextView                android:id="@+id/notification3"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="+1861234567"                android:textColor="@color/menu_text_username_color" />        </LinearLayout>    </RelativeLayout>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_marginTop="20dp"        android:gravity="center" >        <Button            android:id="@+id/cancle"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:background="@color/menu_text_username_color"            android:padding="10dp"            android:text="取消"            android:textColor="@color/title_right_text_color_normal" />        <Button            android:id="@+id/sure"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_marginLeft="20dp"            android:background="@color/menu_text_username_color"            android:padding="10dp"            android:text="确认"            android:textColor="@color/title_right_text_color_normal" />    </LinearLayout></LinearLayout>

0 0
原创粉丝点击