编写一个简单的andriod注册页面,并跳转后显示注册信息

编程入门 行业动态 更新时间:2024-10-20 15:58:23

编写一个简单的andriod注册页面,并<a href=https://www.elefans.com/category/jswz/34/1769274.html style=跳转后显示注册信息"/>

编写一个简单的andriod注册页面,并跳转后显示注册信息

简单的andriod注册页面

  • 功能要求
  • 具体实现
    • 注册页面
    • 信息显示页面
    • 程序运行结果

功能要求

1.注册页面
   (1)用户名
   (2)密码,确认密码
   (3)性别(单选)
   (4)学历 spinner
   (5)注册按钮

具体实现

在完成要求的过程中需要涉及五个文件的操作:
         Indexactivity:一开始的注册页面的activity。

         activity_index.xml:index的xml文件,控制注册页面的布局。

         ShowInfoactivity:显示注册信息的activity。

         activity_show_info.xml:显示注册信息页面的布局文件。

         strings.xml:字符串文件

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>

注册页面

   首先将xml布局文件写出一个大致的布局,再在activity中进行其他需要的操作。

               Indexactivity.java代码。使用SharedPreferences存储数据,注册页面控件都比较简单,主要是单选按钮和spinner下拉列表的操作。spinner下拉列表有两种方式,一种是直接在xml中定义,一种是利用适配器对下拉列表进行操作。下面是使用适配器的方式

package com.example.adwork;
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 IndexActivity extends Activity {EditText usename,usepwd,usepwd2;Button submit;RadioButton r1,r2;SharedPreferences sp;Spinner spn; String[] Item;String edu;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_index);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() {@Overridepublic void onItemSelected(AdapterView<?> parent, View view, int position, long id) {edu = Item[position];   //将选中的学历赋给edu//下面是我检验选择是否起作用的验证
//				Toast.makeText(IndexActivity.this, "你点击的是:"+edu, 2000).show();}@Overridepublic void onNothingSelected(AdapterView<?> parent) {// TODO Auto-generated method stub}});// 下面的代码是xml方式
/*		spn.setOnItemSelectedListener(new OnItemSelectedListener() {	//监听用户选择的是哪一学历@Overridepublic void onItemSelected(AdapterView<?> parent, View view, int position, long id) {// TODO Auto-generated method stubItem = getResources().getStringArray(R.array.choices);	//将选中的学历赋给eduedu = Item[position];
//				Toast.makeText(IndexActivity.this, "你点击的是:"+edu, 2000).show();}@Overridepublic void onNothingSelected(AdapterView<?> parent) {// TODO Auto-generated method stub}});*/submit.setOnClickListener(new View.OnClickListener() {			//按键响应@Overridepublic 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 (pwd01.equals(pwd02)) {								//判断两次输入的密码是否一致,若一致则继续,不一致则提醒密码不一致if (r1.isChecked()) {								//判断选中的性别,并赋给sexsex =  "男";}if (r2.isChecked()) {sex =  "女";}editor.putString("username", name);				editor.putString("password", pwd01);editor.putString("sex", sex);editor.putString("education", edu);					//将用户的注册信息存储进SharedPreferences对象editormit(); 									//提交
//					Toast.makeText(IndexActivity.this, "用户名"+name+"密码"+pwd01+"性别"+sex+"学历"+edu, Toast.LENGTH_LONG).show();Toast.makeText(IndexActivity.this, "注册成功!", Toast.LENGTH_LONG).show();		//提示注册成功的信息Intent intent = new Intent(	IndexActivity.this,ShowInfoActivity.class);			//跳转到ShowInfoactivitystartActivity(intent);}else {Toast.makeText(IndexActivity.this, "密码不一致!", Toast.LENGTH_LONG).show();			//提示密码不一致}}});}}

     activity_index.xml代码。

<LinearLayoutandroid:layout_width="fill_parent"android:layout_height="wrap_content" android:orientation="vertical"><!-- 用户名部分 --><TextViewandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:text="@string/name" /><EditTextandroid:id="@+id/usename"android:layout_width="fill_parent"android:layout_height="wrap_content"android:text=""/><!-- 密码部分 --><TextViewandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:text="@string/password"/><EditTextandroid:id="@+id/usepwd"android:layout_width="fill_parent"android:layout_height="wrap_content"android:password="true" /><!-- 确认密码部分 --><TextViewandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:text="@string/password02"/><EditTextandroid:id="@+id/usepwd2"android:layout_width="fill_parent"android:layout_height="wrap_content"android:password="true" /> <!-- 性别部分 --><TextViewandroid: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:id="@+id/boy"android:text="@string/boy"/><RadioButton android:id="@+id/girl"android:text="@string/girl"/></RadioGroup><!-- 学历部分 --><TextViewandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:text="@string/education"/><!--   android:entries="@array/choices" 使用xml方式的话需要将这句代码放入spinner空间中--><Spinnerandroid: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>

信息显示页面

     信息显示页面相对简单了许多,只需要将存储在SharedPreferences中的数据取出并显示出来即可。
ShowInfoactivity代码:

package com.example.adwork;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 ShowInfoActivity extends Activity {TextView showname,showpwd,showsex,showedu;SharedPreferences sp;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_show_info);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);								//设置显示文本框的内容和格式}}

activity_show_info.xml代码:

<LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" ><TextViewandroid:id="@+id/showname"android:layout_width="fill_parent"android:layout_height="wrap_content"/><TextViewandroid:id="@+id/showpwd"android:layout_width="fill_parent"android:layout_height="wrap_content"/><TextViewandroid:id="@+id/showsex"android:layout_width="fill_parent"android:layout_height="wrap_content"/><TextViewandroid:id="@+id/showedu"android:layout_width="fill_parent"android:layout_height="wrap_content"/></LinearLayout>

程序运行结果



注:头一回写文章,可能不全面。我本身也是大学生,正在学习这门课程,可能理解并不到位,记录一下自己写的东西。

更多推荐

编写一个简单的andriod注册页面,并跳转后显示注册信息

本文发布于:2024-02-11 20:12:02,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1683180.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:跳转   页面   简单   信息   andriod

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!