activity_main注册页面
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 adapter = new ArrayAdapter(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方式
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跳转页面
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("用户名:tt"+name);
showpwd.setText("密码:tt"+pwd);
showsex.setText("性别:tt"+sex);
showedu.setText("学历:tt"+edu); //设置显示文本框的内容和格式
}
}
string.xml
Adwork Settings Hello world! 用户名: 密 码: 确认密码: 性 别: 学 历: 注册 男 女 - 小学
- 初中
- 高中
- 大学
- 大学以上
ShowInfoActivity



