Android 开发笔记___登陆app

Posted alm

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android 开发笔记___登陆app相关的知识,希望对你有一定的参考价值。

  1 package com.example.alimjan.hello_world;
  2 
  3 /**
  4  * Created by alimjan on 7/4/2017.
  5  */
  6 
  7 
  8         import android.content.Context;
  9         import android.support.v7.app.AppCompatActivity;
 10         import android.app.AlertDialog;
 11         import android.content.DialogInterface;
 12         import android.content.Intent;
 13         import android.os.Bundle;
 14         import android.text.Editable;
 15         import android.text.TextWatcher;
 16         import android.view.View;
 17         import android.view.View.OnClickListener;
 18         import android.widget.AdapterView;
 19         import android.widget.ArrayAdapter;
 20         import android.widget.Button;
 21         import android.widget.CheckBox;
 22         import android.widget.CompoundButton;
 23         import android.widget.EditText;
 24         import android.widget.RadioButton;
 25         import android.widget.RadioGroup;
 26         import android.widget.Spinner;
 27         import android.widget.TextView;
 28         import android.widget.Toast;
 29         import android.widget.AdapterView.OnItemSelectedListener;
 30 
 31 public class class_3_6 extends AppCompatActivity implements OnClickListener {
 32 
 33     private RadioGroup rg_login;
 34     private RadioButton rb_password;
 35     private RadioButton rb_verifycode;
 36     private EditText et_phone;
 37     private TextView tv_password;
 38     private EditText et_password;
 39     private Button btn_forget;
 40     private CheckBox ck_remember;
 41     private Button btn_login;
 42 
 43     private int mRequestCode = 0;
 44     private int mType = 0;
 45     private boolean bRemember = false;
 46     private String mPassword = "111111";
 47     private String mVerifyCode;
 48 
 49     @Override
 50     protected void onCreate(Bundle savedInstanceState) {
 51         super.onCreate(savedInstanceState);
 52         setContentView(R.layout.code_3_6);
 53         rg_login = (RadioGroup) findViewById(R.id.rg_login);
 54         rb_password = (RadioButton) findViewById(R.id.rb_password);
 55         rb_verifycode = (RadioButton) findViewById(R.id.rb_verifycode);
 56         et_phone = (EditText) findViewById(R.id.et_phone);
 57         tv_password = (TextView) findViewById(R.id.tv_password);
 58         et_password = (EditText) findViewById(R.id.et_password);
 59         btn_forget = (Button) findViewById(R.id.btn_forget);
 60         ck_remember = (CheckBox) findViewById(R.id.ck_remember);
 61         btn_login = (Button) findViewById(R.id.btn_login);
 62 
 63         rg_login.setOnCheckedChangeListener(new RadioListener());
 64         ck_remember.setOnCheckedChangeListener(new CheckListener());
 65         et_phone.addTextChangedListener(new HideTextWatcher(et_phone));
 66         et_password.addTextChangedListener(new HideTextWatcher(et_password));
 67         btn_forget.setOnClickListener(this);
 68         btn_login.setOnClickListener(this);
 69 
 70         ArrayAdapter<String> typeAdapter = new ArrayAdapter<String>(this,
 71                 R.layout.item_select, typeArray);
 72         typeAdapter.setDropDownViewResource(R.layout.item_dropdown);
 73         Spinner sp_type = (Spinner) findViewById(R.id.sp_type);
 74         sp_type.setPrompt("请选择用户类型");
 75         sp_type.setAdapter(typeAdapter);
 76         sp_type.setSelection(mType);
 77         sp_type.setOnItemSelectedListener(new TypeSelectedListener());
 78     }
 79 
 80     private class RadioListener implements RadioGroup.OnCheckedChangeListener {
 81         @Override
 82         public void onCheckedChanged(RadioGroup group, int checkedId) {
 83             if (checkedId == R.id.rb_password) {
 84                 tv_password.setText("登录密码:");
 85                 et_password.setHint("请输入密码");
 86                 btn_forget.setText("忘记密码");
 87                 ck_remember.setVisibility(View.VISIBLE);
 88             } else if (checkedId == R.id.rb_verifycode) {
 89                 tv_password.setText(" 验证码:");
 90                 et_password.setHint("请输入验证码");
 91                 btn_forget.setText("获取验证码");
 92                 ck_remember.setVisibility(View.INVISIBLE);
 93             }
 94         }
 95     }
 96 
 97     private String[] typeArray = {"个人用户", "公司用户"};
 98     class TypeSelectedListener implements OnItemSelectedListener {
 99         public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
100             mType = arg2;
101         }
102 
103         public void onNothingSelected(AdapterView<?> arg0) {
104         }
105     }
106 
107     private class CheckListener implements CompoundButton.OnCheckedChangeListener {
108         @Override
109         public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
110             if (buttonView.getId() == R.id.ck_remember) {
111                 bRemember = isChecked;
112             }
113         }
114     }
115 
116     private class HideTextWatcher implements TextWatcher {
117         private EditText mView;
118         private int mMaxLength;
119         private CharSequence mStr;
120 
121         public HideTextWatcher(EditText v) {
122             super();
123             mView = v;
124             mMaxLength = ViewUtil.getMaxLength(v);
125         }
126 
127         @Override
128         public void beforeTextChanged(CharSequence s, int start, int count, int after) {
129         }
130 
131         @Override
132         public void onTextChanged(CharSequence s, int start, int before, int count) {
133             mStr = s;
134         }
135 
136         @Override
137         public void afterTextChanged(Editable s) {
138             if (mStr == null || mStr.length() == 0)
139                 return;
140             if ((mStr.length() == 11 && mMaxLength == 11) ||
141                     (mStr.length() == 6 && mMaxLength == 6)) {
142                 ViewUtil.hideOneInputMethod(class_3_6.this, mView);
143             }
144         }
145     }
146 
147     @Override
148     public void onClick(View v) {
149         String phone = et_phone.getText().toString();
150         if (v.getId() == R.id.btn_forget) {
151             if (phone==null || phone.length()<11) {
152                 Toast.makeText(this, "请输入正确的手机号", Toast.LENGTH_SHORT).show();
153                 return;
154             }
155             if (rb_password.isChecked() == true) {
156                 Intent intent = new Intent(this, class_3_6_1.class);
157                 intent.putExtra("phone", phone);
158                 startActivityForResult(intent, mRequestCode);
159             } else if (rb_verifycode.isChecked() == true) {
160                 mVerifyCode = String.format("%06d", (int)(Math.random()*1000000%1000000));
161                 AlertDialog.Builder builder = new AlertDialog.Builder(this);
162                 builder.setTitle("请记住验证码");
163                 builder.setMessage("手机号"+phone+",本次验证码是"+mVerifyCode+",请输入验证码");
164                 builder.setPositiveButton("好的", null);
165                 AlertDialog alert = builder.create();
166                 alert.show();
167             }
168         } else if (v.getId() == R.id.btn_login) {
169             if (phone==null || phone.length()<11) {
170                 Toast.makeText(this, "请输入正确的手机号", Toast.LENGTH_SHORT).show();
171                 return;
172             }
173             if (rb_password.isChecked() == true) {
174                 if (et_password.getText().toString().equals(mPassword) != true) {
175                     Toast.makeText(this, "请输入正确的密码", Toast.LENGTH_SHORT).show();
176                     return;
177                 } else {
178                     loginSuccess();
179                 }
180             } else if (rb_verifycode.isChecked() == true) {
181                 if (et_password.getText().toString().equals(mVerifyCode) != true) {
182                     Toast.makeText(this, "请输入正确的验证码", Toast.LENGTH_SHORT).show();
183                     return;
184                 } else {
185                     loginSuccess();
186                 }
187             }
188         }
189     }
190 
191     @Override
192     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
193         if (requestCode == mRequestCode && data!=null) {
194             //用户密码已改为新密码
195             mPassword = data.getStringExtra("new_password");
196         }
197     }
198 
199     //从修改密码页面返回登录页面,要清空密码的输入框
200     @Override
201     protected void onRestart() {
202         et_password.setText("");
203         super.onRestart();
204     }
205 
206     private void loginSuccess() {
207         String desc = String.format("您的手机号码是%s,类型是%s。恭喜你通过登录验证,点击“确定”按钮返回上个页面",
208                 et_phone.getText().toString(), typeArray[mType]);
209         AlertDialog.Builder builder = new AlertDialog.Builder(this);
210         builder.setTitle("登录成功");
211         builder.setMessage(desc);
212         builder.setPositiveButton("确定返回", new DialogInterface.OnClickListener() {
213             @Override
214             public void onClick(DialogInterface dialog, int which) {
215                 finish();
216             }
217         });
218         builder.setNegativeButton("我再看看", null);
219         AlertDialog alert = builder.create();
220         alert.show();
221     }
222 
223     public static void startHome(Context mContext) {
224         Intent intent = new Intent(mContext, class_3_6.class);
225         mContext.startActivity(intent);
226     }
227 
228 }
 1 package com.example.alimjan.hello_world;
 2 
 3 /**
 4  * Created by alimjan on 7/4/2017.
 5  */
 6 
 7         import android.app.Activity;
 8         import android.support.v7.app.AppCompatActivity;
 9         import android.app.AlertDialog;
10         import android.content.Intent;
11         import android.os.Bundle;
12         import android.view.View;
13         import android.view.View.OnClickListener;
14         import android.widget.EditText;
15         import android.widget.Toast;
16 
17 public class class_3_6_1 extends AppCompatActivity implements OnClickListener {
18 
19     private EditText et_password_first;
20     private EditText et_password_second;
21     private EditText et_verifycode;
22     private String mVerifyCode;
23     private String mPhone;
24 
25     @Override
26     protected void onCreate(Bundle savedInstanceState) {
27         super.onCreate(savedInstanceState);
28         setContentView(R.layout.code_3_6_1);
29         et_password_first = (EditText) findViewById(R.id.et_password_first);
30         et_password_second = (EditText) findViewById(R.id.et_password_second);
31         et_verifycode = (EditText) findViewById(R.id.et_verifycode);
32         findViewById(R.id.btn_verifycode).setOnClickListener(this);
33         findViewById(R.id.btn_confirm).setOnClickListener(this);
34         mPhone = getIntent().getStringExtra("phone");
35     }
36 
37     @Override
38     public void onClick(View v) {
39         if (v.getId() == R.id.btn_verifycode) {
40             if (mPhone==null || mPhone.length()<11) {
41                 Toast.makeText(this, "请输入正确的手机号", Toast.LENGTH_SHORT).show();
42                 return;
43             }
44             mVerifyCode = String.format("%06d", (int) (Math.random() * 1000000 % 1000000));
45             AlertDialog.Builder builder = new AlertDialog.Builder(this);
46             builder.setTitle("请记住验证码");
47             builder.setMessage("手机号"+mPhone+",本次验证码是"+mVerifyCode+",请输入验证码");
48             builder.setPositiveButton("好的", null);
49             AlertDialog alert = builder.create();
50             alert.show();
51         } else if (v.getId() == R.id.btn_confirm) {
52             String password_first = et_password_first.getText().toString();
53             String password_second = et_password_second.getText().toString();
54             if (password_first==null || password_first.length()<6 ||
55                     password_second==null || password_second.length()<6) {
56                 Toast.makeText(this, "请输入正确的新密码", Toast.LENGTH_SHORT).show();
57                 return;
58             }
59             if (password_first.equals(password_second) != true) {
60                 Toast.makeText(this, "两次输入的新密码不一致", Toast.LENGTH_SHORT).show();
61                 return;
62             }
63             if (et_verifycode.getText().toString().equals(mVerifyCode) != true) {
64                 Toast.makeText(this, "请输入正确的验证码", Toast.LENGTH_SHORT).show();
65                 return;
66             } else {
67                 Toast.makeText(this, "密码修改成功", Toast.LENGTH_SHORT).show();
68                 Intent intent = new Intent();
69                 intent.putExtra("new_password", password_first);
70                 setResult(Activity.RESULT_OK, intent);
71                 finish();
72             }
73         }
74     }
75 
76 }

 

  1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2     android:layout_width="match_parent"
  3     android:layout_height="match_parent"
  4     android:focusable="true"
  5     android:focusableInTouchMode="true"
  6     android:orientation="vertical"
  7     android:padding="5dp" >
  8 
  9     <RadioGroup
 10         android:id="@+id/rg_login"
 11         android:layout_width="match_parent"
 12         android:layout_height="60dp"
 13         android:orientation="horizontal" >
 14 
 15         <RadioButton
 16             android:id="@+id/rb_password"
 17             android:layout_width="0dp"
 18             android:layout_height="match_parent"
 19             android:layout_weight="1"
 20             android:checked="true"
 21             android:gravity="left|center"
 22             android:text="密码登录"
 23             android:textColor="@color/black"
 24             android:textSize="17sp" />
 25 
 26         <RadioButton
 27             android:id="@+id/rb_verifycode"
 28             android:layout_width="0dp"
 29             android:layout_height="match_parent"
 30             android:layout_weight="1"
 31             android:checked="false"
 32             android:gravity="left|center"
 33             android:text="验证码登录"
 34             android:textColor="@color/black"
 35             android:textSize="17sp" />
 36     </RadioGroup>
 37 
 38     <RelativeLayout
 39         android:layout_width="match_parent"
 40         android:layout_height="60dp" >
 41 
 42         <TextView
 43             android:id="@+id/tv_type"
 44             android:layout_width="wrap_content"
 45             android:layout_height="match_parent"
 46             android:layout_alignParentLeft="true"
 47             android:gravity="center"
 48             android:text="  我是:"
 49             android:textColor="@color/black"
 50             android:textSize="17sp" />
 51 
 52         <Spinner
 53             android:id="@+id/sp_type"
 54             android:layout_width="match_parent"
 55             android:layout_height="match_parent"
 56             android:layout_toRightOf="@+id/tv_type"
 57             android:gravity="left|center"
 58             android:spinnerMode="dialog" />
 59     </RelativeLayout>
 60 
 61     <RelativeLayout
 62         android:layout_width="match_parent"
 63         android:layout_height="60dp" >
 64 
 65         <TextView
 66             android:id="@+id/tv_phone"
 67             android:layout_width="wrap_content"
 68             android:layout_height="match_parent"
 69             android:layout_alignParentLeft="true"
 70             android:gravity="center"
 71             android:text="手机号码:"
 72             android:textColor="@color/black"
 73             android:textSize="17sp" />
 74 
 75         <EditText
 76             android:id="@+id/et_phone"
 77             android:layout_width="match_parent"
 78             android:layout_height="match_parent"
 79             android:layout_marginBottom="5dp"
 80          

以上是关于Android 开发笔记___登陆app的主要内容,如果未能解决你的问题,请参考以下文章

Android 开发笔记___存储方式__共享参数__sharedprefences

(转)博客园登陆__JSEncrypt 分析

Android开发——UI_片段

Android内核源码bionic目录下的子目录arch-arm源码分析笔记

Android_校易app开发日志_第三天

Android开发_记事本数据库