找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2774|回复: 0
打印 上一主题 下一主题
收起左侧

安卓Java源程序 带选择头像功能的用户注册 使用Intent回传数据

[复制链接]
跳转到指定楼层
楼主
本帖最后由 dori 于 2020-12-18 23:12 编辑

完成带选择头像功能的用户注册应用程序开发。
           图1
           图2
           图3

MainActivity.java文件:
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //获取界面上的组件
        Button bn=findViewById(R.id.bn);
        //为按钮绑定事件监听器
        bn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //创建需要对应于目标Activity的Intent
                Intent intent=new Intent(MainActivity.this,SecondActivity.class);
                //启动指定Activity并等待返回结果,其中0x11是请求码,用于标识该请求
                startActivityForResult(intent,0x11);
            }
        });
    }
    @Override
    public void onActivityResult(int requestCode,int resultCode,Intent intent)
    {
        //当requestCode、resultCode同时为0x11时,即处理特定的结果
        if(requestCode==0x11&&resultCode==0x11)
        {
            //取出Intent里的Extras数据
            Bundle data=intent.getExtras();
            //取出Bundle中的数据
            int imageId=data.getInt("ID");
            ImageView imageView=findViewById(R.id.iv);
            imageView.setImageResource(imageId);
        }
    }
}
SecondActivity.java文件:

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

public class SecondActivity extends Activity {
    //获取图片
    public int []imageId=new int []{R.drawable.img01,R.drawable.img02,R.drawable.img03,
            R.drawable.img04, R.drawable.img05,R.drawable.img06,R.drawable.img07,
            R.drawable.img08,R.drawable.img09};
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        //获取组件
        GridView gridView=findViewById(R.id.gridView1);
        BaseAdapter adapter=new BaseAdapter() {
            public View getView(int position, View convertView, ViewGroup parent) {
                ImageView imageView;
                if(convertView==null)
                {
                    imageView=new ImageView(SecondActivity.this);
                    imageView.setAdjustViewBounds(true);
                    imageView.setMaxWidth(158);
                    imageView.setMaxHeight(150);
                    imageView.setPadding(5, 5, 5, 5);
                }
                else
                {
                    imageView=(ImageView)convertView;
                }
                imageView.setImageResource(imageId[position]);
                return imageView;
            }
            @Override
            public long getItemId(int position) {
                return position;
            }
            @Override
            public Object getItem(int position) {
                return position;
            }
            @Override
            public int getCount() {
                return imageId.length;
            }
        };
        gridView.setAdapter(adapter);
        //绑定监听器
        gridView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //获得Intent
                Intent intent=getIntent();
                Bundle bundle=new Bundle();
                bundle.putInt("ID", imageId[position]);
                intent.putExtras(bundle);
                setResult(0x11,intent);
                finish();
            }
        });
    }
}
activity_main.xml文件:

<?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=".MainActivity">

    <LinearLayout
        android:id="@+id/linearLayout2"
        android:layout_width="247dp"
        android:layout_height="280dp"
        android:layout_marginTop="100dp"
        android:layout_marginEnd="2dp"
        android:layout_marginRight="2dp"
        android:layout_marginBottom="365dp"
        android:orientation="vertical"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/linearLayout"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="59dp">

            <TextView
                android:id="@+id/textView"
                android:layout_width="143dp"
                android:layout_height="match_parent"
                android:text="用户名:"
                android:textSize="25sp" />

            <EditText
                android:id="@+id/editText"
                android:layout_width="145dp"
                android:layout_height="match_parent"
                android:ems="10"
                android:gravity="start|top"
                android:inputType="textMultiLine" />
        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="59dp">

            <TextView
                android:id="@+id/textView2"
                android:layout_width="143dp"
                android:layout_height="match_parent"
                android:text="密码:"
                android:textSize="25sp" />

            <EditText
                android:id="@+id/editText2"
                android:layout_width="118dp"
                android:layout_height="match_parent"
                android:ems="10"
                android:inputType="number" />
        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="59dp">

            <TextView
                android:id="@+id/textView3"
                android:layout_width="143dp"
                android:layout_height="49dp"
                android:text="确认密码:"
                android:textSize="25sp" />

            <EditText
                android:id="@+id/editText3"
                android:layout_width="120dp"
                android:layout_height="match_parent"
                android:ems="10"
                android:inputType="number" />
        </TableRow>

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="59dp">

            <TextView
                android:id="@+id/textView4"
                android:layout_width="wrap_content"
                android:layout_height="59dp"
                android:text="E-mail地址:"
                android:textSize="25sp" />

            <EditText
                android:id="@+id/editText4"
                android:layout_width="103dp"
                android:layout_height="match_parent"
                android:ems="10"
                android:gravity="start|top"
                android:inputType="textMultiLine" />
        </TableRow>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="121dp"
        android:layout_height="283dp"
        android:layout_marginStart="2dp"
        android:layout_marginLeft="2dp"
        android:layout_marginTop="100dp"
        android:layout_marginBottom="363dp"
        android:orientation="vertical"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/linearLayout2"
        app:layout_constraintTop_toTopOf="parent">

        <ImageView
            android:id="@+id/iv"
            android:layout_width="match_parent"
            android:layout_height="170dp"
            app:srcCompat="@drawable/icon" />

        <Button
            android:id="@+id/bn"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="选择头像"
            android:textSize="20sp" />
    </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
avtivity_second.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <GridView
        android:id="@+id/gridView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="10px"
        android:horizontalSpacing="3px"
        android:verticalSpacing="3px"
        android:numColumns="4"/>
</LinearLayout>
使用Intent回传数据.rar (9.04 MB, 下载次数: 4)
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏1 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表