Android Studio 实现注册界面选择头像功能{记录5}{跳转到文件管理选择图片}

注册界面:

代码:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".RegActivity"
    android:padding="20dp">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="80dp"
                android:orientation="horizontal">

                <TextView
                    android:id="@+id/tvuser"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="用户名" />

                <EditText
                    android:id="@+id/user"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="4"
                    android:ems="10"
                    android:hint="请输入用户名"
                    android:inputType="textPersonName"
                    android:minHeight="48dp" />
            </LinearLayout>
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="80dp"
                android:gravity="center"
                android:orientation="horizontal">

                <TextView
                    android:id="@+id/textView"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:gravity="left"
                    android:text="使用头像" />

                <ImageView
                    android:id="@+id/reg_img"
                    android:layout_width="0dp"
                    android:layout_height="70dp"
                    android:layout_weight="3"/>
                <Button
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/btn_choose"
                    android:text="选择头像">

                </Button>


            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="80dp"
                android:orientation="horizontal">

                <TextView
                    android:id="@+id/tvpwo"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="密码" />

                <EditText
                    android:id="@+id/pwo"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="4"
                    android:ems="10"
                    android:hint="请输入密码"
                    android:inputType="textPassword"
                    android:minHeight="48dp" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="80dp"
                android:orientation="horizontal">

                <TextView
                    android:id="@+id/tvrepwo"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="确认密码" />

                <EditText
                    android:id="@+id/rpw"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:ems="10"
                    android:hint="请确认密码"
                    android:inputType="textPassword"
                    android:minHeight="48dp" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="80dp"
                android:orientation="horizontal">

                <TextView
                    android:id="@+id/tvname"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="姓名" />

                <EditText
                    android:id="@+id/uname"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:ems="10"
                    android:hint="请输入您的姓名"
                    android:inputType="textPersonName"
                    android:minHeight="48dp" />

            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="30dp"
                android:orientation="horizontal">

                <TextView
                    android:id="@+id/sex"
                    android:layout_width="145dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:text="性别"
                    tools:text="性别" />

                <RadioGroup
                    android:id="@+id/sexq"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:orientation="horizontal">

                    <RadioButton
                        android:id="@+id/ButtonM"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:text="男"
                        tools:ignore="TouchTargetSizeCheck" />

                    <RadioButton
                        android:id="@+id/ButtonW"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:text="女"
                        tools:ignore="TouchTargetSizeCheck" />
                </RadioGroup>

            </LinearLayout>

            <CheckBox
                android:id="@+id/checkBox3"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="已阅读会员协议" />

            <Button
                android:id="@+id/buttonR"
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:text="注册" />

            <Button
                android:id="@+id/buttonB"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="返回" />

            <TextView
                android:id="@+id/tvRC"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />
        </LinearLayout>
    </ScrollView>

</androidx.constraintlayout.widget.ConstraintLayout>

功能代码RegActivity.java

package com.example.gridviewtest;

import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class RegActivity extends AppCompatActivity {
ImageView iv;
Button btn_choose;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register);
        iv=(ImageView) findViewById(R.id.reg_img);
        btn_choose=(Button)findViewById(R.id.btn_choose);
        btn_choose.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                mGetContent.launch("image/*");
            }
        });
    }

    ActivityResultLauncher<String> mGetContent = registerForActivityResult(new ActivityResultContracts.GetContent(),
            new ActivityResultCallback<Uri>() {
                @Override
                public void onActivityResult(Uri uri) {
                    // Handle the returned Uri
                }
            });
}

 

posted @ 2022-04-11 12:17  ILEQ  阅读(257)  评论(0编辑  收藏  举报