Android 应用开发:注册信息界面

1.目标结果:

注册界面注册信息显示
1. 完成注册信息界面。
2. 新建UI界面,显示注册数据。
( 实现在Java程序中获取数据、验证数据;以及提交数据到下一个Activity页面的功能。)


2.软件环境:

 JDK1.8.0_131
 Android Studio

3.实现过程 — 附代码供参考

step1:设计UI界面

这个部分比较简单,按照要求把控件放在布局中,并更改需要更改的属性。
代码会自动生成的!!!!
其中步骤D会有报错,不用担心,后续会添加资源文件。

A.将主页面修改为线性布局LinearLayout,垂直排列。
B.依次添加相应组件:用户名文本框和输入框,密码文本框和输入框,性别文本框和复选框。
C.添加联系电话文本框和输入框,更改输入类android:inputType =”text|phone”。
D.添加部门文本框和列表框,设置列表框数据来源(android:entries)depts.xml。
E.添加爱好文本框和一个线性布局,布局加入四个checkBoox组件表示四个爱好。(音乐、书籍、电影、运动)
F.添加一个确认按钮,设置单击响应(android:onClick)myclick方法。

UI设计完成时,界面布局如下:
在这里插入图片描述
step2:表示部门的Spinner组件,其数据来源文件depts.xml位于res/values 目录下。

文件目录结构如下:
在这里插入图片描述
这里附录depts.xml,文件代码

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="dept">
    <item>人力资源部</item>
    <item >销售部</item>
    <item >开发部</item>
    <item >财务部</item>
    </string-array>
</resources>

step3:设计后台程序 ⭐

A.在主Activity文件中,定义各个控件对象和一个爱好复选框对象favs动态数组。
B.在onCreate()方法中,获取组件。
C.获取性别方法。
D.获取爱好方法,爱好以逗号隔开。
E.检查通过时,输出注册信息,提交到下一个Activity页面。
F.新建一个activity_result.xml布局文件,放置一个文本框控件,并创建ResultActivity.class类,修改onCreat()方法,显示上页传来的数据。


这还有一个点,书上没有。就是新建的activty要在AndroidManifest.xml文件中注册。

要添加注册代码,或者在设置里进行注册。
三种方式选一个即可!!!!!!!

<activity android:name=".ResultActivity"/>
<activity android:name=".ResultActivity"></activity>
<activity android:name="com.....ResultActivity" android:label="@string/app_name" ></activity>

在这里插入图片描述


代码部分:
这部分为系统自动生成代码。

//这里附录activity_main.xml,文件代码
<?xml version="1.0" encoding="utf-8"?>
<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: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.dn.zxyapplication_02.MainActivity">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:weightSum="1">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="用户名"
            android:id="@+id/textView"
            android:textColor="#000000" />

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textPersonName"
            android:ems="10"
            android:id="@+id/name" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="密码"
            android:id="@+id/textView2"
            android:textColor="#000000" />

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="textPassword"
            android:ems="10"
            android:id="@+id/password" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="性别"
            android:id="@+id/textView3"
            android:textColor="#000000" />
            
        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <RadioGroup
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/sex">

            <RadioButton
                android:layout_width="100dp"
                android:layout_height="wrap_content"
                android:text="男"
                android:id="@+id/man"
                android:checked="true" />

            <RadioButton
                android:layout_width="100dp"
                android:layout_height="wrap_content"
                android:text="女"
                android:id="@+id/woman"
                android:checked="false" />
            </RadioGroup>
        </TableRow>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="联系电话"
            android:id="@+id/textView4"
            android:textColor="#000000" />

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="phone"
            android:ems="10"
            android:id="@+id/phone"
            android:onClick="myclick"
            android:nestedScrollingEnabled="true" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="部门"
            android:id="@+id/textView5"
            android:textColor="#000000" />

        <Spinner
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/dept"
            android:entries="@array/dept"
            android:layout_weight="0.17" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="爱好"
            android:id="@+id/textView6"
            android:textColor="#000000" />

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="myclick"
            android:nestedScrollingEnabled="false">

            <CheckBox
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="书籍"
                android:id="@+id/book"
                android:checked="false" />

            <CheckBox
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="运动"
                android:id="@+id/sport"
                android:checked="false" />

            <CheckBox
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="音乐"
                android:id="@+id/music"
                android:checked="false" />

            <CheckBox
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="电影"
                android:id="@+id/movie"
                android:checked="false" />

        </TableRow>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="确定"
            android:id="@+id/ok"
            android:onClick="myclick"
            android:layout_gravity="center_horizontal" />

    </LinearLayout>
</RelativeLayout>

1.这个部分是主后台程序。MainActivity.java

//MainActivity.java
package com.example.dn.zxyapplication_02;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;
import android.text.Editable;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;


public class
        MainActivity extends AppCompatActivity {

    public MainActivity() {


    }

    private ArrayList<CheckBox> favs;                //定义一个存放爱好中复选框的对象favs
    private EditText NAME;                          // 布局各组件对象
    private EditText PASSWORD;
    private EditText PHONE;
    private Spinner DEPT;
    private RadioGroup SEX;
    private RadioButton MAN;
    private RadioButton WOMAN;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //获取个组件对象
        NAME=(EditText)findViewById(R.id.name);
        PASSWORD=(EditText)findViewById(R.id.password);
        PHONE=(EditText)findViewById(R.id.phone);
        SEX=(RadioGroup)findViewById(R.id.sex);
        DEPT=(Spinner)findViewById(R.id.dept);
        MAN=(RadioButton)findViewById(R.id.man);
        WOMAN=(RadioButton)findViewById(R.id.woman);
        favs=new ArrayList<CheckBox>();
        CheckBox book=(CheckBox) findViewById(R.id.book);
        CheckBox sport=(CheckBox) findViewById(R.id.sport);
        CheckBox music=(CheckBox) findViewById(R.id.music);
        CheckBox movie=(CheckBox) findViewById(R.id.movie);
        favs.add(book);
        favs.add(sport);
        favs.add(music);
        favs.add(movie);

    }



    public String getSex(){
        RadioButton radioButton= (RadioButton)findViewById(SEX.getCheckedRadioButtonId());
        if(radioButton!=null) {
            return radioButton.getText().toString();
        }else {
            return "Not Selected";
        }
    }

    public String getFavorite()
    {
        String favo="";
        for(CheckBox cb:favs){
            if(cb.isChecked()){
                favo+=cb.getText().toString();
                favo+=",";
            }
        }
        if(!"".equals(favo))
            favo=favo.substring(0,favo.length()-1);
        else
            favo="您未选择爱好!";
        return favo;
    }

    public void myclick(View view){
        //if (true){
        StringBuilder sb =new StringBuilder();
        sb.append("用户名: "+NAME.getText().toString()+"\n");
        Log.v("name", "done");
        sb.append("性别: " + getSex() + "\n");
        Log.v("sex", "done");
        sb.append("电话: "+PHONE.getText().toString()+"\n");
        Log.v("tel","done");
        sb.append("部门: "+DEPT.getSelectedItem().toString()+"\n");
        Log.v("part","done");
        sb.append("爱好: "+getFavorite()+"\n");
        Log.v("fav","done");
        Toast.makeText(this, sb.toString(), Toast.LENGTH_LONG).show();
        // Toast.makeText(this, "Test", Toast.LENGTH_LONG).show();

       Intent intent = new Intent();
        intent.setClass(this, ResultActivity.class);
        intent.putExtra("info", sb.toString());
        this.startActivity(intent);
        //}
    }

}

2.结果展示的后台程序。ResultActivity.java

//ResultActivity.java
package com.example.dn.zxyapplication_02;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Window;
import android.widget.TextView;

/**
 * Created by DN on 2020/3/30.
 */


public class ResultActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);

        setContentView(R.layout.activity_result);

        TextView result = (TextView) findViewById(R.id.result);

        result.setText("从前一页面传来如下:\n\n" + this.getIntent().getStringExtra("info"));

    }
}

3.自动生成的xml布局文件。(里面只有一个TextView控件)activity_result.xml

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

    <TextView
        android:id="@+id/result"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
        
</LinearLayout>

  • 7
    点赞
  • 35
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Cyril-zxy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值