GuideMap 登录界面 详细文档(三)
2016-03-05 23:18
435 查看
六、LoginActivity 登录类
UI界面
1、获取已经保存好的用户密码 private void getSavedUsers() 2、初始化 private void initView() 3、点击事件 @Override public void onClick(View v) 4、退出此Activity时保存users @Override public void onPause() 5、账户名下拉框相关 弹出窗口初始化 public void initPop() 内部类,账户名下拉框的listView的适配器 class MyAapter extends ArrayAdapter<User> PopupWindow对象dismiss时的事件 public void onDismiss() 点击listView public void onItemClick() 6、监听editText编辑框的变化 private void setListener() 7、显示载入中界面 private void showLoginingDlg() private void closeLoginingDlg() private void initLoginingDlg()
/** * 登陆界面 */ public class LoginActivity extends AppCompatActivity implements View.OnClickListener, AdapterView.OnItemClickListener , PopupWindow.OnDismissListener{ protected static final String TAG = "LoginActivity"; private Button mLoginButton; private TextView mErrorLoginButton; private TextView mRegisterButton; private EditText mUserAccountEdit; //用户名编辑框 private EditText mUserpasswordEdit; //密码编辑框 private Toolbar toolbar; private LinearLayout mLoginLinearLayout; // 登录内容的容器 private LinearLayout mUserAccountLinearLayout; // 将下拉弹出窗口在此容器下方显示 private Animation mTranslate; // 位移动画 private Dialog mLoginingDlg; // 显示正在登录的Dialog private ImageView mMoreUser; // 下拉图标 private ImageView mLoginMoreUserView; // 弹出下拉弹出窗的按钮 private String mAccount; private String mPassword; private ArrayList<User> mUsers; // 用户列表 private ListView mUserAccountListView; // 下拉弹出窗显示的ListView对象 private PopupWindow mPop; // 下拉弹出窗 private MyAapter mAdapter; // ListView的监听器 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); initView(); getSavedUsers(); setListener(); mLoginLinearLayout.startAnimation(mTranslate); // Y轴水平移动 } private void getSavedUsers(){ /* 获取已经保存好的用户密码 */ mUsers= UserLoginSave.getUserList(LoginActivity.this); if (mUsers.size()>0){ /* 将列表中的第一个user显示在编辑框 */ mUserAccountEdit.setText(mUsers.get(0).GetAccount()); mAccount=mUsers.get(0).GetAccount(); mUserpasswordEdit.setText(mUsers.get(0).GetPassword()); mPassword=mUsers.get(0).GetPassword(); } LinearLayout parent = (LinearLayout) getLayoutInflater().inflate( R.layout.userifo_listview, null); mUserAccountListView = (ListView) parent.findViewById(android.R.id.list); parent.removeView(mUserAccountListView); // 必须脱离父子关系,不然会报错 mUserAccountListView.setOnItemClickListener(this); // 设置点击事件 mAdapter = new MyAapter(mUsers); mUserAccountListView.setAdapter(mAdapter); } private void initView(){ mUserAccountEdit=(EditText)findViewById(R.id.login_edtAccount); mUserpasswordEdit=(EditText)findViewById(R.id.login_edtPwd); mLoginButton=(Button)findViewById(R.id.login_btn); mLoginButton.setOnClickListener(this); mErrorLoginButton=(TextView)findViewById(R.id.loginerror_tv); mErrorLoginButton.setOnClickListener(this); mRegisterButton=(TextView)findViewById(R.id.register_tv); mRegisterButton.setOnClickListener(this); mMoreUser = (ImageView) findViewById(R.id.login_more_user); mLoginButton = (Button) findViewById(R.id.login_btn); mLoginMoreUserView = (ImageView) findViewById(R.id.login_more_user); mLoginLinearLayout = (LinearLayout) findViewById(R.id.login_linearLayout); mUserAccountLinearLayout = (LinearLayout) findViewById(R.id.userAccount_LinearLayout); mTranslate = AnimationUtils.loadAnimation(this, R.anim.my_translate); // 初始化动画对象 initLoginingDlg(); } @Override public void onClick(View v) { int id=v.getId(); switch (id){ case R.id.login_btn: Toast.makeText(this, "login", Toast.LENGTH_LONG).show(); showLoginingDlg();//显示"正在登录"对话框 Log.i(TAG, mAccount + " " + mPassword); if (mAccount == null || mAccount.equals("")) { // 账号为空时 Toast.makeText(LoginActivity.this, "请输入账号", Toast.LENGTH_SHORT) .show(); } else if (mPassword == null || mPassword.equals("")) { // 密码为空时 Toast.makeText(LoginActivity.this, "请输入密码", Toast.LENGTH_SHORT) .show(); } else { // 账号和密码都不为空时 boolean mIsSave = true; try { Log.i(TAG, "保存用户列表"); for (User user : mUsers) { // 判断本地文档是否有此ID用户 if (user.GetAccount().equals(mAccount)) { mIsSave = false; Toast.makeText(LoginActivity.this, "本地有此ID", Toast.LENGTH_SHORT) .show(); break; } } if (mIsSave) { // 将新用户加入users Toast.makeText(LoginActivity.this, "保存ID", Toast.LENGTH_SHORT) .show(); User user = new User(mAccount, mPassword); mUsers.add(user); } } catch (Exception e) { e.printStackTrace(); } /** * 检查用户账户是否存在,返回0则存在 * */ VolleyIO.JsonGet(Constant.Url_isAccountExist + "account=" + mAccount, new VolleyInterface(LoginActivity.this, VolleyInterface.mListener, VolleyInterface.mErrorListener) { @Override public void onMySuccess(JSONObject result) { Log.i(TAG, Constant.Url_isAccountExist + "account=" + mAccount); if (result.optString("errorName").equals("0")) { /** * 进行登录操作,返回0则登陆成功 */ VolleyIO.JsonGet(Constant.LoginUrl + "account=" + mAccount + "&" + "password=" + mPassword, new VolleyInterface(LoginActivity.this, VolleyInterface.mListener, VolleyInterface.mErrorListener) { @Override public void onMySuccess(JSONObject result) { Log.i(TAG, result.toString()); if (result.optString("errorName").equals("0")) { Toast.makeText(LoginActivity.this, "登陆成功1", Toast.LENGTH_SHORT).show(); /** * 获取用户信息 */ VolleyIO.JsonGet(Constant.Url_getinfo + "account=" + mAccount, new VolleyInterface(LoginActivity.this, VolleyInterface.mListener, VolleyInterface.mErrorListener) { @Override public void onMySuccess(JSONObject result) { Toast.makeText(LoginActivity.this, "登陆成功2", Toast.LENGTH_SHORT).show(); Gson gson = new Gson(); User user = gson.fromJson(result.toString(), User.class); LoginUser.account = user.GetAccount(); LoginUser.password = user.GetPassword(); LoginUser.name = user.GetName(); LoginUser.phone = user.GetPhone(); LoginUser.mail = user.GetMail(); closeLoginingDlg();// 关闭对话框 try { UserLoginSave.saveUserList(LoginActivity.this, mUsers); } catch (Exception e) { e.printStackTrace(); } finish(); } @Override public void onMyError(VolleyError error) { } }); } else { Toast.makeText(LoginActivity.this, "密码错误", Toast.LENGTH_SHORT).show(); } } @Override public void onMyError(VolleyError error) { Log.i(TAG, "onMyError"); } }); } else { Toast.makeText(LoginActivity.this, "用户名不存在", Toast.LENGTH_SHORT).show(); } } @Override public void onMyError(VolleyError error) { Toast.makeText(LoginActivity.this, "登录失败", Toast.LENGTH_LONG).show(); Log.i(TAG, Constant.Url_isAccountExist + "account=" + mAccount); } }); } break; case R.id.loginerror_tv: Toast.makeText(this, "login error", Toast.LENGTH_LONG).show(); /** * * 测试 */ VolleyIO.JsonGet(Constant.TestUrl, new VolleyInterface(LoginActivity.this, VolleyInterface.mListener, VolleyInterface.mErrorListener) { @Override public void onMySuccess(JSONObject result) { Gson gson = new Gson(); Test test = gson.fromJson(result.toString(), Test.class); LoginUser.name=test.getCity(); } @Override public void onMyError(VolleyError error) { } }); break; case R.id.register_tv: Intent intent=new Intent(); intent.setClass(this, RegisterActivity.class); startActivity(intent); Toast.makeText(this, "register", Toast.LENGTH_LONG).show(); break; case R.id.login_more_user: // 当点击下拉栏 Log.i(TAG, "当点击下拉栏"); if (mPop == null) { initPop(); } if (!mPop.isShowing() && mUsers.size() > 0) { Log.i(TAG, "切换为角向上图标"); mMoreUser.setImageResource(R.drawable.login_more_up); // 切换图标 mPop.showAsDropDown(mUserAccountLinearLayout, 2, 1); // 显示弹出窗口 } break; } } /* 退出此Activity时保存users */ @Override public void onPause() { super.onPause(); try { UserLoginSave.saveUserList(LoginActivity.this,mUsers); } catch (Exception e) { e.printStackTrace(); } } /** * PopupWindow对象dismiss时的事件 */ @Override public void onDismiss() { mMoreUser.setImageResource(R.drawable.login_more_up); } @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { mUserAccountEdit.setText(mUsers.get(position).GetAccount()); mUserpasswordEdit.setText(mUsers.get(position).GetPassword()); mPop.dismiss(); } /** * ListView的适配器 * */ class MyAapter extends ArrayAdapter<User> { public MyAapter(ArrayList<User> users) { super(LoginActivity.this, 0, users); } public View getView(final int position, View convertView, ViewGroup parent) { if (convertView == null) { convertView = getLayoutInflater().inflate( R.layout.login_listview_item, null); } TextView userIdText = (TextView) convertView .findViewById(R.id.listview_userAccount); userIdText.setText(getItem(position).GetAccount()); ImageView deleteUser = (ImageView) convertView .findViewById(R.id.login_delete_user); deleteUser.setOnClickListener(new View.OnClickListener() { // 点击删除deleteUser时,在mUsers中删除选中的元素 @Override public void onClick(View v) { if (getItem(position).GetAccount().equals(mAccount)) { // 如果要删除的用户Id和Id编辑框当前值相等,则清空 mAccount = ""; mPassword = ""; mUserAccountEdit.setText(mAccount); mUserpasswordEdit.setText(mPassword); } mUsers.remove(getItem(position)); mAdapter.notifyDataSetChanged(); // 更新ListView } }); return convertView; } } /** * 监听EditText的变化 */ private void setListener() { mUserAccountEdit.addTextChangedListener(new TextWatcher() { //变化中 public void onTextChanged(CharSequence s, int start, int before, int count) { mAccount = s.toString(); } //变化前 public void beforeTextChanged(CharSequence s, int start, int count, int after) { } //变化后 public void afterTextChanged(Editable s) { } }); mUserpasswordEdit.addTextChangedListener(new TextWatcher() { public void onTextChanged(CharSequence s, int start, int before, int count) { mPassword = s.toString(); } public void beforeTextChanged(CharSequence s, int start, int count, int after) { } public void afterTextChanged(Editable s) { } }); mLoginButton.setOnClickListener(this); mLoginMoreUserView.setOnClickListener(this); } /** * 显示正在登录对话框 */ private void showLoginingDlg() { if (mLoginingDlg != null) mLoginingDlg.show(); } /** * 关闭正在登录对话框 */ private void closeLoginingDlg() { if (mLoginingDlg != null && mLoginingDlg.isShowing()) mLoginingDlg.dismiss(); } /** * 弹出窗口 */ public void initPop() { int width = mUserAccountLinearLayout.getWidth() - 4; int height = LinearLayout.LayoutParams.WRAP_CONTENT; mPop = new PopupWindow(mUserAccountListView, width, height, true); mPop.setOnDismissListener(this);// 设置弹出窗口消失时监听器 // 注意要加这句代码,点击弹出窗口其它区域才会让窗口消失 mPop.setBackgroundDrawable(new ColorDrawable(0xffffffff)); } /** * * * 初始化正在登录对话框 * * */ private void initLoginingDlg() { mLoginingDlg = new Dialog(this, R.style.loginingDlg); mLoginingDlg.setContentView(R.layout.logining_dlg); Window window = mLoginingDlg.getWindow(); WindowManager.LayoutParams params = window.getAttributes(); // 获取和mmLoginingDlg关联的当前窗口的属性,从而设置它在屏幕中显示的位置 // 获取屏幕的高宽 DisplayMetrics dm = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(dm); int cxScreen = dm.widthPixels; int cyScreen = dm.heightPixels; int height = (int) getResources().getDimension( R.dimen.loginingdlg_height);// 高42dp int lrMargin = (int) getResources().getDimension( R.dimen.loginingdlg_lr_margin); // 左右边沿10dp int topMargin = (int) getResources().getDimension( R.dimen.loginingdlg_top_margin); // 上沿20dp params.y = (-(cyScreen - height) / 2) + topMargin; // -199 /* 对话框默认位置在屏幕中心,所以x,y表示此控件到"屏幕中心"的偏移量 */ params.width = cxScreen; params.height = height; // width,height表示mLoginingDlg的实际大小 mLoginingDlg.setCanceledOnTouchOutside(true); // 设置点击Dialog外部任意区域关闭Dialog } }
UI界面
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/login3"> <RelativeLayout android:id="@+id/login_layout" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:layout_marginRight="20dp" android:gravity="center" > <LinearLayout android:id="@+id/login_linearLayout" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginLeft="15dp" android:layout_marginRight="15dp" android:layout_marginTop="80dp" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/white" android:orientation="vertical" > <LinearLayout android:id="@+id/userAccount_LinearLayout" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="right" android:orientation="horizontal" > <!-- android:ems="10"表示EditText内最多只显示10个字符,超出不显示 --> <!-- android:singleLine="true"表示不能全部显示时,后面用“…”来表示 --> <!-- android:background="@null"不显示默认编辑框背景 --> <EditText android:id="@+id/login_edtAccount" android:layout_width="0dp" android:layout_height="44dp" android:layout_weight="1" android:background="@null" android:ems="10" android:hint="@string/idtxthint" android:paddingLeft="15dp" android:paddingRight="15dp" android:singleLine="true" android:textColorHint="#999999" android:textSize="18sp" android:textColor="@color/black"> <requestFocus /> </EditText> <ImageView android:id="@+id/login_more_user" android:layout_width="wrap_content" android:layout_height="44dp" android:layout_marginRight="15dp" android:layout_marginTop="5dp" android:src="@drawable/login_more_up" /> </LinearLayout> <!-- 横线 --> <View android:layout_width="match_parent" android:layout_height="2dp" android:background="#CACDD1" /> <EditText android:id="@+id/login_edtPwd" android:layout_width="match_parent" android:layout_height="44dp" android:background="@null" android:hint="@string/pwdtxthint" android:inputType="textPassword" android:paddingLeft="15dp" android:paddingRight="0dp" android:singleLine="true" android:textColorHint="#999999" android:textColor="@color/black"/> <!-- 提示信息的颜色 --> android:textSize="18sp" /> </LinearLayout> <Button android:id="@+id/login_btn" android:layout_width="fill_parent" android:layout_height="40dp" android:layout_marginTop="30dp" android:background="#ff336699" android:textColor="@android:color/white" android:gravity="center" android:text="登录" /> </LinearLayout> <TextView android:id="@+id/register_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="注册" android:textSize="24sp" android:textColor="@color/white" android:layout_below="@id/login_linearLayout" android:layout_marginTop="30dp" android:layout_alignLeft="@id/login_linearLayout"/> <TextView android:id="@+id/loginerror_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="忘记密码" android:textSize="24sp" android:textColor="@color/white" android:layout_below="@id/login_linearLayout" android:layout_marginTop="30dp" android:layout_alignRight="@id/login_linearLayout"/> </RelativeLayout> </RelativeLayout>
相关文章推荐
- GuideMap 登陆界面 详细文档(二)
- GuideMap 登陆界面 详细文档(一)
- PAT-A1085. Perfect Sequence (25)
- 西南BANG 之 android学习笔记1——5个android UI 布局
- LeetCode96——Unique Binary Search Trees
- 334. Increasing Triplet Subsequence
- UICollectionView-Flowlayout
- Subsequence(尺取法)
- UIImage 的imageWithContentsOfFile:path和imageNamed的区别
- UESTC 1251 谕神的密码【暴力枚举】
- (3) IOS笔记本——UIScrollView的其他应用
- POJ2299 Ultra-QuickSort 归并排序求逆序对
- 手势识别器(UIGestureRecognizer)- UIPanGestureRecognizer(拖拽)
- 手势识别器(UIGestureRecognizer)- UIPinchGestureRecognizer(捏合)
- [leetcode] 312. Burst Balloons
- 手势识别器(UIGestureRecognizer)- UIRotationGestureRecognizer(旋转)
- UESTC 758 P酱的冒险旅途【贪心】
- UESTC oj 758 P酱的冒险旅途
- 关于Float.MIN_VALUE 和 Float.MAX_VALUE
- 手势识别器(UIGestureRecognizer)- UISwipeGestureRecognizer(轻扫)