1. 程式人生 > 實用技巧 >註冊介面

註冊介面

<?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
="#F5F4F4" android:orientation="vertical" tools:context=".MainActivity"> <TextView android:id="@+id/textView" android:layout_width="match_parent" android:layout_height="72dp" android:background="#673AB7" android:enabled="true" android:gravity
="center" android:text="註冊介面" android:textColor="#090909" android:textSize="30sp" /> <TableLayout android:layout_width="match_parent" android:layout_height="256dp" android:gravity="center"> <TableRow android:layout_width
="match_parent" android:layout_height="match_parent" android:gravity="center"> <TextView android:id="@+id/name" android:layout_width="wrap_content" android:layout_height="30dp" android:enabled="true" android:text="使用者名稱:" android:textSize="20sp" android:textStyle="bold" /> <EditText android:id="@+id/tname" android:layout_width="wrap_content" android:layout_height="wrap_content" android:ems="10" android:inputType="textPersonName" /> </TableRow> <TableRow android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center"> <TextView android:id="@+id/password" android:layout_width="wrap_content" android:layout_height="30dp" android:enabled="true" android:text="密碼:" android:textSize="20sp" android:textStyle="bold" /> <EditText android:id="@+id/tpass" android:layout_width="wrap_content" android:layout_height="wrap_content" android:ems="10" android:inputType="numberPassword" /> </TableRow> </TableLayout> <TableLayout android:layout_width="match_parent" android:layout_height="196dp" android:gravity="center"> <TableRow android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:enabled="true" android:text="性別:" android:textSize="20sp" android:textStyle="bold" /> <RadioGroup android:id="@+id/r" android:layout_width="220dp" android:layout_height="wrap_content" android:orientation="horizontal"> <RadioButton android:id="@+id/girl" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="女" /> <RadioButton android:id="@+id/boy" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="男" /> </RadioGroup> </TableRow> <TableRow android:layout_width="match_parent" android:layout_height="200dp" android:gravity="center"> <TextView android:id="@+id/textView7" android:layout_width="wrap_content" android:layout_height="wrap_content" android:enabled="true" android:text="愛好:" android:textSize="20sp" android:textStyle="bold" /> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <CheckBox android:id="@+id/p" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="繪畫" /> <CheckBox android:id="@+id/basketball" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="打籃球" /> </LinearLayout> </TableRow> <TableRow android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center"> <TextView android:id="@+id/textView8" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20sp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <CheckBox android:id="@+id/s" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="唱歌" /> <CheckBox android:id="@+id/d" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="跳舞" /> </LinearLayout> </TableRow> </TableLayout> <Button android:id="@+id/button" android:layout_width="123dp" android:layout_height="59dp" android:layout_gravity="center" android:background="#673AB7" android:text="註冊" android:textAllCaps="false" android:textColor="#252523" android:textStyle="bold" /> </LinearLayout>
package com.example.myapplication;

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

public class MainActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {

    private String hobby;
    final Intent intent = new Intent();
    private EditText n;
    private EditText password;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        n = (EditText)findViewById(R.id.tname);
        password = (EditText)findViewById(R.id.tpass);
        RadioGroup rg = (RadioGroup)findViewById(R.id.r);
        CheckBox print = (CheckBox) findViewById(R.id.p);
        CheckBox basketball = (CheckBox)findViewById(R.id.basketball);
        CheckBox sing = (CheckBox)findViewById(R.id.s);
        CheckBox dance = (CheckBox)findViewById(R.id.d);
        hobby = new String();

        //設定跳轉到的Activity
        intent.setClass(MainActivity.this,z.class);



        //為RadioGroup設定監聽
        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                if (i == R.id.girl){
                    intent.putExtra("gender", "女");
                }else if (i == R.id.boy){
                    intent.putExtra("gender", "男");
                }
            }
        });

        //為CheckBox設定監聽
        print.setOnCheckedChangeListener(this);
        basketball.setOnCheckedChangeListener(this);
        sing.setOnCheckedChangeListener(this);
        dance.setOnCheckedChangeListener(this);


        //為 Button設定監聽
        Button button = (Button)findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Bundle bundle = new Bundle();
                bundle.putString("name",n.getText().toString());
                bundle.putString("password",password.getText().toString());
                intent.putExtras(bundle);
                startActivity(intent);

            }
        });
    }

    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
        String motion = compoundButton.getText().toString();
        if (b){
            if(!hobby.contains(motion)){
                hobby = hobby + motion;
                intent.putExtra("hobby", hobby);
            }
        }else{
            if (hobby.contains(motion)){
                hobby = hobby.replace(motion,"");
                intent.putExtra("hobby", hobby);
            }
        }
    }

}

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

    <TextView
        android:id="@+id/textView2"
        android:layout_width="match_parent"
        android:layout_height="83dp"
        android:background="#673AB7"
        android:gravity="center"
        android:text="註冊資訊"
        android:textColor="#070707"
        android:textSize="25sp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="25dp"
        android:gravity="center"
        android:orientation="vertical">

        <TextView
            android:id="@+id/textView3"
            android:layout_width="match_parent"
            android:layout_height="63dp"
            android:text="你的註冊資訊是:"
            android:textSize="20sp" />

        <TableLayout
            android:layout_width="match_parent"
            android:layout_height="400dp">

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

                <TextView
                    android:id="@+id/textView4"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="使用者名稱:"
                    android:textSize="20sp" />

                <TextView
                    android:id="@+id/show_name"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />
            </TableRow>

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

                <TextView
                    android:id="@+id/textView9"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="密碼:"
                    android:textSize="20sp" />

                <TextView
                    android:id="@+id/show_pass"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />
            </TableRow>

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

                <TextView
                    android:id="@+id/textView11"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="性別:"
                    android:textSize="20sp" />

                <TextView
                    android:id="@+id/show_gender"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />
            </TableRow>

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

                <TextView
                    android:id="@+id/textView13"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="愛好:"
                    android:textSize="20sp" />

                <TextView
                    android:id="@+id/show_hobby"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />
            </TableRow>
        </TableLayout>
    </LinearLayout>
</LinearLayout>
package com.example.myapplication;

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

public class z  extends Activity {

    public void startActivity(Intent intent){

    }
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.h);
       /* Button button=(Button)findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {

                Intent intent=new Intent(z.this,MainActivity.class);

                startActivity(intent);

            }
        });*/

        TextView show_name = (TextView)findViewById(R.id.show_name);
        TextView show_pass =(TextView)findViewById(R.id.show_pass);
        TextView show_gender = (TextView)findViewById(R.id.show_gender);
        TextView show_hobby =(TextView)findViewById(R.id.show_hobby);
        Intent intent = getIntent();
        Bundle bundle = getIntent().getExtras();
        show_name.setText(bundle.getString("name"));
        show_pass.setText(bundle.getString("password"));
        show_gender.setText(intent.getStringExtra("gender"));
        show_hobby.setText(intent.getStringExtra("hobby"));

    }

    public void finish(){

    }
}
AndroidManifest.xml檔案

<?
xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.myapplication"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <meta-data android:name="com.google.android.actions" android:resource="@xml/shape" /> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".z" android:label="介面"> </activity> </application> </manifest>

AndroidManifest.xml