1. 程式人生 > >註冊介面的實現

註冊介面的實現

下面先放上成品圖:

使用者的註冊介面其實就是幾個輸入框(EditText)和幾個TextView組成的,重點是所輸入的電話號碼和郵箱都能被識別出來並且以超連結的方式顯現出來,點選過後還會開啟相應的軟體,其實都歸功與TextView中的一個設定:

android:autoLink="all"

這串程式碼能自動識別出顯示的資料中是郵箱還是電話號碼並且以超連結的方式顯現出來,可以說是很殘暴了,實現註冊視窗具體如下:

使用了兩個視窗,一個註冊視窗,一個顯示註冊資訊的視窗,先看activity_register.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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@mipmap/back"
    android:orientation="vertical"
    android:padding="15dp"
    tools:context="com.example.administrator.login.RegisterActivity"
    >
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:orientation="horizontal"
        >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/name"
        android:textSize="25dp"
        />
    <EditText
        android:id="@+id/edt_name"
        android:layout_width="200dp"
        android:layout_height="wrap_content" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:orientation="horizontal"
        >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/sex"
            android:textSize="25dp"
            />
        <EditText
            android:id="@+id/edt_sex"
            android:layout_width="200dp"
            android:layout_height="wrap_content" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:orientation="horizontal"
        >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/age"
            android:textSize="25dp"
            />
        <EditText
            android:id="@+id/edt_age"
            android:numeric="decimal"
            android:layout_width="200dp"
            android:layout_height="wrap_content" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:orientation="horizontal"
        >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/phone"

            android:textSize="25dp"
            />
        <EditText
            android:id="@+id/edt_phone"
            android:phoneNumber="true"
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:autoLink="all"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:orientation="horizontal"
        >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/email"
            android:textSize="25dp"
            />
        <EditText
            android:id="@+id/edt_eamil"
            android:layout_width="230dp"
            android:layout_height="wrap_content"
            android:autoLink="all"
            android:singleLine="true"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="horizontal"
        >
       <Button
           android:id="@+id/btn_register"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="@string/register"/>
        <Button
            android:id="@+id/btn_canceltwo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/cancel"/>
    </LinearLayout>
</LinearLayout>

RegisterActivity程式碼如下:

package com.example.administrator.login;

import android.app.Activity;
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.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class RegisterActivity extends Activity {
    private EditText edtName;
    private EditText edtSex;
    private EditText edtAge;
    private EditText edtPhone;
    private EditText edtEmail;
    private Button btnRegister;
    private Button btnCanseltwo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        Intent intent = getIntent();
        edtName = (EditText) findViewById(R.id.edt_name);
        edtSex=(EditText) findViewById(R.id.edt_sex);
        edtAge=(EditText)findViewById(R.id.edt_age);
        edtPhone=(EditText)findViewById(R.id.edt_phone);
        edtEmail=(EditText)findViewById(R.id.edt_eamil);
        btnRegister=(Button)findViewById(R.id.btn_register);
        btnCanseltwo=(Button)findViewById(R.id.btn_canceltwo);

        btnCanseltwo.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v) {
                finish();
            }
        });

        btnRegister.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v){
                String name = edtName.getText().toString();
                String sex = edtSex.getText().toString();
                String phone = edtPhone.getText().toString();
                String email = edtEmail.getText().toString();
                String age = edtAge.getText().toString();
                if(name.equals("")||sex.equals("")||phone.equals("")||age.equals("")||email.equals("")){
                    Toast.makeText(RegisterActivity.this,"請確認資訊不為空", Toast.LENGTH_SHORT).show();
                }else{

                    Intent intent = new Intent(RegisterActivity.this,RegisterTwo.class);
                    Bundle bundle = new Bundle();
                    bundle.putString("name",name);
                    bundle.putString("sex",sex);
                    bundle.putString("phone",phone);
                    bundle.putString("email",email);
                    bundle.putString("age",age);
                    intent.putExtras(bundle);
                    startActivity(intent);
                }
            }
        });
        }
    }

activit_register_two.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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@mipmap/back"
    android:gravity="center"
    android:orientation="vertical"
    tools:context="com.example.administrator.login.RegisterTwo">
    <TextView
        android:id="@+id/tv_register"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="25sp"
        android:autoLink="all"
        />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_vertical">

        <Button
            android:id="@+id/btn_re"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/reture"/>

    </LinearLayout>


</LinearLayout>

RegisterTwo程式碼如下:

package com.example.administrator.login;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class RegisterTwo extends Activity {
    private TextView tvRegister;
   // private Button btnRe;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register_two);
       // btnRe.findViewById(R.id.btn_re);
        Intent intent = getIntent();
        tvRegister = (TextView) findViewById(R.id.tv_register);
        if(intent!=null){
            Bundle data = intent.getExtras();
            String name = data.getString("name");
            String age = data.getString("age");
            String phone = data.getString("phone");
            String sex = data.getString("sex");
            String email =data.getString("email");
            tvRegister.setText("姓名:" + name +
                    "\n性別:" + sex +
                    "\n年齡:" + age +
                    "\n電話:" + phone +
                    "\n郵箱:" + email);
        }
      /**  btnRe.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v) {
                Intent intent = new Intent(RegisterTwo.this,MainActivity.class);
                startActivity(intent);
            }
        });*/
    }

}