首页 > 代码库 > Android 开发笔记___登陆app

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:layout_marginTop="5dp"
 81             android:layout_toRightOf="@+id/tv_phone"
 82             android:background="@drawable/editext_selector"
 83             android:gravity="left|center"
 84             android:hint="请输入手机号码"
 85             android:inputType="number"
 86             android:maxLength="11"
 87             android:textColor="@color/black"
 88             android:textColorHint="@color/grey"
 89             android:textCursorDrawable="@drawable/text_cursor"
 90             android:textSize="17sp" />
 91     </RelativeLayout>
 92 
 93     <RelativeLayout
 94         android:layout_width="match_parent"
 95         android:layout_height="60dp" >
 96 
 97         <TextView
 98             android:id="@+id/tv_password"
 99             android:layout_width="wrap_content"
100             android:layout_height="match_parent"
101             android:layout_alignParentLeft="true"
102             android:gravity="center"
103             android:text="登录密码:"
104             android:textColor="@color/black"
105             android:textSize="17sp" />
106 
107         <FrameLayout
108             android:layout_width="match_parent"
109             android:layout_height="match_parent"
110             android:layout_toRightOf="@+id/tv_password" >
111 
112             <EditText
113                 android:id="@+id/et_password"
114                 android:layout_width="match_parent"
115                 android:layout_height="match_parent"
116                 android:layout_marginBottom="5dp"
117                 android:layout_marginTop="5dp"
118                 android:background="@drawable/editext_selector"
119                 android:gravity="left|center"
120                 android:hint="请输入密码"
121                 android:inputType="numberPassword"
122                 android:maxLength="6"
123                 android:textColor="@color/black"
124                 android:textColorHint="@color/grey"
125                 android:textCursorDrawable="@drawable/text_cursor"
126                 android:textSize="17sp" />
127 
128             <Button
129                 android:id="@+id/btn_forget"
130                 android:layout_width="wrap_content"
131                 android:layout_height="match_parent"
132                 android:layout_gravity="right"
133                 android:gravity="center"
134                 android:text="忘记密码"
135                 android:textColor="@color/black"
136                 android:textSize="17sp" />
137         </FrameLayout>
138     </RelativeLayout>
139 
140     <CheckBox
141         android:id="@+id/ck_remember"
142         android:layout_width="match_parent"
143         android:layout_height="wrap_content"
144         android:button="@drawable/checkbox_selector"
145         android:checked="false"
146         android:padding="10dp"
147         android:text="记住密码"
148         android:textColor="@color/black"
149         android:textSize="17sp" />
150 
151     <Button
152         android:id="@+id/btn_login"
153         android:layout_width="match_parent"
154         android:layout_height="wrap_content"
155         android:text="登录"
156         android:textColor="@color/black"
157         android:textSize="22sp" />
158 
159 </LinearLayout>

 

 

  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     <RelativeLayout
 10         android:layout_width="match_parent"
 11         android:layout_height="60dp" >
 12 
 13         <TextView
 14             android:id="@+id/tv_password_first"
 15             android:layout_width="wrap_content"
 16             android:layout_height="match_parent"
 17             android:layout_alignParentLeft="true"
 18             android:gravity="center"
 19             android:text="输入新密码:"
 20             android:textColor="@color/black"
 21             android:textSize="17sp" />
 22 
 23         <EditText
 24             android:id="@+id/et_password_first"
 25             android:layout_width="match_parent"
 26             android:layout_height="match_parent"
 27             android:layout_marginBottom="5dp"
 28             android:layout_marginTop="5dp"
 29             android:layout_toRightOf="@+id/tv_password_first"
 30             android:background="@drawable/editext_selector"
 31             android:gravity="left|center"
 32             android:hint="请输入新密码"
 33             android:inputType="numberPassword"
 34             android:maxLength="11"
 35             android:textColor="@color/black"
 36             android:textColorHint="@color/grey"
 37             android:textCursorDrawable="@drawable/text_cursor"
 38             android:textSize="17sp" />
 39     </RelativeLayout>
 40 
 41     <RelativeLayout
 42         android:layout_width="match_parent"
 43         android:layout_height="60dp" >
 44 
 45         <TextView
 46             android:id="@+id/tv_password_second"
 47             android:layout_width="wrap_content"
 48             android:layout_height="match_parent"
 49             android:layout_alignParentLeft="true"
 50             android:gravity="center"
 51             android:text="确认新密码:"
 52             android:textColor="@color/black"
 53             android:textSize="17sp" />
 54 
 55         <EditText
 56             android:id="@+id/et_password_second"
 57             android:layout_width="match_parent"
 58             android:layout_height="match_parent"
 59             android:layout_marginBottom="5dp"
 60             android:layout_marginTop="5dp"
 61             android:layout_toRightOf="@+id/tv_password_second"
 62             android:background="@drawable/editext_selector"
 63             android:gravity="left|center"
 64             android:hint="请再次输入新密码"
 65             android:inputType="numberPassword"
 66             android:maxLength="11"
 67             android:textColor="@color/black"
 68             android:textColorHint="@color/grey"
 69             android:textCursorDrawable="@drawable/text_cursor"
 70             android:textSize="17sp" />
 71     </RelativeLayout>
 72 
 73     <RelativeLayout
 74         android:layout_width="match_parent"
 75         android:layout_height="60dp" >
 76 
 77         <TextView
 78             android:id="@+id/tv_verifycode"
 79             android:layout_width="wrap_content"
 80             android:layout_height="match_parent"
 81             android:layout_alignParentLeft="true"
 82             android:gravity="center"
 83             android:text="  验证码:"
 84             android:textColor="@color/black"
 85             android:textSize="17sp" />
 86 
 87         <FrameLayout
 88             android:layout_width="match_parent"
 89             android:layout_height="match_parent"
 90             android:layout_toRightOf="@+id/tv_verifycode" >
 91 
 92             <EditText
 93                 android:id="@+id/et_verifycode"
 94                 android:layout_width="match_parent"
 95                 android:layout_height="match_parent"
 96                 android:layout_marginBottom="5dp"
 97                 android:layout_marginTop="5dp"
 98                 android:background="@drawable/editext_selector"
 99                 android:gravity="left|center"
100                 android:hint="请输入验证码"
101                 android:inputType="numberPassword"
102                 android:maxLength="6"
103                 android:textColor="@color/black"
104                 android:textColorHint="@color/grey"
105                 android:textCursorDrawable="@drawable/text_cursor"
106                 android:textSize="17sp" />
107 
108             <Button
109                 android:id="@+id/btn_verifycode"
110                 android:layout_width="wrap_content"
111                 android:layout_height="match_parent"
112                 android:layout_gravity="right"
113                 android:gravity="center"
114                 android:text="获取验证码"
115                 android:textColor="@color/black"
116                 android:textSize="17sp" />
117         </FrameLayout>
118     </RelativeLayout>
119 
120     <Button
121         android:id="@+id/btn_confirm"
122         android:layout_width="match_parent"
123         android:layout_height="wrap_content"
124         android:text="确定"
125         android:textColor="@color/black"
126         android:textSize="22sp" />
127 
128 </LinearLayout>

 

技术分享技术分享

 

Android 开发笔记___登陆app