在路上

 找回密码
 立即注册
在路上 站点首页 学习 查看内容

java实现简单注册选择所在城市

2016-7-29 15:41| 发布者: zhangjf| 查看: 585| 评论: 0

摘要: 本文实例为大家分享了java实现简单注册选择所在城市的全部代码,供大家参考,具体内容如下 1.activity_main.xml ?xml version=1.0 encoding=utf-8?LinearLayout xmlns:android=http://schemas.android.com/apk ...

本文实例为大家分享了java实现简单注册选择所在城市的全部代码,供大家参考,具体内容如下

1.activity_main.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:orientation="vertical"
  5. android:layout_width="fill_parent"
  6. android:layout_height="fill_parent"
  7. >
  8. <TextView
  9. android:layout_width="wrap_content"
  10. android:layout_height="wrap_content"
  11. android:text="用户名:"
  12. />
  13. <EditText
  14. android:id="@+id/user"
  15. android:minWidth="200px"
  16. android:layout_width="wrap_content"
  17. android:layout_height="wrap_content" />
  18. <LinearLayout
  19. android:gravity="center_vertical"
  20. android:layout_width="match_parent"
  21. android:layout_height="wrap_content">
  22. <TextView
  23. android:layout_width="wrap_content"
  24. android:layout_height="wrap_content"
  25. android:text="性别:"
  26. />
  27. <RadioGroup
  28. android:id="@+id/sex"
  29. android:orientation="horizontal"
  30. android:layout_width="wrap_content"
  31. android:layout_height="wrap_content">
  32. <RadioButton
  33. android:id="@+id/radio0"
  34. android:layout_width="wrap_content"
  35. android:layout_height="wrap_content"
  36. android:checked="true"
  37. android:text="男"/>
  38. <RadioButton
  39. android:id="@+id/radio1"
  40. android:layout_width="wrap_content"
  41. android:layout_height="wrap_content"
  42. android:text="女"/>
  43. </RadioGroup>
  44. </LinearLayout>
  45. <LinearLayout
  46. android:orientation="vertical"
  47. android:layout_width="wrap_content"
  48. android:layout_height="wrap_content">
  49. <TextView android:id="@+id/textView1"
  50. android:text="请选择所在城市:"
  51. android:layout_height="wrap_content"
  52. android:layout_width="wrap_content"/>
  53. <Spinner
  54. android:entries="@array/ctype"
  55. android:layout_height="wrap_content"
  56. android:layout_width="wrap_content"
  57. android:id="@+id/spinner1"/>
  58. </LinearLayout>
  59. <TextView
  60. android:layout_width="wrap_content"
  61. android:layout_height="wrap_content"
  62. android:text="密码:"/>
  63. <EditText
  64. android:id="@+id/pwd"
  65. android:minWidth="200px"
  66. android:inputType="textPassword"
  67. android:layout_width="wrap_content"
  68. android:layout_height="wrap_content" />
  69. <TextView
  70. android:layout_width="wrap_content"
  71. android:layout_height="wrap_content"
  72. android:text="确认密码:"
  73. />
  74. <EditText
  75. android:id="@+id/repwd"
  76. android:minWidth="200px"
  77. android:inputType="textPassword"
  78. android:layout_width="wrap_content"
  79. android:layout_height="wrap_content" />
  80. <TextView
  81. android:id="@+id/textView3"
  82. android:layout_width="wrap_content"
  83. android:layout_height="wrap_content"
  84. android:text="E-mail地址:" />
  85. <EditText
  86. android:id="@+id/email"
  87. android:minWidth="400px"
  88. android:layout_width="wrap_content"
  89. android:layout_height="wrap_content" />
  90. <Button
  91. android:id="@+id/submit"
  92. android:layout_width="wrap_content"
  93. android:layout_height="wrap_content"
  94. android:text="提交" />
  95. </LinearLayout>
复制代码

2.register.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical"
  6. >
  7. <TextView
  8. android:id="@+id/user"
  9. android:layout_width="wrap_content"
  10. android:layout_height="wrap_content"
  11. android:padding="10px"
  12. android:text="用户名:" />
  13. <TextView
  14. android:id="@+id/sex"
  15. android:layout_width="wrap_content"
  16. android:layout_height="wrap_content"
  17. android:padding="10px"
  18. android:text="性别:"
  19. />
  20. <TextView
  21. android:id="@+id/city"
  22. android:layout_width="wrap_content"
  23. android:layout_height="wrap_content"
  24. android:padding="10px"
  25. android:text="城市:"
  26. />
  27. <TextView
  28. android:id="@+id/pwd"
  29. android:layout_width="wrap_content"
  30. android:layout_height="wrap_content"
  31. android:padding="10px"
  32. android:text="密码:" />
  33. <TextView
  34. android:id="@+id/email"
  35. android:padding="10px"
  36. android:layout_width="wrap_content"
  37. android:layout_height="wrap_content"
  38. android:text="E-mail:" />
  39. <Button
  40. android:id="@+id/back"
  41. android:text="返回上一步"
  42. android:layout_width="wrap_content"
  43. android:layout_height="wrap_content" />
  44. </LinearLayout>
复制代码

3. MainActivity.java

  1. package com.example.ejcker_llin.myapplication;
  2. import android.app.Activity;
  3. import android.content.Intent;
  4. import android.support.v7.app.AppCompatActivity;
  5. import android.os.Bundle;
  6. import android.view.View;
  7. import android.widget.AdapterView;
  8. import android.widget.Button;
  9. import android.widget.EditText;
  10. import android.widget.RadioButton;
  11. import android.widget.RadioGroup;
  12. import android.widget.Spinner;
  13. import android.widget.Toast;
  14. public class MainActivity extends Activity {
  15. private Button submit;
  16. private String sex1;
  17. private String city;
  18. final int code=0x717;
  19. @Override
  20. protected void onCreate(Bundle savedInstanceState) {
  21. super.onCreate(savedInstanceState);
  22. setContentView(R.layout.activity_main);
  23. submit= (Button) findViewById(R.id.submit);
  24. submit.setOnClickListener(new View.OnClickListener() {
  25. @Override
  26. public void onClick(View v) {
  27. String user=((EditText)findViewById(R.id.user)).getText().toString();
  28. String pwd=((EditText)findViewById(R.id.pwd)).getText().toString();
  29. String repwd=((EditText)findViewById(R.id.repwd)).getText().toString();
  30. String email=((EditText)findViewById(R.id.email)).getText().toString();
  31. RadioGroup sex= (RadioGroup) findViewById(R.id.sex);
  32. for(int i=0;i<sex.getChildCount();i++){
  33. RadioButton r= (RadioButton) sex.getChildAt(i);
  34. if(r.isChecked()){
  35. sex1=r.getText().toString();
  36. break;
  37. }
  38. }
  39. Spinner spinner= (Spinner) findViewById(R.id.spinner1);
  40. spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
  41. @Override
  42. public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
  43. city=parent.getItemAtPosition(position).toString();
  44. }
  45. @Override
  46. public void onNothingSelected(AdapterView<?> parent) {
  47. }
  48. });
  49. if(!"".equals(user)&&!"".equals(pwd)&&!"".equals(email)){
  50. if(!pwd.equals(repwd)){
  51. Toast.makeText(MainActivity.this,"两次输入的密码不一致,请重新输入!",Toast.LENGTH_LONG).show();
  52. ((EditText) findViewById(R.id.pwd)).setText("");
  53. ((EditText) findViewById(R.id.repwd)).setText("");
  54. ((EditText) findViewById(R.id.pwd)).requestFocus();
  55. }else {
  56. Intent intent=new Intent(MainActivity.this,RegisterAcivity.class);
  57. Bundle bundle=new Bundle();
  58. bundle.putCharSequence("user",user);
  59. bundle.putCharSequence("sex",sex1);
  60. bundle.putCharSequence("city",city);
  61. bundle.putCharSequence("pwd",pwd);
  62. bundle.putCharSequence("email",email);
  63. intent.putExtras(bundle);
  64. //startActivity(intent);
  65. startActivityForResult(intent,code);
  66. }
  67. }else {
  68. Toast.makeText(MainActivity.this,"请将注册信息输入完整!",Toast.LENGTH_LONG).show();
  69. }
  70. }
  71. });
  72. }
  73. }
复制代码

4. RegisterAcivity.java

  1. package com.example.ejcker_llin.myapplication;
  2. import android.app.Activity;
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.widget.Button;
  7. import android.widget.TextView;
  8. /**
  9. * Created by Jcker_llin on 2016/4/5.
  10. */
  11. public class RegisterAcivity extends Activity{
  12. @Override
  13. protected void onCreate(Bundle savedInstanceState) {
  14. super.onCreate(savedInstanceState);
  15. setContentView(R.layout.register);
  16. final Intent intent=getIntent();
  17. Bundle bundle=intent.getExtras();
  18. TextView user= (TextView) findViewById(R.id.user);
  19. user.setText("用户名:"+bundle.getString("user"));
  20. TextView sex= (TextView) findViewById(R.id.sex);
  21. sex.setText("性别:"+bundle.getString("sex"));
  22. TextView city= (TextView) findViewById(R.id.city);
  23. city.setText("城市:"+bundle.getString("city"));
  24. TextView pwd= (TextView) findViewById(R.id.pwd);
  25. pwd.setText("密码:"+bundle.getString("pwd"));
  26. TextView email= (TextView) findViewById(R.id.email);
  27. email.setText("E-mail:"+bundle.getString("email"));
  28. Button button= (Button) findViewById(R.id.back);
  29. button.setOnClickListener(new View.OnClickListener() {
  30. @Override
  31. public void onClick(View v) {
  32. setResult(0x717,intent);
  33. finish();
  34. }
  35. });
  36. }
  37. }
复制代码

5.

6.

7. arrays.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3. <string-array name="ctype">
  4. <item>北京</item>
  5. <item>上海</item>
  6. <item>广州</item>
  7. <item>杭州</item>
  8. <item>天津</item>
  9. <item>香港</item>
  10. <item>重庆</item>
  11. <item>西安</item>
  12. <item>其他</item>
  13. </string-array>
  14. </resources>
复制代码

以上就是本文的全部内容,希望对大家的学习有所帮助。

最新评论

小黑屋|在路上 ( 蜀ICP备15035742号-1 

;

GMT+8, 2025-5-6 09:00

Copyright 2015-2025 djqfx

返回顶部