1. 程式人生 > >利用相對佈局實現使用者註冊功能

利用相對佈局實現使用者註冊功能

執行效果如下圖所示: 在這裡插入圖片描述

一、實現佈局檔案: 1、註冊介面程式碼:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    android:background="@mipmap/backgroundimg"
    android:padding="15dp"
    tools:context=".RegistrationActivity">

    <TextView
        android:id="@+id/tv_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="30sp"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="30dp"
        android:layout_marginBottom="20dp"
        android:text="@string/title"/>

    <TextView
        android:id="@+id/tv_username"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:textColor="#000000"
        android:layout_marginBottom="15dp"
        android:layout_below="@id/tv_title"
        android:text="@string/username"/>

    <EditText
        android:id="@+id/edt_username"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/tv_username"
        android:layout_alignBaseline="@id/tv_username"
        android:layout_below="@id/tv_title"
        android:singleLine="true"
        android:hint="@string/input_username"/>

    <TextView
        android:id="@+id/tv_password"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:textColor="#000000"
        android:layout_marginBottom="15dp"
        android:layout_below="@id/tv_username"
        android:text="@string/password"/>

    <EditText
        android:id="@+id/edt_password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/tv_password"
        android:layout_alignLeft="@id/edt_username"
        android:layout_alignBaseline="@id/tv_password"
        android:layout_below="@id/edt_username"
        android:singleLine="true"
        android:inputType="textPassword"
        android:hint="@string/input_password"/>

    <TextView
        android:id="@+id/tv_phone"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:textColor="#000000"
        android:layout_marginBottom="15dp"
        android:layout_below="@id/tv_password"
        android:text="@string/phone"/>

    <EditText
        android:id="@+id/edt_phone"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/tv_phone"
        android:layout_alignLeft="@id/edt_password"
        android:layout_alignBaseline="@id/tv_phone"
        android:layout_below="@id/edt_password"
        android:singleLine="true"
        android:inputType="phone"
        android:hint="@string/input_phone"/>

    <TextView
        android:id="@+id/tv_email"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:textColor="#000000"
        android:layout_marginBottom="15dp"
        android:layout_below="@id/tv_phone"
        android:text="@string/email"/>

    <EditText
        android:id="@+id/edt_email"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/tv_email"
        android:layout_alignLeft="@id/edt_password"
        android:layout_alignBaseline="@id/tv_email"
        android:layout_below="@id/edt_phone"
        android:singleLine="true"
        android:inputType="textEmailAddress"
        android:hint="@string/input_email"/>

    <TextView
        android:id="@+id/tv_sex"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:textColor="#000000"
        android:layout_marginBottom="15dp"
        android:layout_below="@id/tv_email"
        android:text="@string/sex"/>

    <RadioGroup
        android:id="@+id/rgp_sex"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/tv_sex"
        android:layout_alignLeft="@id/edt_email"
        android:layout_alignTop="@id/tv_sex"

        android:orientation="horizontal"
        >
        <RadioButton
            android:id="@+id/rbn_male"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="@string/male"
            />
        <RadioButton
            android:id="@+id/rbn_female"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="20dp"
            android:text="@string/female"
            />
    </RadioGroup>


    <TextView
        android:id="@+id/tv_lovely"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:textColor="#000000"
        android:layout_marginBottom="15dp"
        android:layout_below="@id/tv_sex"
        android:text="@string/lovely"/>

    <CheckBox
        android:id="@+id/cb_travel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/travel"
        android:layout_toRightOf="@id/tv_lovely"
        android:layout_alignLeft="@id/edt_username"
        android:layout_alignBaseline="@id/tv_lovely"
       />
    <CheckBox
        android:id="@+id/cb_music"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/music"
        android:layout_toRightOf="@id/cb_travel"
        android:layout_alignBaseline="@id/tv_lovely"
        />
    <CheckBox
        android:id="@+id/cb_cook"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/cook"
        android:layout_toRightOf="@id/cb_music"
        android:layout_alignBaseline="@id/tv_lovely"
        />

    <Button
        android:id="@+id/btn_sure"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/sure"
        android:onClick="onSure"
        android:layout_below="@id/tv_lovely"/>
    <Button
        android:id="@+id/btn_resure"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/resure"
        android:onClick="onClear"
        android:layout_below="@id/tv_lovely"
        android:layout_alignParentRight="true"
        />

</RelativeLayout>

2、顯示介面程式碼:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".InformationActivity"
    android:background="@mipmap/timgss"
    >

    <TextView
        android:id="@+id/tv_info"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="27dp"
        android:text="@string/display_info"
        android:textColor="#0000ff"
        android:textSize="30sp" />

    <TextView
        android:id="@+id/tv_username"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/tv_info"
        android:layout_marginLeft="30dp"
        android:layout_marginTop="40dp"
        android:text="@string/username"
        android:autoLink="all"
        android:textSize="20sp" />

    <TextView
        android:id="@+id/tv_username_info"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/tv_username"
        android:layout_marginLeft="16dp"
        android:layout_toRightOf="@+id/tv_username"
        android:text="@string/username"
        android:autoLink="all"
        android:textColor="#ff00ff"
        android:textSize="20sp" />

    <TextView
        android:id="@+id/tv_password"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/tv_username"
        android:layout_marginLeft="30dp"
        android:layout_marginTop="20dp"
        android:text="@string/password"

        android:textSize="20sp" />

    <TextView
        android:id="@+id/tv_password_info"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/tv_password"
        android:layout_alignLeft="@+id/tv_username_info"
        android:layout_below="@+id/tv_username_info"
        android:text="@string/password"
        android:textColor="#ff00ff"
        android:textSize="20sp" />

    <TextView
        android:id="@+id/tv_phone"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/tv_password"
        android:layout_marginLeft="30dp"
        android:layout_marginTop="20dp"
        android:text="@string/phone"
        android:textSize="20sp" />

    <TextView
        android:id="@+id/tv_phone_info"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/tv_phone"
        android:layout_alignLeft="@+id/tv_password_info"
        android:layout_below="@+id/tv_password_info"
        android:text="@string/phone"
        android:textColor="#ff00ff"
        android:autoLink="all"
        android:textSize="20sp" />

    <TextView
        android:id="@+id/tv_email"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/tv_phone"
        android:layout_marginLeft="30dp"
        android:layout_marginTop="20dp"
        android:text="@string/email"
        android:textSize="20sp" />

    <TextView
        android:id="@+id/tv_email_info"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/tv_email"
        android:layout_alignLeft="@+id/tv_phone_info"
        android:layout_below="@+id/tv_phone_info"
        android:text="@string/email"
        android:textColor="#ff00ff"
        android:autoLink="all"
        android:textSize="20sp" />

    <TextView
        android:id="@+id/tv_gender"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/tv_email"
        android:layout_marginLeft="30dp"
        android:layout_marginTop="20dp"
        android:text="@string/gender"
        android:textSize="20sp" />

    <TextView
        android:id="@+id/tv_gender_info"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/tv_gender"
        android:layout_alignLeft="@+id/tv_email_info"
        android:layout_below="@+id/tv_email_info"
        android:text="@string/gender"
        android:textColor="#ff00ff"
        android:autoLink="all"
        android:textSize="20sp" />

    <TextView
        android:id="@+id/tv_hobby"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/tv_gender"
        android:layout_marginLeft="30dp"
        android:layout_marginTop="20dp"
        android:text="@string/hobby"
        android:textSize="20sp" />

    <TextView
        android:id="@+id/tv_hobby_info"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/tv_hobby"
        android:layout_alignLeft="@+id/tv_gender_info"
        android:layout_below="@+id/tv_gender_info"
        android:text="@string/hobby"
        android:textColor="#ff00ff"
        android:autoLink="all"
        android:textSize="20sp" />
</RelativeLayout>

3、string,.xml程式碼:

<resources>
    <string name="app_name">相對佈局:註冊個人資訊</string>
    <string name="title">註冊個人資訊</string>
    <string name="username">使用者名稱:</string>
    <string name="input_username">請輸入使用者名稱</string>
    <string name="password">密碼:</string>
    <string name="input_password">請輸入密碼</string>
    <string name="phone">電話:</string>
    <string name="input_phone">請輸入電話號碼</string>
    <string name="email">郵箱:</string>
    <string name="input_email">請輸入電子郵箱</string>
    <string name="sex">性別:</string>
    <string name="male">男</string>
    <string name="female">女</string>
    <string name="lovely">愛好:</string>
    <string name="travel">旅行</string>
    <string name="music">音樂</string>
    <string name="cook">美食</string>
    <string name="sure">提交</string>
    <string name="resure">重置</string>
    <string name="display_info">顯示個人資訊</string>
    <string name="gender">性別:</string>
    <string name="hobby">愛好:</string>
</resources>

二、實現事件響應檔案 1、註冊介面事件響應程式碼:

package com.example.a15114.registration;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

public class RegistrationActivity extends AppCompatActivity {
    private EditText edtusername;
    private EditText edtpassword;
    private EditText edtphone;
    private EditText edtemail;
    private RadioGroup rgpsex;
    private RadioButton rbmale;
    private RadioButton rbfemale;
    private CheckBox cbtravel;
    private CheckBox cbmusic;
    private CheckBox cbcook;
    String username,password,phone,email,sex,lovely;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_registration);
        edtusername=(EditText)findViewById(R.id.edt_username);
        edtpassword=(EditText)findViewById(R.id.edt_password);
        edtphone=(EditText)findViewById(R.id.edt_phone);
        edtemail=(EditText)findViewById(R.id.edt_email);
        rgpsex=(RadioGroup)findViewById(R.id.rgp_sex);
        rbmale=(RadioButton)findViewById(R.id.rbn_male);
        rbfemale=(RadioButton)findViewById(R.id.rbn_female);
        cbtravel=(CheckBox)findViewById(R.id.cb_travel);
        cbmusic=(CheckBox)findViewById(R.id.cb_music);
        cbcook=(CheckBox)findViewById(R.id.cb_cook);




    }
    public void onSure(View view){
        username=edtusername.getText().toString().trim();
        password=edtpassword.getText().toString().trim();
        phone=edtphone.getText().toString().trim();
        email=edtemail.getText().toString().trim();
        switch (rgpsex.getCheckedRadioButtonId()){
            case R.id.rbn_male:
                sex=rbmale.getText().toString();
                break;
            case R.id.rbn_female:
                sex=rbfemale.getText().toString();
                break;
        }
        StringBuilder builder=new StringBuilder();
        if(cbtravel.isChecked()){
            builder.append(cbtravel.getText().toString()+"");
        }
        if(cbmusic.isChecked()){
            builder.append(cbmusic.getText().toString()+"");
        }
        if (cbcook.isChecked()){
            builder.append(cbcook.getText().toString());
        }
        String  lovely=builder.toString().trim();

        //建立資料包
        Bundle data=new Bundle();
        Intent intent=new Intent(RegistrationActivity.this,InformationActivity.class);
        data.putString("username",username);
        data.putString("password",password);
        data.putString("phone",phone);
        data.putString("email",email);
        data.putString("sex",sex);
        data.putString("lovely",lovely);
        intent.putExtras(data);
        startActivity(intent);
    }
    public void onClear(View view){
        edtusername.setText("");
        edtpassword.setText("");
        edtphone.setText("");
        edtemail.setText("");
        rbmale.setChecked(true);
        rbfemale.setChecked(false);
        cbtravel.setChecked(false);
        cbmusic.setChecked(false);
        cbcook.setChecked(false);
    }
}

2、顯示資訊介面響應程式碼:

package com.example.a15114.registration;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class InformationActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_information);
        Intent intent=getIntent();
        if (intent!=null){
            Bundle data=intent.getExtras();
            String username=data.getString("username");
            String password=data.getString("password");
            String phone=data.getString("phone");
            String email=data.getString("email");
            String sex=data.getString("sex");
            String lovely=data.getString("lovely");
            TextView tv_username=(TextView)findViewById(R.id.tv_username_info);
            TextView tv_password=(TextView)findViewById(R.id.tv_password_info);
            TextView tv_phone=(TextView)findViewById(R.id.tv_phone_info);
            TextView tv_email=(TextView)findViewById(R.id.tv_email_info);
            TextView tv_sex=(TextView)findViewById(R.id.tv_gender_info);
            TextView tv_lovely=(TextView)findViewById(R.id.tv_hobby_info);

            tv_username.setText(username);
            tv_password.setText(password);
            tv_phone.setText(phone);
            tv_email.setText(email);
            tv_sex.setText(sex);
            tv_lovely.setText(lovely);

        }
    }
}