android 自定义实现圆角Dialog

  

        首先我们需要自定义dialog布局文件view_input_pwd_dialog.xml,如下:




    


    

    

    

    

        

dialog背景圆角定义dialog_bg.xml:



    
        
            
            
        
    



代码实现:

  LayoutInflater inflater = LayoutInflater
                .from(getActivity());
        LinearLayout layout = (LinearLayout) inflater.inflate(
                R.layout.view_input_pwd_dialog, null);
        final EditTextWithLabel pwdEt = (EditTextWithLabel) layout
                .findViewById(R.id.edit_text);
        Button firmBtn = (Button) layout
                .findViewById(R.id.btn_dialog_firm);
        Button cancleBtn = (Button) layout
                .findViewById(R.id.btn_dialog_give_up);
        TextView wifiNameTv = (TextView) layout.findViewById(R.id.wifi_name);
        wifiNameTv.setText(getString(R.string.share_wifi_name_lable, wifi.hot_name));
        pwdEt.getEditText().setInputType(InputType.TYPE_CLASS_TEXT
                | InputType.TYPE_TEXT_VARIATION_PASSWORD);
        pwdEt.getImageView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EditText editText = pwdEt.getEditText();
                int hide = InputType.TYPE_CLASS_TEXT
                        | InputType.TYPE_TEXT_VARIATION_PASSWORD;
                int show = InputType.TYPE_CLASS_TEXT
                        | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD;
                if (editText.getInputType() == hide) {
                    editText.setInputType(show);
                    pwdEt.getImageView().setImageResource(R.mipmap.pwd_show);
                } else {
                    editText.setInputType(hide);
                    pwdEt.getImageView().setImageResource(R.mipmap.pwd_hide);
                }
                pwdEt.getEditText().setSelection(pwdEt.getText().toString().length());
            }
        });

        final AlertDialog dlg = new AlertDialog.Builder(
                getActivity()).create();

        dlg.setView(layout, 0, 0, 0, 0);//防止黑边
        dlg.setCancelable(true);
        firmBtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                String pwd = pwdEt.getText().toString();
                if (StringUtils.isEmpty(pwd)) {
                    ToastUtils.show("密码不能为空,请确认");
                    return;
                }
                if (!StringUtils.isEmpty(pwd) && pwd.length() < 8) {
                    ToastUtils.show("密码位数不正确,请确认");
                    return;
                }

                wifi.password = pwd;
                mNetId = mHotManager.connectWiFi(wifi);
                mFragmentHomeBinding.header.setCenterText(wifi.hot_name, 20);
                updateConnectingUI(getResources().getString(R.string.be_connecting));
                isConnecting = true;
                mFragmentHomeBinding.header.startAnimation();
                dlg.dismiss();
            }
        });
        cancleBtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                dlg.dismiss();
            }
        });
        Window window = dlg.getWindow();
        window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));  // 有白色背景,加这句代码
        dlg.show();
        //获取屏幕的长宽
        WindowManager windowManager = getActivity().getWindowManager();
        Display display = windowManager.getDefaultDisplay();
        int screenheight = display.getHeight();
        int screenWidth = display.getWidth();

        //设置弹出框的长宽
        dlg.getWindow().setLayout(screenWidth * 4 / 5, screenheight * 2 / 5);

最终实现效果:

          android 自定义实现圆角Dialog_第1张图片

你可能感兴趣的:(Android,UI笔记)