本文主要是介绍android程序一个注册,然后到设置兴趣爱好,出生日期,然后把所有信息显示在另一个页面的程序,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
这个程序要达到的效果简图如下图
第一个页面
public class MainActivity extends Activity implements OnClickListener,OnCheckedChangeListener {EditText username, password;RadioGroup sex;Button btn_register, btn_cancel;UserBean user;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);user = new UserBean();initView();}private void initView() {username = (EditText) findViewById(R.id.username);password = (EditText) findViewById(R.id.password);sex = (RadioGroup) findViewById(R.id.sex);btn_register = (Button) findViewById(R.id.btn_register);btn_cancel = (Button) findViewById(R.id.btn_cancel);btn_register.setOnClickListener(this);btn_cancel.setOnClickListener(this);sex.setOnCheckedChangeListener(this);// 给定一个初始状态sex.check(R.id.boy);}@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.btn_cancel:// 关闭finish();break;case R.id.btn_register:String uname = username.getText().toString();String pwd = password.getText().toString();// 非空if (TextUtils.isEmpty(uname) || TextUtils.isEmpty(pwd)) {Toast.makeText(this, "用户名或密码不能为空", Toast.LENGTH_SHORT).show();return;} else {user.setUsername(uname);user.setPassword(pwd);// 跳转并且携带数据Intent intent = new Intent(this, SetActivity.class);intent.putExtra("user", user);startActivity(intent);}break;default:break;}}@Overridepublic void onCheckedChanged(RadioGroup group, int checkedId) {// 状态改变时switch (checkedId) {case R.id.boy:user.setSex('男');break;case R.id.gril:user.setSex('女');break;}}
}
第一个页面的xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"android:padding="20dp"tools:context="com.example.lesson4_demo.MainActivity" ><EditText
android:id="@+id/username"android:layout_width="match_parent"android:layout_height="wrap_content"android:hint="请输入用户名" /><EditText
android:id="@+id/password"android:layout_width="match_parent"android:layout_height="wrap_content"android:hint="请输入密码" /><RadioGroup
android:id="@+id/sex"android:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="center"android:orientation="horizontal" ><RadioButton
android:id="@+id/boy"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"android:gravity="center"android:padding="10dp"android:text="男" /><RadioButton
android:id="@+id/gril"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"android:gravity="center"android:padding="10dp"android:text="女" /></RadioGroup><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="注册" /><Button
android:id="@+id/btn_cancel"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="取消" /></LinearLayout></LinearLayout>
新建user类拿来装信息
public class UserBean implements Serializable {/*** */private static final long serialVersionUID = 1123123123L;@Overridepublic String toString() {return "UserBean [username=" + username + ", password=" + password+ ", sex=" + sex + ", hobbit=" + hobbit + ", birthday="+ birthday + "]";}String username;String password;char sex;String hobbit;String birthday;public UserBean() {super();}public UserBean(String username, String password, char sex, String hobbit,String birthday) {super();this.username = username;this.password = password;this.sex = sex;this.hobbit = hobbit;this.birthday = birthday;}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}public char getSex() {return sex;}public void setSex(char sex) {this.sex = sex;}public String getHobbit() {return hobbit;}public void setHobbit(String hobbit) {this.hobbit = hobbit;}public String getBirthday() {return birthday;}public void setBirthday(String birthday) {this.birthday = birthday;}}
第二个页面 选择爱好和出生日期
public class SetActivity extends Activity implements OnCheckedChangeListener,OnClickListener {CheckBox cb_music, cb_lol, cb_swing;DatePicker datePicker;Button btn_finish;List<String> list = new ArrayList<String>();UserBean user;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_set);initView();user = (UserBean) getIntent().getSerializableExtra("user");}private void initView() {cb_music = (CheckBox) findViewById(R.id.cb_music);cb_lol = (CheckBox) findViewById(R.id.cb_lol);cb_swing = (CheckBox) findViewById(R.id.cb_swing);cb_music.setOnCheckedChangeListener(this);cb_lol.setOnCheckedChangeListener(this);cb_swing.setOnCheckedChangeListener(this);datePicker = (DatePicker) findViewById(R.id.datepicker);btn_finish = (Button) findViewById(R.id.btn_finish);// 初始化时间Calendar calendar = Calendar.getInstance();datePicker.init(calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH),new OnDateChangedListener() {@Overridepublic void onDateChanged(DatePicker view, int year,int monthOfYear, int dayOfMonth) {user.setBirthday(year + "-" + (monthOfYear + 1) + "-"+ dayOfMonth);}});btn_finish.setOnClickListener(this);}@Overridepublic void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {switch (buttonView.getId()) {case R.id.cb_music:if (isChecked)list.add("音乐");elselist.remove("音乐");break;case R.id.cb_lol:if (isChecked)list.add("英雄联盟");elselist.remove("英雄联盟");break;case R.id.cb_swing:if (isChecked)list.add("游泳");elselist.remove("游泳");break;}}@Overridepublic void onClick(View v) {// 将爱好设置进去if (list.size() > 0) {StringBuffer sb = new StringBuffer();for (String str : list) {sb.append(str + " ");}user.setHobbit(sb.toString());}// 跳转Intent intent = new Intent(this, ResultActivity.class);intent.putExtra("user", user);startActivity(intent);}}
第二个页面的xml
<?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"android:orientation="vertical" ><Button
android:id="@+id/btn_finish"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="完成" /><TextView
android:layout_width="match_parent"android:layout_height="wrap_content"android:background="#80cccccc"android:padding="5dp"android:text="兴趣爱好" /><CheckBox
android:id="@+id/cb_music"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="音乐" /><CheckBox
android:id="@+id/cb_lol"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="LOL" /><CheckBox
android:id="@+id/cb_swing"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="游泳" /><TextView
android:layout_width="match_parent"android:layout_height="wrap_content"android:background="#80cccccc"android:padding="5dp"android:text="生日" /><DatePicker
android:id="@+id/datepicker"android:layout_width="match_parent"android:layout_height="match_parent" /></LinearLayout>
最后一个显示的页面
public class ResultActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);TextView tv = new TextView(this);setContentView(tv);UserBean user = (UserBean) getIntent().getSerializableExtra("user");tv.setText(user.toString());}
}
这篇关于android程序一个注册,然后到设置兴趣爱好,出生日期,然后把所有信息显示在另一个页面的程序的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!