世事洞明皆学问,人情练达即文章。这篇文章主要讲述Android 开发笔记___登陆app相关的知识,希望能为你提供帮助。
1 package com.example.alimjan.hello_world; 2 3 /** 4* Created by alimjan on 7/4/2017. 5*/ 6 7 8import android.content.Context; 9import android.support.v7.app.AppCompatActivity; 10import android.app.AlertDialog; 11import android.content.DialogInterface; 12import android.content.Intent; 13import android.os.Bundle; 14import android.text.Editable; 15import android.text.TextWatcher; 16import android.view.View; 17import android.view.View.OnClickListener; 18import android.widget.AdapterView; 19import android.widget.ArrayAdapter; 20import android.widget.Button; 21import android.widget.CheckBox; 22import android.widget.CompoundButton; 23import android.widget.EditText; 24import android.widget.RadioButton; 25import android.widget.RadioGroup; 26import android.widget.Spinner; 27import android.widget.TextView; 28import android.widget.Toast; 29import android.widget.AdapterView.OnItemSelectedListener; 30 31 public class class_3_6 extends AppCompatActivity implements OnClickListener { 32 33private RadioGroup rg_login; 34private RadioButton rb_password; 35private RadioButton rb_verifycode; 36private EditText et_phone; 37private TextView tv_password; 38private EditText et_password; 39private Button btn_forget; 40private CheckBox ck_remember; 41private Button btn_login; 42 43private int mRequestCode = 0; 44private int mType = 0; 45private boolean bRemember = false; 46private String mPassword = "111111"; 47private String mVerifyCode; 48 49@Override 50protected void onCreate(Bundle savedInstanceState) { 51super.onCreate(savedInstanceState); 52setContentView(R.layout.code_3_6); 53rg_login = (RadioGroup) findViewById(R.id.rg_login); 54rb_password = (RadioButton) findViewById(R.id.rb_password); 55rb_verifycode = (RadioButton) findViewById(R.id.rb_verifycode); 56et_phone = (EditText) findViewById(R.id.et_phone); 57tv_password = (TextView) findViewById(R.id.tv_password); 58et_password = (EditText) findViewById(R.id.et_password); 59btn_forget = (Button) findViewById(R.id.btn_forget); 60ck_remember = (CheckBox) findViewById(R.id.ck_remember); 61btn_login = (Button) findViewById(R.id.btn_login); 62 63rg_login.setOnCheckedChangeListener(new RadioListener()); 64ck_remember.setOnCheckedChangeListener(new CheckListener()); 65et_phone.addTextChangedListener(new HideTextWatcher(et_phone)); 66et_password.addTextChangedListener(new HideTextWatcher(et_password)); 67btn_forget.setOnClickListener(this); 68btn_login.setOnClickListener(this); 69 70ArrayAdapter< String> typeAdapter = new ArrayAdapter< String> (this, 71R.layout.item_select, typeArray); 72typeAdapter.setDropDownViewResource(R.layout.item_dropdown); 73Spinner sp_type = (Spinner) findViewById(R.id.sp_type); 74sp_type.setPrompt("请选择用户类型"); 75sp_type.setAdapter(typeAdapter); 76sp_type.setSelection(mType); 77sp_type.setOnItemSelectedListener(new TypeSelectedListener()); 78} 79 80private class RadioListener implements RadioGroup.OnCheckedChangeListener { 81@Override 82public void onCheckedChanged(RadioGroup group, int checkedId) { 83if (checkedId == R.id.rb_password) { 84tv_password.setText("登录密码:"); 85et_password.setHint("请输入密码"); 86btn_forget.setText("忘记密码"); 87ck_remember.setVisibility(View.VISIBLE); 88} else if (checkedId == R.id.rb_verifycode) { 89tv_password.setText(" 验证码:"); 90et_password.setHint("请输入验证码"); 91btn_forget.setText("获取验证码"); 92ck_remember.setVisibility(View.INVISIBLE); 93} 94} 95} 96 97private String[] typeArray = {"个人用户", "公司用户"}; 98class TypeSelectedListener implements OnItemSelectedListener { 99public void onItemSelected(AdapterView< ?> arg0, View arg1, int arg2, long arg3) { 100mType = arg2; 101} 102 103public void onNothingSelected(AdapterView< ?> arg0) { 104} 105} 106 107private class CheckListener implements CompoundButton.OnCheckedChangeListener { 108@Override 109public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 110if (buttonView.getId() == R.id.ck_remember) { 111bRemember = isChecked; 112} 113} 114} 115 116private class HideTextWatcher implements TextWatcher { 117private EditText mView; 118private int mMaxLength; 119private CharSequence mStr; 120 121public HideTextWatcher(EditText v) { 122super(); 123mView = v; 124mMaxLength = ViewUtil.getMaxLength(v); 125} 126 127@Override 128public void beforeTextChanged(CharSequence s, int start, int count, int after) { 129} 130 131@Override 132public void onTextChanged(CharSequence s, int start, int before, int count) { 133mStr = s; 134} 135 136@Override 137public void afterTextChanged(Editable s) { 138if (mStr == null || mStr.length() == 0) 139return; 140if ((mStr.length() == 11 & & mMaxLength == 11) || 141(mStr.length() == 6 & & mMaxLength == 6)) { 142ViewUtil.hideOneInputMethod(class_3_6.this, mView); 143} 144} 145} 146 147@Override 148public void onClick(View v) { 149String phone = et_phone.getText().toString(); 150if (v.getId() == R.id.btn_forget) { 151if (phone==null || phone.length()< 11) { 152Toast.makeText(this, "请输入正确的手机号", Toast.LENGTH_SHORT).show(); 153return; 154} 155if (rb_password.isChecked() == true) { 156Intent intent = new Intent(this, class_3_6_1.class); 157intent.putExtra("phone", phone); 158startActivityForResult(intent, mRequestCode); 159} else if (rb_verifycode.isChecked() == true) { 160mVerifyCode = String.format("%06d", (int)(Math.random()*1000000%1000000)); 161AlertDialog.Builder builder = new AlertDialog.Builder(this); 162builder.setTitle("请记住验证码"); 163builder.setMessage("手机号"+phone+",本次验证码是"+mVerifyCode+",请输入验证码"); 164builder.setPositiveButton("好的", null); 165AlertDialog alert = builder.create(); 166alert.show(); 167} 168} else if (v.getId() == R.id.btn_login) { 169if (phone==null || phone.length()< 11) { 170Toast.makeText(this, "请输入正确的手机号", Toast.LENGTH_SHORT).show(); 171return; 172} 173if (rb_password.isChecked() == true) { 174if (et_password.getText().toString().equals(mPassword) != true) { 175Toast.makeText(this, "请输入正确的密码", Toast.LENGTH_SHORT).show(); 176return; 177} else { 178loginSuccess(); 179} 180} else if (rb_verifycode.isChecked() == true) { 181if (et_password.getText().toString().equals(mVerifyCode) != true) { 182Toast.makeText(this, "请输入正确的验证码", Toast.LENGTH_SHORT).show(); 183return; 184} else { 185loginSuccess(); 186} 187} 188} 189} 190 191@Override 192protected void onActivityResult(int requestCode, int resultCode, Intent data) { 193if (requestCode == mRequestCode & & data!=null) { 194//用户密码已改为新密码 195mPassword = data.getStringExtra("new_password"); 196} 197} 198 199//从修改密码页面返回登录页面,要清空密码的输入框 200@Override 201protected void onRestart() { 202et_password.setText(""); 203super.onRestart(); 204} 205 206private void loginSuccess() { 207String desc = String.format("您的手机号码是%s,类型是%s。恭喜你通过登录验证,点击“确定”按钮返回上个页面", 208et_phone.getText().toString(), typeArray[mType]); 209AlertDialog.Builder builder = new AlertDialog.Builder(this); 210builder.setTitle("登录成功"); 211builder.setMessage(desc); 212builder.setPositiveButton("确定返回", new DialogInterface.OnClickListener() { 213@Override 214public void onClick(DialogInterface dialog, int which) { 215finish(); 216} 217}); 218builder.setNegativeButton("我再看看", null); 219AlertDialog alert = builder.create(); 220alert.show(); 221} 222 223public static void startHome(Context mContext) { 224Intent intent = new Intent(mContext, class_3_6.class); 225mContext.startActivity(intent); 226} 227 228 }
1 package com.example.alimjan.hello_world; 2 3 /** 4* Created by alimjan on 7/4/2017. 5*/ 6 7import android.app.Activity; 8import android.support.v7.app.AppCompatActivity; 9import android.app.AlertDialog; 10import android.content.Intent; 11import android.os.Bundle; 12import android.view.View; 13import android.view.View.OnClickListener; 14import android.widget.EditText; 15import android.widget.Toast; 16 17 public class class_3_6_1 extends AppCompatActivity implements OnClickListener { 18 19private EditText et_password_first; 20private EditText et_password_second; 21private EditText et_verifycode; 22private String mVerifyCode; 23private String mPhone; 24 25@Override 26protected void onCreate(Bundle savedInstanceState) { 27super.onCreate(savedInstanceState); 28setContentView(R.layout.code_3_6_1); 29et_password_first = (EditText) findViewById(R.id.et_password_first); 30et_password_second = (EditText) findViewById(R.id.et_password_second); 31et_verifycode = (EditText) findViewById(R.id.et_verifycode); 32findViewById(R.id.btn_verifycode).setOnClickListener(this); 33findViewById(R.id.btn_confirm).setOnClickListener(this); 34mPhone = getIntent().getStringExtra("phone"); 35} 36 37@Override 38public void onClick(View v) { 39if (v.getId() == R.id.btn_verifycode) { 40if (mPhone==null || mPhone.length()< 11) { 41Toast.makeText(this, "请输入正确的手机号", Toast.LENGTH_SHORT).show(); 42return; 43} 44mVerifyCode = String.format("%06d", (int) (Math.random() * 1000000 % 1000000)); 45AlertDialog.Builder builder = new AlertDialog.Builder(this); 46builder.setTitle("请记住验证码"); 47builder.setMessage("手机号"+mPhone+",本次验证码是"+mVerifyCode+",请输入验证码"); 48builder.setPositiveButton("好的", null); 49AlertDialog alert = builder.create(); 50alert.show(); 51} else if (v.getId() == R.id.btn_confirm) { 52String password_first = et_password_first.getText().toString(); 53String password_second = et_password_second.getText().toString(); 54if (password_first==null || password_first.length()< 6 || 55password_second==null || password_second.length()< 6) { 56Toast.makeText(this, "请输入正确的新密码", Toast.LENGTH_SHORT).show(); 57return; 58} 59if (password_first.equals(password_second) != true) { 60Toast.makeText(this, "两次输入的新密码不一致", Toast.LENGTH_SHORT).show(); 61return; 62} 63if (et_verifycode.getText().toString().equals(mVerifyCode) != true) { 64Toast.makeText(this, "请输入正确的验证码", Toast.LENGTH_SHORT).show(); 65return; 66} else { 67Toast.makeText(this, "密码修改成功", Toast.LENGTH_SHORT).show(); 68Intent intent = new Intent(); 69intent.putExtra("new_password", password_first); 70setResult(Activity.RESULT_OK, intent); 71finish(); 72} 73} 74} 75 76 }
【Android 开发笔记___登陆app】
1 < LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2android:layout_width="match_parent" 3android:layout_height="match_parent" 4android:focusable="true" 5android:focusableInTouchMode="true" 6android:orientation="vertical" 7android:padding="5dp" > 8 9< RadioGroup 10android:id="@+id/rg_login" 11android:layout_width="match_parent" 12android:layout_height="60dp" 13android:orientation="horizontal" > 14 15< RadioButton 16android:id="@+id/rb_password" 17android:layout_width="0dp" 18android:layout_height="match_parent" 19android:layout_weight="1" 20android:checked="true" 21android:gravity="left|center" 22android:text="密码登录" 23android:textColor="@color/black" 24android:textSize="17sp" /> 25 26< RadioButton 27android:id="@+id/rb_verifycode" 28android:layout_width="0dp" 29android:layout_height="match_parent" 30android:layout_weight="1" 31android:checked="false" 32android:gravity="left|center" 33android:text="验证码登录" 34android:textColor="@color/black" 35android:textSize="17sp" /> 36< /RadioGroup> 37 38< RelativeLayout 39android:layout_width="match_parent" 40android:layout_height="60dp" > 41 42< TextView 43android:id="@+id/tv_type" 44android:layout_width="wrap_content" 45android:layout_height="match_parent" 46android:layout_alignParentLeft="true" 47android:gravity="center" 48android:text="我是:" 49android:textColor="@color/black" 50android:textSize="17sp" /> 51 52< Spinner 53android:id="@+id/sp_type" 54android:layout_width="match_parent" 55android:layout_height="match_parent" 56android:layout_toRightOf="@+id/tv_type" 57android:gravity="left|center" 58android:spinnerMode="dialog" /> 59< /RelativeLayout> 60 61< RelativeLayout 62android:layout_width="match_parent" 63android:layout_height="60dp" > 64 65< TextView 66android:id="@+id/tv_phone" 67android:layout_width="wrap_content" 68android:layout_height="match_parent" 69android:layout_alignParentLeft="true" 70android:gravity="center" 71android:text="手机号码:" 72android:textColor="@color/black" 73android:textSize="17sp" /> 74 75< EditText 76android:id="@+id/et_phone" 77android:layout_width="match_parent" 78android:layout_height="match_parent" 79android:layout_marginBottom="5dp" 80
推荐阅读
- android 聊天室窗体
- Android 65K问题之Multidex原理分析及NoClassDefFoundError的解决方法
- hdoj 4925 Apple tree 最小割
- React Native Android打包apk
- SEO关键字研究工具合集(20大Google关键字规划师替代品)
- 如何修复Android你的连接不是私密的错误(解决方法教程)
- 远程桌面软件(Windows和Mac的10大最佳免费TeamViewer替代品)
- 如何修复Windows 10 NVIDIA GeForce Experience无法打开(解决方法)
- error launching installer,本文教您安装时出现 Error launching installer怎样处理