Android 注册小实例
来源:互联网 发布:燕雀焉知鸿鹄之志图片 编辑:程序博客网 时间:2024/04/30 15:35
这两天在学习Android,看了几天视频以后,因为之前做过一个小的Android项目感觉看完视频没有什么很难的。但是因为我们做的那个项目之前都是封装好的,所以觉得好多东西都不会,没有自己搭建起一个项目。所以决定实战一下可能自己搭建的过程中就会遇到很多问题。做实例不是从登陆就是从注册开始做,我就从注册开始做起。
1.建包
首先建立一个项目里面有自己规定好的包,如图:
因为我只做了个注册,所以没有把包分的那么仔细。应该还有存放adpater,工具类等等,项目大的话把他们都分开。
2.编写界面布局activity_regesiter.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:background="#F0F0F0" android:orientation="vertical" > <LinearLayout style="@style/top_title_style" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="5dp" android:layout_marginTop="2dp" android:layout_weight="1" android:gravity="center" android:paddingLeft="4dp" android:text="注册" android:textColor="#FFFFFF" android:textSize="20sp" /> </LinearLayout> <RelativeLayout android:layout_width="fill_parent" android:layout_height="45dp" android:background="#FFFFFF" android:minHeight="50.0dip" android:layout_marginTop="30.0dip" android:paddingLeft="14.0dip" android:paddingRight="0dip" > <EditText android:id="@+id/et_register_username_id" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginLeft="0dp" android:background="#FFFFFF" android:hint="手机号" android:paddingLeft="15dip" android:textColorHint="#BEBEBE" android:textSize="18sp" /> <Button android:id="@+id/bt_getcode_id" android:layout_width="140dp" android:layout_height="match_parent" android:layout_centerVertical="true" android:layout_marginLeft="200dp" android:background="@drawable/shape1" android:gravity="center" android:text="获取验证码" android:textColor="#FFFFFF" android:textSize="14sp" /> </RelativeLayout> <View style="@style/PersonalLine" /> <RelativeLayout android:layout_width="fill_parent" android:layout_height="45dp" android:background="#FFFFFF" android:minHeight="50.0dip" android:paddingLeft="14.0dip" android:paddingRight="12.0dip" > <EditText android:id="@+id/et_register_code_id" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginLeft="0dp" android:background="@null" android:hint="请输入验证码" android:paddingLeft="15dip" android:textColorHint="#BEBEBE" android:textSize="18sp" /> </RelativeLayout> <View style="@style/PersonalLine" /> <RelativeLayout android:layout_width="fill_parent" android:layout_height="45dp" android:background="#FFFFFF" android:minHeight="50.0dip" android:paddingLeft="14.0dip" android:paddingRight="12.0dip" > <EditText android:id="@+id/et_register_password_id" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginLeft="0dp" android:background="@null" android:hint="请输入密码" android:inputType="textPassword" android:paddingLeft="15dip" android:textColorHint="#BEBEBE" android:textSize="18sp" /> </RelativeLayout> <View style="@style/PersonalLine" /> <Button android:id="@+id/bt_register_id" android:layout_width="300dp" android:layout_height="40dp" android:layout_marginTop="40dp" android:layout_gravity="center_horizontal" android:background="@drawable/shape2" android:gravity="center" android:text="注 册" android:textColor="#FFFFFF" android:textSize="18sp" /></LinearLayout>
3.抽取公共类URL,存放URL地址的
public class URL {private static String urlPrefix = "http://120.25.249.94/jrkj/GetJrkjData.ashx?op=";/** * 注册 */public final static String REGISTER_URL = urlPrefix + "RegistUser";}<span style="color:#3366ff;"></span>
4.编写实体UserEntity
/** * 注册实体类 * @author xiao * */public class UserEntity {private String result;private String data;public String getData() {return data;}public void setData(String data) {this.data = data;}public String getResult() {return result;}public void setResult(String result) {this.result = result;}}<span style="color:#3366ff;"></span>
5.编写RegesiterActivity
public class RegesiterActivity extends Activity implements OnClickListener {// 定义页面控件private EditText etUsername;private EditText etcode;private EditText etpassword;private Button btnCodeButton;private Button btnRegesiterButton;private TextView textView;// 定义变量private String userName;private String passWord;// 客户端输入的验证码private String valicationCode;// 服务器端获取的验证码private static String serverValicationCode;public boolean isChange = false;private boolean tag = true;private int i = 60;Thread thread = null;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.activity_regesiter);// 初始化页面控件initView();}/** * 初始化页面控件事件 */public void initView() {etUsername = (EditText) findViewById(R.id.et_register_username_id);etpassword = (EditText) findViewById(R.id.et_register_password_id);etcode = (EditText) findViewById(R.id.et_register_code_id);btnCodeButton = (Button) findViewById(R.id.bt_getcode_id);btnRegesiterButton = (Button) findViewById(R.id.bt_register_id);btnCodeButton.setOnClickListener(this);btnRegesiterButton.setOnClickListener(this);}@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.bt_getcode_id:// 判断用户名和密码是否输入if (!isvalidate())break;btnCodeButton.setText("获取验证码");btnCodeButton.setClickable(true);isChange = true;changeBtnGetCode();getValidateCode();break;case R.id.bt_register_id:register();break;default:break;}}/** * 判断用户名和密码是否输入 * * @return */private boolean isvalidate() {// 获取控件取值String username = etUsername.getText().toString().trim();String password = etpassword.getText().toString().trim();if (username == null || username.equals("")) {Toast.makeText(this, "手机号不能为空", Toast.LENGTH_SHORT).show();return false;}if (password == null || username.equals("")) {Toast.makeText(this, "密码不能为空", Toast.LENGTH_SHORT).show();return false;}return true;}/** * 点击验证码按钮 */private void changeBtnGetCode() {thread = new Thread() {@Overridepublic void run() {if (tag) {while (i > 0) {i--;if (RegesiterActivity.this == null) {break;}RegesiterActivity.this.runOnUiThread(new Runnable() {@Overridepublic void run() {btnCodeButton.setText("获取验证码(" + i + ")");btnCodeButton.setClickable(false);}});try {Thread.sleep(1000);} catch (InterruptedException e) {throw new RuntimeException(e);}}tag = false;}i = 60;tag = true;if (RegesiterActivity.this != null) {RegesiterActivity.this.runOnUiThread(new Runnable() {@Overridepublic void run() {btnCodeButton.setText("获取验证码");btnCodeButton.setClickable(true);}});}};};thread.start();}/** * 获取验证码 * * @return */public boolean getValidateCode() {String name = etUsername.getText().toString().trim();String code = etcode.getText().toString().trim();if (name.equals("")) {Toast.makeText(this, "请输入用户名或手机号!", Toast.LENGTH_SHORT).show();return false;} else {userName = name;valicationCode = code;Thread codeThread = new Thread(codeRunnable);codeThread.start();}return true;}/** 实例化的handler */private Handler mHandler = new Handler() {@Overridepublic void handleMessage(Message msg) {switch (msg.what) {case 1:serverValicationCode = (String) msg.obj;Toast.makeText(getBaseContext(), "获取验证码成功!", Toast.LENGTH_SHORT).show();break;case -1:Toast.makeText(getBaseContext(), "获取验证码失败!", Toast.LENGTH_SHORT).show();break;case 0:Toast.makeText(getBaseContext(), "哎呀,出错啦..", Toast.LENGTH_SHORT).show();break;case 2:Toast.makeText(getBaseContext(), "注册成功!", Toast.LENGTH_SHORT).show();Intent intent = new Intent();intent.setClass(RegesiterActivity.this, EditxuqiuActivity.class);startActivity(intent);break;default:break;}super.handleMessage(msg);}};/** 定义获取验证码的子线程 */private Runnable codeRunnable = new Runnable() {@Overridepublic void run() {Message msg = new Message();UserEntity userEntity = null;try {userEntity = requestByHttpPost();// 获取服务器数据serverValicationCode = userEntity.getResult();// 返回true则将消息的what值为1,为false则what为-1,异常为0if (serverValicationCode.equals("")) {msg.what = -1;} else {msg.what = 1;msg.obj = serverValicationCode;}} catch (Exception e) {msg.what = 0;e.printStackTrace();}mHandler.sendMessage(msg);}};/** * 从服务器获取验证码 * @return * @throws Exception */public UserEntity requestByHttpPost() throws Exception {UserEntity userEntity = null;String urlString = URL.REGISTER_URL;HttpGet getMethod = new HttpGet(urlString);HttpClient httpClient = new DefaultHttpClient();try {HttpResponse response = httpClient.execute(getMethod); // 发起GET请求System.out.println(response.toString());String strResponseString = EntityUtils.toString(response.getEntity(), "utf-8");userEntity = JSON.parseObject(strResponseString, UserEntity.class);} catch (ClientProtocolException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}return userEntity;}/** * 注册 * */public void register() {// 1.首先判断输入的值是否有效// 2.然后判断输入的验证码是否有效(防止没有点击获取验证码自己填的错误验证码)// 3.最后注册if (isValid()) {if (valicationCode.equals(serverValicationCode)) {Thread thread = new Thread(sRunnable);thread.start();} else {Toast.makeText(this, "输入的验证码不正确!", Toast.LENGTH_SHORT).show();}}}/** * 注册之前判断数据是否为空 * */public boolean isValid() {userName = etUsername.getText().toString().trim();valicationCode = etcode.getText().toString().trim();passWord = etpassword.getText().toString().trim();if (userName.equals("")) {Toast.makeText(this, "请输入手机号!", Toast.LENGTH_SHORT).show();return false;}if (valicationCode.equals("")) {Toast.makeText(this, "验证码不能为空!", Toast.LENGTH_SHORT).show();return false;}if (!serverValicationCode.equals(valicationCode)) {Toast.makeText(this, "验证码错误", Toast.LENGTH_SHORT).show();return false;}if (passWord.equals("")) {Toast.makeText(this, "密码不能为空!", Toast.LENGTH_SHORT).show();return false;} else if (passWord.length() < 6) {Toast.makeText(this, "密码至少6位!", Toast.LENGTH_SHORT).show();return false;}return true;}/** 自定义注册子线程 */private Runnable sRunnable = new Runnable() {@Overridepublic void run() {Message msg = new Message();try {//获取数据UserEntity userentity=getRegesiterData();String result=userentity.getResult();// 返回值为success表示成功if (result.equals("success")) {msg.what = 2;msg.obj = result;} else if (result.equals("false")) {msg.what = -1;}} catch (Exception e) {msg.what = 0;e.printStackTrace();}mHandler.sendMessage(msg);}};/** * 从服务器获取注册是否成功的消息 * @return * @throws Exception */public UserEntity getRegesiterData() throws Exception {String urlString = URL.REGISTER_URL;UserEntity userEntity = null;HttpGet getMethod = new HttpGet(urlString);HttpClient httpClient = new DefaultHttpClient();try {HttpResponse response = httpClient.execute(getMethod); // 发起GET请求System.out.println(response.toString());String strResponseString = EntityUtils.toString(response.getEntity(), "utf-8");userEntity = JSON.parseObject(strResponseString, UserEntity.class);} catch (ClientProtocolException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}return userEntity;}}<span style="color:#3366ff;"></span>
6.配置AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.smile.jrkjshizhan" android:versionCode="1" android:versionName="1.0" > <!-- 设置通信权限 --> <uses-permission android:name="android.permission.INTERNET" /> <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@android:style/Theme.Light.NoTitleBar" > <!-- 注册 --> <activity android:name="com.smile.jrkjshizhan.ui.RegesiterActivity" android:label="@string/app_name" > <!-- 设置启动开启页面 --> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
最后一定不要忘记配置通信权限,我就是忘记了配置通信权限,所以访问不了借口的数据。
给大家上一个注册图:
原本想着录屏来着,模拟器怎么也打不开,就给大家上一张图片吧。。。
这是一个完整的注册实例,大家按照这个写,就能完成注册的功能。献给那些初学者们!欢迎大家多多提意见!最后在Activity中有两次访问url,一次是从服务器中获取验证码,一次是从服务器中获取注册信息,里面有好多重复的代码,目前正在封装中,稍后呈现给大家。
2 0
- Android 注册小实例
- SSH框架注册小实例
- AJax / JQuery 注册应用小实例
- Android JNI 显示注册实例
- Android对话框实例-注册对话框
- android动态注册广播实例
- Android JNI小实例
- Ajax小实例2验证注册框-经验总结
- android 桌面小部件实例
- Android中Handler的总结(三)实例用户注册
- Android入门实例三 注册界面的设计与实现
- Android练习-----实例应用:设计用户注册界面
- Android小程序-Walker注册页面(二)
- Android小程序-Walker注册页面校验(三)
- android progress bar的小实例
- Android应用小实例--炫酷计时器
- android---LinearLayout,listener的计算器小实例
- Android学习之ImageSwitcher小实例
- ORACLE 10.2.5垮版本升级11.2.2 for windows 详细文档
- JSON和XML的区别
- Kibana User Guide [4.2] » Visualize » Data Table
- Android 触摸手势基础 官方文档概览
- 实习三个月小结
- Android 注册小实例
- 单链表(C实现)
- 1009. 说反话 (20)
- 实现C/S程序的自动更新2
- weblogic 服务开机自动启动
- 谈谈使用VFW在windows下编程控制摄像头(一)。 【孙涛】
- 【五】注入框架RoboGuice使用:(Your First POJO Injection)
- 数据压缩与信息熵
- 给我们自己一个空间—兄弟连IT教育