Android开发学习安卓注册完善表单判断跳转

activity_main注册页面

<?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:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <!-- 用户名部分 -->
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/name" />

        <EditText
            android:id="@+id/usename"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text=""
            />
        <!-- 密码部分 -->
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/password"
            />

        <EditText
            android:id="@+id/usepwd"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:password="true"

            />
        <!-- 确认密码部分 -->
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/password02"
            />

        <EditText
            android:id="@+id/usepwd2"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:password="true"

            />
        <!-- 性别部分 -->
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/sex"
            />
        <RadioGroup
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            >
            <RadioButton
                android:layout_height="match_parent"
                android:layout_width="match_parent"
                android:id="@+id/boy"
                android:text="@string/boy"
                />
            <RadioButton
                android:id="@+id/girl"
                android:text="@string/girl"
                />
        </RadioGroup>
        <!-- 学历部分 -->
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/education"
            />
        <!--   android:entries="@array/choices" 使用xml方式的话需要将这句代码放入spinner空间中-->
        <Spinner
            android:id="@+id/spinner1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />
        <!-- 注册按钮部分 -->
        <Button
            android:id="@+id/submit"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/submit" />


    </LinearLayout>


</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity注册界面Java

package com.example.myapplication;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Spinner;
import android.widget.Toast;

public class MainActivity extends Activity {
    
    
    EditText usename,usepwd,usepwd2;
    Button submit;
    RadioButton r1,r2;
    SharedPreferences sp;
    Spinner spn;
    String[] Item;
    String edu;
    @Override
    public void onCreate(Bundle savedInstanceState) {
    
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        usename = (EditText) this.findViewById(R.id.usename);			//用户名编辑框
        usepwd = (EditText) this.findViewById(R.id.usepwd);				//设置初始密码编辑框
        usepwd2 = (EditText) this.findViewById(R.id.usepwd2);			//二次输入密码编辑框
        submit =  (Button) this.findViewById(R.id.submit);				//注册按钮
        sp = this.getSharedPreferences("userinfo", this.MODE_PRIVATE);	//将用户注册的信息录入到SharedPreferences中进行存储
        r1 = (RadioButton) this.findViewById(R.id.boy);					//单选按钮男孩选项
        r2 = (RadioButton) this.findViewById(R.id.girl);				//单选按钮女孩选项
        spn = (Spinner) findViewById(R.id.spinner1);					//学历下拉列表

        Item = getResources().getStringArray(R.array.choices);         //获取提前在string.xml文件中定义好的学历下拉列表选项

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, Item);

        spn.setAdapter(adapter);

        spn.setOnItemSelectedListener(new OnItemSelectedListener() {
    
    

            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    
    
                edu = Item[position];   //将选中的学历赋给edu
                //下面是我检验选择是否起作用的验证
//				Toast.makeText(IndexActivity.this, "你点击的是:"+edu, 2000).show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
    
    
                // TODO Auto-generated method stub

            }
        });

        // 下面的代码是xml方式
/*		spn.setOnItemSelectedListener(new OnItemSelectedListener() {	//监听用户选择的是哪一学历

			@Override
			public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
				// TODO Auto-generated method stub

				Item = getResources().getStringArray(R.array.choices);	//将选中的学历赋给edu
				edu = Item[position];
//				Toast.makeText(IndexActivity.this, "你点击的是:"+edu, 2000).show();
			}

			@Override
			public void onNothingSelected(AdapterView<?> parent) {
				// TODO Auto-generated method stub

			}

		});*/


        submit.setOnClickListener(new View.OnClickListener() {
    
    			//按键响应

            @Override
            public void onClick(View v) {
    
    
                String name = usename.getText().toString();				//用户名
                String pwd01 = usepwd.getText().toString();				//密码
                String pwd02 = usepwd2.getText().toString();			//二次输入的密码
                String sex = "";										//性别
                SharedPreferences.Editor editor = sp.edit();			//sp的编辑对象editor,通过它进行数据的存取
                if(name.equals("")&&pwd01.equals("")&&pwd02.equals("")&&sex.equals("")){
    
    
                    Toast.makeText(MainActivity.this, "请将表单填写完整", Toast.LENGTH_LONG).show();
                }
               else if(name .equals("")){
    
    
                    Toast.makeText(MainActivity.this, "用户名不能为空", Toast.LENGTH_LONG).show();
                }
                else if(pwd01.equals("") || pwd02.equals("")){
    
    
                    Toast.makeText(MainActivity.this, "请将密码填写完整!", Toast.LENGTH_LONG).show();
                }
                else if (pwd01.equals(pwd02)) {
    
                                    //判断两次输入的密码是否一致,若一致则继续,不一致则提醒密码不一致

                        if (r1.isChecked()) {
    
                                    //判断选中的性别,并赋给sex
                            sex = "男";
                        }
                        if (r2.isChecked()) {
    
    
                            sex = "女";
                        }
                        editor.putString("username", name);
                        editor.putString("password", pwd01);
                        editor.putString("sex", sex);
                        editor.putString("education", edu);                    //将用户的注册信息存储进SharedPreferences对象
                        editor.commit();                                    //提交
//					Toast.makeText(IndexActivity.this, "用户名"+name+"密码"+pwd01+"性别"+sex+"学历"+edu, Toast.LENGTH_LONG).show();
                        Toast.makeText(MainActivity.this, "注册成功!", Toast.LENGTH_LONG).show();        //提示注册成功的信息
                        Intent intent = new Intent(MainActivity.this, Main2Activity.class);            //跳转到ShowInfoactivity
                        startActivity(intent);

                    }
                    else
                        {
    
    
                        Toast.makeText(MainActivity.this, "密码不一致!", Toast.LENGTH_LONG).show();			//提示密码不一致
                    }
                }
                

        });
    }


}

activity_main2跳转页面

<?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=".Main2Activity">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/showname"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            />

        <TextView
            android:id="@+id/showpwd"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            />

        <TextView
            android:id="@+id/showsex"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            />

        <TextView
            android:id="@+id/showedu"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            />

    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

Main2Activity跳转显示页面Java

package com.example.myapplication;

import android.os.Bundle;



import android.app.Activity;
import android.content.SharedPreferences;
import android.view.Menu;
import android.widget.EditText;
import android.widget.TextView;

public class Main2Activity extends Activity {
    
    
    TextView showname,showpwd,showsex,showedu;
    SharedPreferences sp;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        showname = (TextView) this.findViewById(R.id.showname);		//用户名显示文本框
        showpwd = (TextView) this.findViewById(R.id.showpwd);		//密码显示文本框
        showsex = (TextView) this.findViewById(R.id.showsex);		//性别显示文本框
        showedu = (TextView) this.findViewById(R.id.showedu);		//学历显示文本框
        sp = this.getSharedPreferences("userinfo", this.MODE_PRIVATE);	//接收用户信息

        String name = sp.getString("username", "");
        String pwd = sp.getString("password", "");
        String sex = sp.getString("sex", "");
        String edu = sp.getString("education", "");					//将用户的注册信息取出并赋值给对应的变量

        showname.setText("用户名:\t\t"+name);
        showpwd.setText("密码:\t\t"+pwd);
        showsex.setText("性别:\t\t"+sex);
        showedu.setText("学历:\t\t"+edu);								//设置显示文本框的内容和格式
    }


}

string.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">Adwork</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>
    <string name="name">用户名:</string>
    <string name="password">密 码:</string>
    <string name="password02">确认密码:</string>
    <string name="sex">性 别:</string>
    <string name="education">学 历:</string>
    <string name="submit">注册</string>
    <string name="boy"></string>
    <string name="girl"></string>

    <string-array name="choices">
        <item>小学</item>
        <item>初中 </item>
        <item>高中</item>
        <item>大学</item>
        <item>大学以上</item>
    </string-array>

    <string name="title_activity_show_info">ShowInfoActivity</string>

</resources>

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/m0_47987937/article/details/121031472