Android自定义弹出框

系统默认的弹框太丑陋了,可以根据个人的喜好自定义弹框,弹框页面布局代码如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/editpassword"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <EditText
        android:background="#bfbfbf"
        android:id="@+id/pwd"
        android:layout_width="match_parent"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:layout_height="35dp"
        android:ems="10" >
        <requestFocus />
    </EditText>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" 
        android:layout_marginTop="10dp"
        android:layout_marginBottom="5dp">

        <Button
            android:layout_marginLeft="20dp"
            android:id="@+id/ok"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/ok"
            android:background="#696969"
            android:textColor="#000000"
            android:textSize="20sp" />

        <Button
            android:layout_marginLeft="20dp"
            android:layout_marginRight="20dp"
            android:id="@+id/cancle"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="#696969"
            android:text="@string/cancle"
            android:textColor="#000000"
            android:textSize="20sp" />
    </LinearLayout>

</LinearLayout>

按钮点击事件:

        btn1.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				showDeviceName();
				
			}
		});


重点部分代码:

	public void showDeviceName(){
		AlertDialog.Builder builder=new AlertDialog.Builder(getActivity());
		LayoutInflater inflater=getActivity().getLayoutInflater();
		final View pswLayout=inflater.inflate(R.layout.edit_password, (ViewGroup)getActivity().findViewById(R.id.editpassword));
		Button btn_ok=(Button) pswLayout.findViewById(R.id.ok);
		Button btn_cancle=(Button) pswLayout.findViewById(R.id.cancle);
		btn_ok.setOnClickListener(new OnClickListener() { //弹框里"确定"按钮的点击事件
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				EditText etPassWord=(EditText) pswLayout.findViewById(R.id.pwd);
				String name=etPassWord.getText().toString().trim();//获取输入的用户名
				if(!name.equals("")){//如果输入的名字不为空
					btName.setText(name);
					setupinterface.setDeviceName(name);//设置系统的名字为输入的名字(方法略)
					showPasswordDialog.dismiss();
				}else{
					showPasswordDialog.dismiss();//如果是空,则返回
				}
				
			}
		});
		btn_cancle.setOnClickListener(new OnClickListener() {//弹框中"取消"按钮的点击事件:
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				showPasswordDialog.dismiss();
			}
		});
		builder.setTitle(R.string.setName);
		builder.setIcon(android.R.drawable.ic_dialog_info);
		builder.setView(pswLayout);
		showPasswordDialog = builder.create();
		showPasswordDialog.show();
	}

注意:在判断用户有没有输入的时候,name !=null && !name.equals("")判断条件。为null的意思是存不存在,.equals("")的意思是内容为空,但是它存在。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值