PopupWindow在注册页面中的简单应用

activity 文件

public  class RegisterActivity extends Activity implements View.OnClickListener {
    private EditText eUsername;
    private EditText ePassword1;
    private EditText ePassword2;
    private Button submitBtn;
    private Button LoginButton;

    private ImageViewRoundOval iv_circle;
    private static int REQUEST_THUMBNAIL = 1;// 请求缩略图信号标识

    private float scaleWidth=1;
    private float scaleHeight=1;
   
    private static final int GALLERY_REQUEST_CODE = 0;    // 相册选图标记
    private static final int CAMERA_REQUEST_CODE = 1;    // 相机拍照标记
    private Button takePhotoBtn, pickPictureBtn, cancelBtn;
    private PopupWindow popupWindow;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        initViews();
    }

    private void initViews() {
        eUsername = (EditText) findViewById(R.id.new_username);
        ePassword1 = (EditText) findViewById(R.id.new_password_1);
        ePassword2 = (EditText) findViewById(R.id.new_password_2);

        submitBtn = (Button) findViewById(R.id.new_btn_submit);
        LoginButton = (Button) findViewById(R.id.btnLogin);

        submitBtn.setOnClickListener(this);
        LoginButton.setOnClickListener(this);
        iv_circle=(ImageViewRoundOval)findViewById(roundRect3);
        iv_circle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showPopupWindow();
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.new_btn_submit:
                //添加你的事件xxx
                break;
            case R.id.btnLogin:
                //添加你的事件
                break;
            default:
                break;
        }
    }

    private void  OpenCamera(){
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(intent, CAMERA_REQUEST_CODE);
    }

    private void pickFromGallery() {
            popupWindow.dismiss();
            Intent pickIntent = new Intent(Intent.ACTION_PICK, null);
            // 如果限制上传到服务器的图片类型时可以直接写如:"image/jpeg 、 image/png等的类型"
            pickIntent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
            startActivityForResult(pickIntent, GALLERY_REQUEST_CODE);
    }


    private void showPopupWindow() {
       /* if (popupWindow != null && popupWindow.isShowing()) {
            return;
        }*/
        //设置contentView
        View contentView = LayoutInflater.from(RegisterActivity.this).inflate(R.layout.layout_picture_selector, null);
        popupWindow = new PopupWindow(contentView,
                WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT, true);

        //点击空白处时,隐藏掉pop窗口
        popupWindow.setFocusable(true);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());

        popupWindow.setContentView(contentView);
        //显示PopupWindow
        View rootview = LayoutInflater.from(RegisterActivity.this).inflate(R.layout.activity_register, null);
        popupWindow.showAtLocation(rootview, Gravity.BOTTOM, 0, 0);
        //设置各个控件的点击响应
        takePhotoBtn = (Button) contentView.findViewById(R.id.picture_selector_take_photo_btn);
        pickPictureBtn = (Button)contentView.findViewById(R.id.picture_selector_pick_picture_btn);
        cancelBtn = (Button) contentView.findViewById(R.id.picture_selector_cancel_btn);
        setButtonListeners();
    }

    private void setButtonListeners() {
                takePhotoBtn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (popupWindow != null && popupWindow.isShowing()) {
                            OpenCamera();
                        }
                    }
                });
                pickPictureBtn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (popupWindow != null && popupWindow.isShowing()) {
                            pickFromGallery();
                        }
                    }
                });

                cancelBtn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (popupWindow != null && popupWindow.isShowing()) {
                            popupWindow.dismiss();
                        }
                    }
                });
    }

register.xml文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@mipmap/pic1"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.Register.RegisterActivity" >

    <EditText
        android:id="@+id/new_password_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/txtWrite"
        android:layout_alignLeft="@+id/new_username"
        android:ems="10"
        android:inputType="textPassword" />

    <EditText
        android:id="@+id/new_username"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/new_password_1"
        android:layout_alignParentRight="true"
        android:layout_marginRight="15dp"
        android:ems="10" />

    <com.example.Register.ImageViewRoundOval
        android:id="@+id/roundRect3"
        android:layout_width="180dp"
        android:layout_height="180dp"
        android:src="@mipmap/userpic"
        android:layout_above="@+id/btnLogin"
        android:layout_alignTop="@+id/new_username">
    </com.example.Register.ImageViewRoundOval>
    <TextView
        android:id="@+id/txtSound"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/new_password_1"
        android:layout_alignLeft="@+id/txtWrite"
        android:text="用户名:"
        android:textSize="18sp" />

    <EditText
        android:id="@+id/new_password_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/new_password_1"
        android:layout_centerVertical="true"
        android:ems="10"
        android:inputType="textPassword" />

    <TextView
        android:id="@+id/txtWrite"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/new_password_2"
        android:layout_alignLeft="@+id/TextView01"
        android:text="密码:"
        android:textSize="18sp" />

    <TextView
        android:id="@+id/TextView01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/new_password_2"
        android:layout_toLeftOf="@+id/new_password_2"
        android:text="确认密码:"
        android:textSize="18sp" />

    <CheckBox
        android:id="@+id/checkBox1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/new_password_2"
        android:layout_toRightOf="@+id/txtWrite"
        android:text="我已阅读并接受用户协议"
        android:textSize="14sp" />

    <Button
        android:id="@+id/new_btn_submit"
        android:layout_width="100dp"
        android:layout_height="38dp"
        android:layout_below="@+id/checkBox1"
        android:layout_centerHorizontal="true"
        android:text="提交注册"
        android:textSize="12sp" />

    <Button
        android:id="@+id/btnLogin"
        android:layout_width="100dp"
        android:layout_height="38dp"
        android:layout_alignBaseline="@+id/new_btn_submit"
        android:layout_alignBottom="@+id/new_btn_submit"
        android:layout_alignRight="@+id/new_password_2"
        android:text="点击登录"
        android:textSize="12sp" />

</RelativeLayout>

popupwindow.xml文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:orientation="vertical">

    <LinearLayout
        android:id="@+id/pop_layout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="#444"
        android:gravity="center_horizontal"
        android:orientation="vertical">

        <Button
            android:id="@+id/picture_selector_take_photo_btn"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dip"
            android:layout_marginRight="10dip"
            android:layout_marginTop="10dp"
            android:background="#4d69ff"
            android:padding="10dp"
            android:text="拍照"
            android:textColor="#CEC9E7"
            android:textSize="18sp"
            android:textStyle="bold" />

        <Button
            android:id="@+id/picture_selector_pick_picture_btn"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dip"
            android:layout_marginRight="10dip"
            android:layout_marginTop="5dp"
            android:background="#4d69ff"
            android:padding="10dp"
            android:text="从相册选择"
            android:textColor="#CEC9E7"
            android:textSize="18sp"
            android:textStyle="bold" />

        <Button
            android:id="@+id/picture_selector_cancel_btn"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="15dip"
            android:layout_marginLeft="10dip"
            android:layout_marginRight="10dip"
            android:layout_marginTop="20dp"
            android:background="@android:color/white"
            android:padding="10dp"
            android:text="取消"
            android:textColor="#373447"
            android:textSize="18sp"
            android:textStyle="bold" />
    </LinearLayout>

</RelativeLayout>

呵呵不见了

以下为activity文件的说明

1、给iv_circle添加新的点击事件(iv_circle是自定义的ImageView,是圆形的),添加点击iv_circle后,显示PopupWindow。

        iv_circle=(ImageViewRoundOval)findViewById(roundRect3);
        iv_circle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showPopupWindow();
            }
        });

2、打开照相机

 private void  OpenCamera(){
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(intent, CAMERA_REQUEST_CODE);
    }

3、打开相册

private void pickFromGallery() {
            popupWindow.dismiss();
            Intent pickIntent = new Intent(Intent.ACTION_PICK, null);
            // 如果限制上传到服务器的图片类型时可以直接写如:"image/jpeg 、 image/png等的类型"
            pickIntent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
            startActivityForResult(pickIntent, GALLERY_REQUEST_CODE);
    }

4、显示PopupWindow

    private void showPopupWindow() {
       /* if (popupWindow != null && popupWindow.isShowing()) {
            return;
        }*/
        //设置contentView
        View contentView = LayoutInflater.from(RegisterActivity.this).inflate(R.layout.layout_picture_selector, null);
        popupWindow = new PopupWindow(contentView,
                WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT, true);

        //点击空白处时,隐藏掉pop窗口
        popupWindow.setFocusable(true);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());

        popupWindow.setContentView(contentView);
        //显示PopupWindow
        View rootview = LayoutInflater.from(RegisterActivity.this).inflate(R.layout.activity_register, null);
        popupWindow.showAtLocation(rootview, Gravity.BOTTOM, 0, 0);
        //设置各个控件的点击响应
        takePhotoBtn = (Button) contentView.findViewById(R.id.picture_selector_take_photo_btn);
        pickPictureBtn = (Button)contentView.findViewById(R.id.picture_selector_pick_picture_btn);
        cancelBtn = (Button) contentView.findViewById(R.id.picture_selector_cancel_btn);
        setButtonListeners();
    }

5、设置PopupWindow中的按钮点击事件

    private void setButtonListeners() {
                takePhotoBtn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (popupWindow != null && popupWindow.isShowing()) {
                   
                            OpenCamera();
                        }
                    }
                });
                pickPictureBtn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (popupWindow != null && popupWindow.isShowing()) {
                
                            pickFromGallery();
                        }
                    }
                });

                cancelBtn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (popupWindow != null && popupWindow.isShowing()) {
                         
                            popupWindow.dismiss();
                        }
                    }
                });
    }

猜你喜欢

转载自my.oschina.net/u/3724795/blog/1593371
今日推荐