android自定义 dialog

本文介绍了一种自定义Dialog的实现方式,包括初始化、显示、隐藏等基本操作,并提供了错误提示、确认信息等多种状态展示的方法。通过示例代码详细说明了如何在Android应用中使用该Dialog。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这里以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() {
			@Override
			public 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() {
			@Override
			public 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();
		}

		@Override
		public 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>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值